View Full Version : Emailing blues

02-17-2006, 10: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

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.

02-17-2006, 11:55 AM
You need to supply more info about your database if you want help

02-17-2006, 12:10 PM
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.


02-17-2006, 12:24 PM
Is this what you are looking for...

// 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 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

02-17-2006, 12:28 PM
That looks really encouraging, goughy000. Will try it and report back. Many thanks and cheers.

02-17-2006, 02: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.

02-17-2006, 05: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.

02-17-2006, 06: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

02-21-2006, 04: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.

02-21-2006, 05:31 PM
Many thanks, degsy, all noted. Thanks for making it clearer.

02-21-2006, 06:09 PM
$row[email] (no quotes) defines a constant.

That is just not proper syntax. It doesn't define anything.

$arr = array();

$arr[foo] = 'bar'; // Here you get an undefined constant notice.

var_dump(defined('foo')); // gives bool(false)

02-22-2006, 03: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.


02-22-2006, 06: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.

02-22-2006, 08: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.

02-22-2006, 09: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:

$var = "$variable[var]";

I have never got an error, on E_ALL error reporting. I will do a couple of tests though.

02-22-2006, 09: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.

02-22-2006, 09: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:


$var = array( "test" => "Hello" );
echo "$var[test]";


02-22-2006, 10:00 PM
I'm surprised aswell. It shouldn't, but it somehow does. I can't spell that damn word either :D .

02-22-2006, 11:11 PM
Concatenate. I might not be all that good of a programmer...but I did win a spelling bee.:p