Email from database

I have a script (php) to read fname, lname and email address from database (Mysql) and send a message to each. The message subjectline and greeting come from a posted value. When I send, the script adds all names to the email and sends a copy to each.:

[INDENT]Greetings John Doe,
Greetings Jane Doe,

vhhvvh hvh hvhvh vh hv h vh vhvhhv h vh vh hv h vh hv h vh vh h vh vh hv!

hvhhv vh vh hv hvh h h h hhhhhh h hhh h h v.

Sincerely,
Me[/INDENT]

The code I am using is:

/* extract email address from database */
$query=“SELECT fname, lname, emailaddress FROM XXXX”;
$result=mysql_query($query);
$num=mysql_numrows($result);

/* mysql_close(); */

$i=0;
while ($i < $num) {

/*convert data to variable */

$fname =mysql_result($result,$i,"fname");
$lname =mysql_result($result,$i,"lname");
$to =mysql_result($result,$i,"emailaddress");

$greet = “$greeting”. mysql_result($result,$i,“fname”)." “.mysql_result($result,$i,“lname”).”, “;
$body = “$greet”.”
“. “$body”;
$headers = “From: $email”;
mail($to, $subject, $body, $headers);
echo $to . " sent <br>”;

$i++;
}
?>

Any suggestions?

I would also like to offer a suggestion… When doing a script like this, do not name it email.php. Unless you have added tags to keep search engines out, spammers can google “email.php” and they are very close to being able to hack into your database.

The script “as is” will send an email to every email address in the database, the problem is that each one gets all of the names. Each email starts with:
Hi John,
Hi Joe,
Hi Bill,…

All names in the database are listed at the begining of each email.

I thought by putting the email command within the loop and specifying " mysql_result($result,$i,“fname”)." " as the name to use, that the varibales would be walked through…

The completed project will only contain about 200-300 names, so mail() should handle it. I will also put a 3 second pause in after sending to keep on the good graces of my hosting service.

If my customer did not care about the personalization This would not be an issue…

Carl

Glad you got it working. As to how it works, let me walk you through it.
This is your original code:


$body = 'This is the content of the e-mail';
$greeting = 'Hello';
while ($i < $num) {

/*convert data to variable */

$fname =mysql_result($result,$i,"fname");
$lname =mysql_result($result,$i,"lname");
$to =mysql_result($result,$i,"emailaddress");


$greet = "$greeting". mysql_result($result,$i,"fname")." ".mysql_result($result,$i,"lname").", ";
$body = "$greet"."\
". "$body";
$headers = "From: $email";
mail($to, $subject, $body, $headers);
echo $to . " sent <br>";

$i++;
}

Seeing as your code didn’t provide the values of either $body or $greeting I put some dummy variables for them at the top of the script for the purpose of illustration

Okay, so we start with the first user you want to send an e-mail to. We generate the greeting, “Hello John Doe”, and set $body to be the concatenation of “Hello John Doe”, "
" and $body, so $body then is:


Hello John Doe,
This is the content of the e-mail

So far so good. Now we go on with the second user, “Jane Doe”. We create the greeting “Hello Jane Doe”, and we set $body to the concatenation of that greeting, "
", and $body. But that is the same $body we just changed to include the greeting to John Doe.
So now we get,


Hello Jane Doe,
Hello John Doe,
This is the content of the e-mail

and this repeated n times (one time for each user in the database) until you get a nice stack of greetings at the top.

Now, with my proposed change:


$greet = "$greeting". mysql_result($result,$i,"fname")." ".mysql_result($result,$i,"lname").", ";
$sendBody = "$greet"."\
". "$body";
$headers = "From: $email";
mail($to, $subject, $sendBody, $headers);

You don’t append to an existing variable ($body), but create a new variable for each e-mail to be send ($sendBody). That way all the variable contains is just the greeting, "
" and contents of the e-mail.

Does that make sense? :slight_smile:

I have spent way more hours than I will admit to trying to resolve this issue. It works!

Could you enlighten me why changing the variable name cured the issue?

Thank you!

The problem is you keep on pre-pending the greeting to $body.

Try to replace this


$body = "$greet"."\
". "$body";
$headers = "From: $email";
mail($to, $subject, $body, $headers);

with this


$sendBody = "$greet"."\
". "$body";
$headers = "From: $email";
mail($to, $subject, $sendBody, $headers);

:slight_smile:

Some times it is the most obvious things that are the hardest to see.

I appreciate you responce, and the time you took to explain the solution.

Thanks, God bless you and your family,

Carl

I need to correct this script to send an email to each record in the database, and only include that persons first and last names in the email.

As the script is now, each email has all names instead of just the one name the email goes to. If there were twenty records in the database, each would have the list of all names.

Thanks for any help you may be able to offer.

I missed the loop in your code.

If you put your posted code in code tags (use the # icon in the editor’s toolbar) it will be a lot easier to read - at least for these aging eyes :weyes:

at the moment your script sends only 1 email.

You need to get the list of names from the database and then loop through them 1 by 1 and send an email with just the current names in the list.

Depending on how many names are in your list, I’m not sure php mail() is the most efficient way of doing it.

What is your question, exactly? Can you clarify?