如何使用phpmailer将电子邮件发送到多个地址

时间:2014-05-09 11:26:31

标签: php phpmailer

我正在尝试使用php邮件程序向多个电子邮件地址发送电子邮件,但它无法正常工作。我试过爆炸地址,但似乎没有任何作用。这是我的代码

HTML:

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

输入中的当前输出是test1 @ gmail.com,test2 @ gmail.com,test3 @ gmail.com,

php发送电子邮件:

/* 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();

所以,如果我在输入框中有一个收件人,它会发送电子邮件,但如果我有多个并将它们分开,则不会发送电子邮件。我试过$ emailto = explode(',',$ email_addresses);因为我正在分离电子邮件,但这不起作用。任何建议都会很棒。

3 个答案:

答案 0 :(得分:11)

假设您在$ email_addresses变量中拥有良好的数据。

您需要更改行:

$email->AddAddress( $emailto );

成:

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

foreach ($addr as $ad) {
    $email->AddAddress( trim($ad) );       
}

答案 1 :(得分:0)

之前已经提到过,请看这里:PHP mailer multiple address

摘录:

您需要为每个收件人调用一次AddAddress方法。

答案 2 :(得分:0)

1- explode将返回一个电子邮件地址数组,例如$ emailto

2-循环此数组并使用AddAddress方法:

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