1

I am trying to send emails to multiple email addresses using php mailer but its not working. I've tried explode the addresses but nothing seems to work. here is my code

html:

<input type="text" name="addresses" value="{$addresses}"/>

current output in the input is test1@gmail.com,test2@gmail.com,test3@gmail.com,

php to send email:

/* Get Customer info*/
$sql = mysql_query("SELECT * FROM customer WHERE ID='$id' LIMIT 1");
$sql=mysql_fetch_array($sql);
$fname=$sql['FIRST_NAME'];
$lname=$sql['LAST_NAME'];
$company=$sql['COMPANY'];
$customer_email=$sql['EMAIL'];

$email_addresses=$VAR['addresses'];

if($email_addresses != "" && $customer_email !=""){    
$emailto=$email_addresses;            
}elseif($email_addresses == "" && $customer_email !=""){
$emailto=$customer_email; 
} 

/* Get Email Options */
$r=mysql_query("SELECT * FROM `email` WHERE `ID`=1");
$r=mysql_fetch_array($r);
$emailfrom=$r['EMAIL_FROM'];
$emailpriority=$r['EMAIL_PRIORITY'];
$emailsubject=$r['EMAIL_SUBJECT'];

/* Headers */
$subject = "$emailsubject";
$mailer = "$emailfrom";
$headers = "From: $mailer \r\n";
$headers .= "Reply-To: $mailer\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n boundary=\"PHP-mixed-    ".$random_hash."\"";
$headers .= "Importance: $emailpriority\r\n";

$email = new PHPMailer();
$email->From      = $mailer;
$email->FromName  = $mailer;
$email->Subject   = $subject;
$email->Body      = $message; 
$email->AddAddress( $emailto );
$email->isHTML(true);

$email->Send();

so if I have one recipient in the input box it send the email fine but if I have multiple and separate them with , it does not send the email. i've tried $emailto=explode(',',$email_addresses); since I am separating the email with , but that does not work. any suggestion would be great.

stackMonk
  • 1,033
  • 17
  • 33
user3620142
  • 135
  • 2
  • 3
  • 13

3 Answers3

12

Assume you have good data in $email_addresses variable.

You need to change line:

$email->AddAddress( $emailto );

into:

$addr = explode(',',$email_addresses);

foreach ($addr as $ad) {
    $email->AddAddress( trim($ad) );       
}
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • hi marcin, the data in html is in proper format. test1@gmail.com, test2@gmail.com, I know the above will send separate emails, how can I send them all together? I would like everyone to see who I am sending to – user3620142 May 09 '14 at 11:35
  • They will see. Only one email will be send – Marcin Nabiałek May 09 '14 at 11:37
  • ok so this works but I think you are missing part of it, so what happens if the if statement above passes to $emailto=$customer_email; – user3620142 May 09 '14 at 11:44
  • ok I've replaced $email_addresses with $emailto and that fixed the issue. thanks so much for your help – user3620142 May 09 '14 at 11:48
0

This has been asked before, see here: PHP mailer multiple address

snippet:

You need to call the AddAddress method once for every recipient.

Community
  • 1
  • 1
Jorg
  • 7,219
  • 3
  • 44
  • 65
0

1- explode will return an array of email address say $emailto

2- loop this array and use AddAddress method:

foreach($emailto as $address){
$email->AddAddress($address,[optional:: name]);
}
laurent
  • 418
  • 3
  • 7