On Wed, 20 Mar 2002, Kris Vose wrote:
> I have a problem with a piece of code that uses the mail function in a
> for loop.  It sends out mail to all the users in the database but it has
> a problem with attaching their specific name into the message.  What
> happens is the first user in the database will get their name in the
> e-mail (Dear John,).  Then the second user in the database will get the
> last users name and their name in the e-mail (Dear John, Dear Mike)
> ...and so on and so forth.
> If ($button != "")
> {
> $t = mysql_query("SELECT * from AddExisting");
>             $number_of_customers = mysql_num_rows($t);
>                         for($i=0; $i<$number_of_customers;$i++)
>                         {
>                         $r = mysql_fetch_array($t);
>                         $email = $r["EMAIL_ADDRESS"];
>                         $cusname = $r["Name"];
>                         $to = $email;
>                         $subject = "hello";
>                         $message = "Dear $cusname,".$message;
>                         $fromaddress = "[EMAIL PROTECTED]";
>                         mail($to, $subject, $message, $fromaddress);
>                         }//end of for
> }//end of if

You don't need a for loop here; you don't actually care specifically how
many customers there are, just that you iterate once for each one. And
you've put a raw email address in the "extra headers" argument to mail()
rather than a properly-formed From: header.

Here's the code you want:

if (!empty(button))
  $t = mysql_query("SELECT * from AddExisting");
  while ($r = mysql_fetch_assoc($t))
    $message = "Dear {$r[name]},\n{$message}";
    mail ($r[email_address], 'hello', $message, 'From: [EMAIL PROTECTED]');


PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to