Forum Moderators: coopster

Message Too Old, No Replies

PHP code for emailing to multiple recipients

         

magneto

1:01 am on Sep 9, 2010 (gmt 0)

10+ Year Member



Hello guys, i got stuck trying to email to multiple recipients. seems that every solution i try either sends no email or to the last email only. how should i structure it. i only want to send to like 3 or 4 emails. One of course is going to be the one provided from a MySQL database, and the others will never change (ei: myemail@hotmail.com, etc) heres my code:

$recipient = $row['Email'];
$subject = "Results";
mail($recipient, $subject, $emailtxt); //mail command :)?>


The emailtxt is for data stored in MySQL and being sent back to the user.

Anyango

8:47 am on Sep 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



the ones that will never change put them in bcc field and recipient like same. Add as many bcc as you want, just properly formatted

Matthew1980

9:10 am on Sep 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

Another solution would be to have all of the email address' put into an array, then using a for each loop you could iterate through each instance of the array and invoke the mail function that way:-

$Address = array();

$Address[] = "one@mailaddress.com";
$Address[] = "two@mailaddress.com";
$Address[] = "three@mailaddress.com";

$Subject = "Sending an email";
$Message = "Hi there, this email has been sent to you\n\r";
$Message .= "because you are a valued member!\n\r";
$mailheaders = "MIME-version: 1.0\r\n";
$mailheaders .= "content-type: text/plain; charset=UTF-8\r\n";
$mailheaders .= "From: Your Company name here <you@yoursite.com>\r\n"

foreach($Address AS $Add=>$ToEmail){
mail($ToEmail, $Subject, $Message, $mailheaders);
}


Though if you just want to use the mail function once you can just place a comma between address' for php/mail to send to them all.

Hope that makes sense anyway.

Cheers,
MRb

CyBerAliEn

11:57 pm on Sep 9, 2010 (gmt 0)

10+ Year Member



Personagoogleample), you might find that your emails may be halted/terminated, queued, delayed, or throttled. (why? consider SPAM)

If you will be emailing LARGE quantities of email or emailing VERY frequently, consider consulting your system administrator(s) to ensure your server/etc will not have any issues.

CyBerAliEn

12:00 am on Sep 10, 2010 (gmt 0)

10+ Year Member



I give up... my post was longer than that, and it messed it up. I tried editing my post to fix it, and it is still dumping the majority of my post! -angry-

magneto

2:30 am on Sep 10, 2010 (gmt 0)

10+ Year Member



thank you both for answering. i guess what i was trying to say is that none of those options had worked for me. i had tried putting this
$recipient = $row['Email'],"myemail@hotmail.com"; //recipient
$subject = "Results"; //subject
mail($recipient, $subject, $emailtxt); //mail command :)?>

or this
$recipient = "$row['Email'], myemail@hotmail.com"; //recipient
$subject = "Results"; //subject
mail($recipient, $subject, $emailtxt); //mail command :)?>

and other similar variations but none worked.
Am i writing it incorrectly? How could i modify just that one line, or add another recipient below the first one like this (by the way, i tried the query below and it didnt work either)
$recipient = $row['Email']; //recipient
$recipient = "myemail@hotmail.com"; //recipient
$subject = "Results"; //subject
mail($recipient, $subject, $emailtxt); //mail command :)?>

this one only shows the last email (myemail@hotmail.com)
thanks again

rocknbil

5:28 am on Sep 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




$recipient = $row['Email'],"myemail@hotmail.com"; //recipient
$subject = "Results"; //subject
mail($recipient, $subject, $emailtxt); //mail command happy!?>


That one should work, but the comma s needed as part of the string. Try

$email = $row['Email'];
$recipient = "$email,myemail@hotmail.com";


$recipient = "$row['Email'], myemail@hotmail.com";


The array reference won't work this way, see above or try

$recipient = "$row{'Email'}, myemail@hotmail.com";

(might not work)

$subject = "Results"; //subject


$recipient = $row['Email']; //recipient
$recipient = "myemail@hotmail.com"; //recipient

This one doesn't work because the script executes linear, top to bottom, second $recipient replaces the first.

Best of all, since all of those will reveal all addresses to all recipients, is build a header with a BCC. No one can see the other emails in the BCC.

$headers = "From: " . $row['Email'] ."\r\n";
$headers .= "BCC:" . "address1@example.com,address2@example.com,address3@example.com\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to,$subject,$body,$headers);

magneto

5:09 pm on Sep 10, 2010 (gmt 0)

10+ Year Member



thanks rocknbill:
this worked great.

$email = $row['Email'];
$recipient = "$email,myemail@hotmail.com";


adding the $headers below made it work even better. Appreciate it.

$headers = "From: " . $row['Email'] ."\r\n";
$headers .= "BCC:" . "address1@example.com,address2@example.com,address3@example.com\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to,$subject,$body,$headers);

penders

11:54 am on Sep 11, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



the ones that will never change put them in BCC field...


Just to add... I had problems recently with a (popular) shared host that prevented email being BCC'd to any email address that wasn't of the same domain the email was being sent from (over zealous security I guess) - just resulted in an error and the email was not sent:

Warning: mail(): SMTP server response: 554 <someone@anotherdomain.co.uk>: Recipient address rejected: Relay access denied

rocknbil

4:24 pm on Sep 12, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Did you test that against a normal email like outlook or even web based email? That's really odd!

If you get stuck in that situation, use the looping method. It's a little more intense as it requires multiple calls to mail(), but unless you're sending out thousands it shouldn't be too taxing.

In fact, you should use that method anyway if you hope to customize the email, an gives a "prettier" to header anyway:

$emails = Array (
'Bill' => 'bill@example.com',
'Bob' => 'bob@example.com',
'Mary' => 'mary@example.com'
);


foreach ($emails as $name=>$email) {
$msg = "
<p>Dear $name,</p>
<p>Here is your message.</p>
";

$headers = "From: \"$name\" <$email>\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to,$subject,$body,$headers);
}


So coming into their mailbox it looks like

To: "Bill" <bill@example.com>

It's the little things. :-)