Jump to content

Multiple addresses in Email sent with PHP - Help please


mike77

Recommended Posts

Hey everyone, thanks for any help in advance.

 

I have a piece of code that I could use some help on.

 

Here is what I am trying to accomplish.  I have a database with so many users, and I'd like to have them opt in to receive emails when messages are posted on the site.  If yes, then I would like to add their email address (which is stored in the database) to an email that is generated with PHP.  I am having trouble figuring out how I add all these emails.  Each email address needs to be incluced before the email is sent.

 

$mail->AddAddress($toaddress);  - Needs each email address. You could just reprint this line with each email address, but not sure how to make dynamic.

 

The $toaddress is the email address which will change record from record.  The part I am trying to figure out is how to store each one before the email is sent out.  To add additional email addresses, you just add another line of code like one above.

 

Here is the current code I have to search the db for just 1 user.  I need to make this pull all email addresses and users then send out the email.  Hope that all makes sense.

 

 

require("c:\php\includes\class.phpmailer.php");

 

$mail = new PHPMailer();

 

$query="SELECT email_address FROM registration WHERE receiveEmail = 'yes' ";

$result=mysql_query($query);

$r = mysql_fetch_array($result, MYSQL_ASSOC);

 

$email = $r['email_address'];   //assigns email address

 

 

$toaddress = $email;  //this needs to be added for each user

 

 

$mailhost = "mail.mywebsite.com"; 

$fromaddress = "[email protected]"; 

$frompwd = "mypassword"; 

$subject = "New Post";

 

 

$fromname = "My Website Name";

$body = $_POST["Message"] ;

$first = $_POST["FirstName"] ;

$last = $_POST["LastName"] ;

$rplyto = "no reply";

$msgbody = $first . " "  . $last . " - "  . $body;

 

 

$mail->IsSMTP();

$mail->Host = $mailhost;

$mail->SMTPAuth = true;

$mail->Username = $fromaddress;

$mail->Password = $frompwd;

 

$mail->From = $fromaddress;

$mail->FromName = $fromname;

$mail->AddReplyTo($rplyto);

$mail->AddAddress($toaddress);  //Need multiple email address here. If I were to manually do this, I could just add the same line with another email.

$mail->IsHTML(true);

$mail->Subject = $subject;

$mail->Body = $msgbody;

 

if(!$mail->Send())

{

   echo "Message could not be sent. <p>";

   echo "Mailer Error: " . $mail->ErrorInfo;

   exit;

}

 

echo "Thank you, your message has been sent!";

 

If none of that made sense, I'll try to make it short and sweet....

 

I need this line "$mail->AddAddress($toaddress);" reprinted for as many email addresses that the query above pulls with the $toaddress to change.   I need all of these stored for when the email sends.

 

It would need to look like this when done..

 

$mail->AddAddress([email protected]);

$mail->AddAddress([email protected]);

$mail->AddAddress([email protected]);

$mail->AddAddress([email protected]);

$mail->AddAddress([email protected]);

 

getting the following:

 

Warning: Invalid argument supplied for foreach() in C:\Sites\Single33\topsfootball\webroot\H2H\rumorpost.php on line 31

 

I tried this...

 

$query="SELECT * FROM registration WHERE 'receiveemail' = 'yes' ";

$result=mysql_query($query);

$r = mysql_fetch_array($result, MYSQL_ASSOC);

 

 

require("c:\php\includes\class.phpmailer.php");

 

$mail = new PHPMailer();

 

foreach($r['email_address'] AS $toaddress)

{

$mail->AddAddress($toaddress);

}

 

$mailhost = "mail.mywebsite.com"; 

$fromaddress = "[email protected]"; 

$frompwd = "mypassword"; 

$subject = "New Post";

 

 

$fromname = "My Website Name";

$body = $_POST["Message"] ;

$first = $_POST["FirstName"] ;

$last = $_POST["LastName"] ;

$rplyto = "no reply";

$msgbody = $first . " "  . $last . " - "  . $body;

 

 

$mail->IsSMTP();

$mail->Host = $mailhost;

$mail->SMTPAuth = true;

$mail->Username = $fromaddress;

$mail->Password = $frompwd;

 

$mail->From = $fromaddress;

$mail->FromName = $fromname;

$mail->AddReplyTo($rplyto);

 

$mail->IsHTML(true);

$mail->Subject = $subject;

$mail->Body = $msgbody;

 

if(!$mail->Send())

{

  echo "Message could not be sent. <p>";

  echo "Mailer Error: " . $mail->ErrorInfo;

  exit;

}

 

echo "Thank you, your message has been sent!";

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.