Page 1 of 1
multiple emails...mail() function
Posted: Fri Dec 22, 2006 3:45 pm
by paul_20k
Hi
I am trying to use mail() function for multiple emails. Its giving me errors like shown below:
Code: Select all
1. If I use
$to=$f_email, $f_email_new;
mail($to,$subject,$body_message,$headers);
it gives me parse error like
Parse error: parse error in c:\program files\........php on line ....
2. If I use
Warning: mail(): SMTP server response: 554 User unknown in c:\program files\.....php on line
I am using Argosoft Mail Server (Free). I tried all options but its giving me error all the time. I can send one email successfully but as soon as I use multiple emails,I get errors.
Is it the limitation of Argosoft Mail server free veriosn?? Its just for testing purpose.
Thanks
Posted: Fri Dec 22, 2006 3:59 pm
by Christopher
I believe that you can only have one email address in the $to parameter. You need to build headers for additional email addresses. The PHP manual for email() shows you how. Or you could use the fantastic Swiftmailer package -- which I believe has passed sliced bread in greatest-thingy-ness.
Posted: Fri Dec 22, 2006 4:11 pm
by paul_20k
hi
thanks for your quick reply. Can you please explain little more about it? if you can give me a small example, that would be really appreciated. As a newbie, I am trying to explore mail() function.
Thanks
Posted: Fri Dec 22, 2006 4:22 pm
by paul_20k
hi
I found the idea. its working fine now..thanks so much
Posted: Fri Dec 22, 2006 4:35 pm
by John Cartwright
paul_20k wrote:hi
I found the idea. its working fine now..thanks so much
psst.. it is forum etiquette to post your solution so anyone with similar problems can benefit

Posted: Fri Dec 22, 2006 4:53 pm
by paul_20k
hi Jcart
I just used the number of mail functions as many times as I have email addresses and it worked fine although its not the best solution but its working for me.
Thanks
Posted: Fri Dec 22, 2006 5:11 pm
by richmix
You can cheat a little. Set $to equal to an array.
Code: Select all
<?php
$to = array($f_email, $f_email_new);
foreach ($to as $address) {
mail($address, $subject, $body_message, $headers);
}
?>
Posted: Fri Dec 22, 2006 5:19 pm
by John Cartwright
If your looking for a better solution, by all means we will help you explore it. It just may take a little extra effort on your part. Although I definantly would suggest it.
Cough cought
http://swiftmailer.org
Posted: Fri Dec 22, 2006 6:38 pm
by paul_20k
thanks alot richmix and Jcart.
Posted: Fri Dec 22, 2006 9:46 pm
by richmix
Also look into the explode() function. Very useful.
$mailList = '
mail1@gmail.com,
mail2@gmail.com,
mail3@gmail.com ...' as str
$mailArray = explode(', ', $mailList) as array
Explode will automatically split a string into an array for you. Very useful for what you're trying to do. I use it myself in my own mail script.
Posted: Sat Dec 23, 2006 3:15 am
by arukomp
You don't need to add e-mails into array. Just put those e-mails in quotes and separate them by commas, just like this:
Code: Select all
$to = "$f_email, $f_email_new";
mail($to,$subject,$body_message,$headers);
It works for me and it should work for you too

Posted: Sun Dec 24, 2006 3:25 am
by wildwobby
arukomp wrote:You don't need to add e-mails into array. Just put those e-mails in quotes and separate them by commas, just like this:
Code: Select all
$to = "$f_email, $f_email_new";
mail($to,$subject,$body_message,$headers);
It works for me and it should work for you too

yeah but then wouldn't it show the 2 recipients eachothers email adresses?
Posted: Sun Dec 24, 2006 6:19 am
by Kieran Huggins
Code: Select all
// I assume you would have an array of recipients like:
$recipients = array($f_email, $f_email_new);
// make headers for BCC - this is the part you care about
$headers .= 'Bcc: '.implode(', ',$recipients)."\r\n";
// you have to set something as the "to"
$to = 'me@my.com';
// set the other variables
$subject = "My poodle";
$body_message = 'bark bark bark!';
// send the mail
mail($to,$subject,$body_message,$headers);
Cheers,
Kieran