Send email to multiple recipients
I'm building a website where people (ie Mr. Smith) fills out a questionaire (not shown) and uses Mr. Smith's "zip code" and to find (3) people/Reps (ie.. Bob, Chuck and Sally) that are already 'Rep Members' and have on a previous occasion, opted to have all the questionaire's that's from Mr. Smith's zip code emailed to them (Bob, Chuck and Sally) to respond to.
So below I've pulled Mr. Smith's zip code "LifeZip" and email address "LifeEmail" from the questionaire form from the previous page and using Mr. Smith's zip code "LifeZip" to find (3) people/Reps (who happen to be Bob, Chuck and Sally) that are already stored in separate table "repstable" that MATCH Mr. Smith's Zip Code so they can respond to Mr. Smith's questionaire.
I'm having trouble placing multiple emails (ie.. Bob, Chuck and Sally) into the "to:" field.
Whether the script sends the same email to the different people separately or they're ALL listed in the "to:" field, I'll take either. Thanks in advance!
<?
session_start();
session_register('LifeZip'); // Zip Code of Mr. Smith from questionaire on previous page
session_register('LifeEmail'); // Email of Mr. Smith from questionaire on previous page
// connect to db
$conn = db_connect();
if (!$conn)
return 'Could not connect to database server - please try later.';
// convert Mr. Smith's LifeZip and LifeEmail from previous page into variable
echo 'use the variables below to find customer the just filled out forms from previous pages (initial info received from page session)';
$CustomerEmailMatch = $_SESSION['LifeEmail'];
$CustomerZipMatch = $_SESSION['LifeZip'];
//Use LifeZip/$CustomerZipMatch from above to 'match' the zip codes in 'repstable' which finds (3) emails, in this case, (Bob, Chuck and Sally) and grabs their emails
$result = mysql_query("SELECT * FROM repstable WHERE RepZipCode = $CustomerZipMatch GROUP BY RepID HAVING COUNT(1) <= 3") or die(mysql_error());
// Below is making sure that I'm pulling Bob, Chuch and Sally's email and their table ID (RepId)
while($row = mysql_fetch_array($result)) {
echo '<br />';
echo "Rep's email Address: ";
echo '<font color="#FF7600">',$row['RepEmail'], '</font>';
echo '<br />';
echo "Rep's ID: ";
echo '<font color="#FF7600">',$row['RepId'], '</font>';
echo '<hr />';
}
// NOW BELOW IS WHERE I'M HAVING THE ISSUE.
// I want to send myself as well as Bob, Chuck and Sally to receive Mr. Smith's "hello" email message. Later I'll insert Mr. Smith's questionaire form information later.
// insert 'RepEmail' which contains multiple emails into '$to:' field below and send email
$to = "My static webmaster email";
$to = "???? RepEmail ????"; // Needs to have Bob, Chuck and Sally's em开发者_如何学Cail here.
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "webmaster @ send this to me.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
Thanks in Advance for this, I've been beating myself up for over a week on this one. If someone knows how to send out an email multiple times to different people where only (1) email is listed in the 'to' field, that'd be great.
Easiest way would be to build up an array of email addresses and then loop through them sending an individual mail to each one:
$recipients = array('youremail@example.com', 'repemail@example.com', 'bob@example.com');
foreach ($recipients as $to) {
mail($to,$subject,$message,$headers);
}
(Note: this is not a good way to send bulk email to 100s of people, but will be fine in your case with just a few addresses.)
If you were happy to have multiple emails in the To field, you just need to comma separate them:
$to = 'youremail@example.com, repemail@example.com, bobemail@example.com';
mail($to,$subject,$message,$headers);
Edit: You can build up your array of recipients in the while loop, so something like:
$recipients = array();
while($row = mysql_fetch_array($result)) {
$recipients[] = $row['RepEmail'];
echo '<br />';
echo "Rep's email Address: ";
echo '<font color="#FF7600">',$row['RepEmail'], '</font>';
echo '<br />';
echo "Rep's ID: ";
echo '<font color="#FF7600">',$row['RepId'], '</font>';
echo '<hr />';
}
then add your static email address to this array:
$recipients[] = 'youremail@example.com';
and then send them as you were before, but this time doing the actual mail part in a loop, for each recipient:
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "webmaster @ send this to me.com";
$headers = "From:" . $from;
foreach ($recipients as $to) {
mail($to,$subject,$message,$headers);
}
精彩评论