我正在使用PHPMailer发送电子邮件,我用它来添加要发送到的电子邮件地址:
$email->AddAddress($result["emailto"]);
我的电子邮件地址来自数据库,它工作正常,但如果我的数据库中的emailto列如下:
email1@domain.com,email2@domain.com
我收到错误消息您必须提供至少一个收件人电子邮件地址。
如何绕过这个能够发送到多个地址?
答案 0 :(得分:6)
您应该将其展开然后添加电子邮件。
$addresses = explode(',', $result["emailto"]);
foreach ($addresses as $address) {
$email->AddAddress($address);
}
答案 1 :(得分:2)
//Explode by comma so that we get an array of emails.
$emailsExploded = explode(",", $result["emailto"]);
//If the array isn't empty, loop through
if(!empty($emailsExploded)){
foreach($emailsExploded as $emailAddress){
$email->AddAddress(trim($emailAddress));
}
} else{
//This should not be the case.
throw new Exception('No emails found!');
}
答案 2 :(得分:1)
你可以对角色进行爆炸,然后通过数组做一个foreach来添加地址。
$addresses = explode(',',$result["emailto"]);
foreach ( $addresses as $address ){
$email->AddAddress($address);
}
答案 3 :(得分:0)
问题是您必须多次拨打AddAddress
或使用;而不是a,
尝试这样做:
$email_array = explode(',', $result['emailto']);
for($i = 0; $i < count($email_array); $i++) {
$email->AddAddress($email_array[$i]);
}
答案 4 :(得分:0)
您可以尝试使用数组,如下所示:
$array = explode(",",$result["emailto"]);
$nb = count($array);
for ($i=0;$i<$nb;$i++) {
$email->AddAddress($array[$i]);
}
答案 5 :(得分:0)
只需添加foreach (explode($result["emailto"], ',') as $email) {$email->AddAddress($email);}