Reputation: 145
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 [email protected],[email protected],[email protected],
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.
Upvotes: 1
Views: 22290
Reputation: 418
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]);
}
Upvotes: 0
Reputation: 111829
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) );
}
Upvotes: 12
Reputation: 7250
This has been asked before, see here: PHP mailer multiple address
snippet:
You need to call the AddAddress
method once for every recipient.
Upvotes: 0