Question

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.

Was it helpful?

Solution

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) );       
}

OTHER TIPS

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

snippet:

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

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]);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top