zonkd 02-17-2006, 09:15 AM Everywhere says how easy it is to send out email newsletters with php.
Must be me, then, because I simply cannot find a successful way to send emails to a group of friends who I have in a mysql database.
I have the sql code, like
$result = @mysql_query("SELECT email FROM mates");
and then the php code
<?php
mail('mates@friends.net', 'Subject', 'My message.');
?>
But how can I make perhaps a variable of the sql code to get it to supply the address for the mail function, please?
And how can I get the function to send me a BCC of each message?
Guidance would be appreciated very much.
goughy000 02-17-2006, 10:55 AM You need to supply more info about your database if you want help
zonkd 02-17-2006, 11:10 AM hi goughy000
The code links to the mysql db and the table - "contacts" - has fname, sname, email and a few other fields. Mostly for this project, I just want to use the email addresses. (It would be great if I could start the email, Dear fname sname, but that isn't vital.)
And that's it. I enter the message in a message variable, I have a subject line, I have my email address as the from, and I ask for a copy of each email to come to me.
Cheers
goughy000 02-17-2006, 11:24 AM Is this what you are looking for...
<?php
// Set Variables
$mysql_server = "localhost";
$mysql_username = "zonkd";
$mysql_password = "psswd";
$mysql_database = "contactdb";
// Mysql conection
mysql_connect("$mysql_server", "$mysql_username", "$mysql_password");
mysql_select_db("$mysql_database");
// Mysql Query
$result = mysql_query("SELECT * FROM contacts");
// Goes through each result sending them the message
while($row = mysql_fetch_array($result)){
$subject = "Subject Here";
$message = "Dear $row[fname] $row[sname], bla bla";
mail("$row[email]", "$Subject", "$message");
}
// End code
?>
About the BCC... no idea, sorry
Havnt tested above code, you need to set the variables at the top of the code
zonkd 02-17-2006, 11:28 AM That looks really encouraging, goughy000. Will try it and report back. Many thanks and cheers.
degsy 02-17-2006, 01:40 PM Nit picking but you should use quotes around your variable name.
mail("$row[email]", "$Subject", "$message");
Seeing as though your string is wrapped in double quotes then use single quotes around the variable.
mail("$row['email']", "$Subject", "$message");
Not using the quotes means you are defining it as a constant.
zonkd 02-17-2006, 04:08 PM Worked well, thanks, goughy000, with a bit of a tweak here and there.
Oddly, degsy's suggestion didn't work. Well, it wouldn't accept it. Thanks, degsy, but couldn't follow through there.
Cheers, goughy000.
goughy000 02-17-2006, 05:15 PM I see what degsy is getting at, but can't see why its needed :confused:
zonkd.. happy to help :) you can PM me if you ever want help directly
degsy 02-21-2006, 03:13 PM Sorry, that should have been
mail($row['email'], $Subject, $message);
$row[email] (no quotes) defines a constant.
$row['email'] or $row["email"] defines a variable. The quotes you use depends on your code.
e.g. you could also use
mail("{$row['email']}", "$Subject", "$message");
Although there is really no point.
zonkd 02-21-2006, 04:31 PM Many thanks, degsy, all noted. Thanks for making it clearer.
marek_mar 02-21-2006, 05:09 PM $row[email] (no quotes) defines a constant.
That is just not proper syntax. It doesn't define anything.
<?php
$arr = array();
$arr[foo] = 'bar'; // Here you get an undefined constant notice.
var_dump(defined('foo')); // gives bool(false)
?>
degsy 02-22-2006, 02:23 PM Define was the wrong word. If you have a variable in the form of
$foo[bar] then PHP will first look for a constant before looking for a variable.
http://www.zend.com/zend/tut/using-strings.php?article=using-strings&kind=t&id=2569&open=1&anc=0&view=1#nonquoted
missing-score 02-22-2006, 05:01 PM It's incorrect syntax but its not incorrect inside of a string. Becuase everything is a string, the value between [ and ] is also considered to be a string and as such wont cause any errors if you dont have quotes. I prefer using braces ({}) and quotes becuase it looks better to me, but theres nothing wrong with either method IMO.
marek_mar 02-22-2006, 07:57 PM Using non-quoted strings gives you a notice... if you don't consider that as an error you shouldn't do it anyway. Defining a constant may make your script stop working as expected.
missing-score 02-22-2006, 08:11 PM Using non-quoted strings gives you a notice... if you don't consider that as an error you shouldn't do it anyway. Defining a constant may make your script stop working as expected.
If you use that within a string, eg:
<?php
$var = "$variable[var]";
?>
I have never got an error, on E_ALL error reporting. I will do a couple of tests though.
marek_mar 02-22-2006, 08:15 PM I have never tried to do that inside a string... I concate everything. I'll test too!
Just tested that. PHP won't register that as an array call. It'll just see $variable and omit the [var] part". After adding the brackets PHP will see it as an array call it but will still give a notice :)
No it works. Zend's IDE coloring isn't perfect.
missing-score 02-22-2006, 08:48 PM Myself, i can see why it works but am surprised that it does. Myself I prefer either enclosing array variables in braces or using concatentation (think I spelt that wrong, long day, dont blame me :P).
But this works fine under E_ALL error reporting on PHP 5.1.2:
<?php
$var = array( "test" => "Hello" );
echo "$var[test]";
?>
marek_mar 02-22-2006, 09:00 PM I'm surprised aswell. It shouldn't, but it somehow does. I can't spell that damn word either :D .
StupidRalph 02-22-2006, 10:11 PM Concatenate. I might not be all that good of a programmer...but I did win a spelling bee.:p
|
|