multiple emails...mail() function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
paul_20k
Forum Commoner
Posts: 45
Joined: Fri Nov 10, 2006 7:02 pm

multiple emails...mail() function

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
paul_20k
Forum Commoner
Posts: 45
Joined: Fri Nov 10, 2006 7:02 pm

Post 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
paul_20k
Forum Commoner
Posts: 45
Joined: Fri Nov 10, 2006 7:02 pm

Post by paul_20k »

hi

I found the idea. its working fine now..thanks so much
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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 :wink:
paul_20k
Forum Commoner
Posts: 45
Joined: Fri Nov 10, 2006 7:02 pm

Post 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
richmix
Forum Commoner
Posts: 31
Joined: Fri Dec 22, 2006 5:21 am

Post 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);
  }
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
paul_20k
Forum Commoner
Posts: 45
Joined: Fri Nov 10, 2006 7:02 pm

Post by paul_20k »

thanks alot richmix and Jcart.
richmix
Forum Commoner
Posts: 31
Joined: Fri Dec 22, 2006 5:21 am

Post 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.
arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

Post 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 ;)
wildwobby
Forum Commoner
Posts: 66
Joined: Sat Jul 01, 2006 8:35 pm

Post 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?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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
Post Reply