PHPMailer AddAddress()

40

我不知道AddAddress PHPMailer函数需要什么格式的数据;我想要将电子邮件发送给多个收件人,因此尝试了以下代码:

$to = "me@example.com,you@example.net,she@example.it";
$obj->AddAddress($to);

但是没有成功。


1
PHPMailer现在(2015年5月)可以通过解析函数处理这种地址字符串。请参见此答案。 - Synchro
5个回答

80

您需要为每个要发送电子邮件的地址调用一次AddAddress函数。此函数只有两个参数:recipient_email_addressrecipient_name。收件人名称是可选的,如果不存在,则不会使用。

$mailer->AddAddress('recipient1@example.com', 'First Name');
$mailer->AddAddress('recipient2@example.com', 'Second Name');
$mailer->AddAddress('recipient3@example.com', 'Third Name');

您可以使用数组来存储收件人,然后使用 for 循环。


2
虽然在第一个 AddAddress() 后使用 AddCC() 添加其他地址更有意义。 - doub1ejack
6
不,那不一样。 - edditor
1
我认为它的新版本是$mailer->addAddress('recipient1@domain.com', 'Name');。 - Yann Chabot
"recipient_name"是用来做什么的?添加姓名的目的是什么? - undefined

14
您需要为每个收件人调用一次AddAddress方法。就像这样:
$mail->AddAddress('person1@example.com', 'Person One');
$mail->AddAddress('person2@example.com', 'Person Two');
// ..

为了让事情变得简单,您应该循环遍历数组来完成此操作。

$recipients = array(
   'person1@example.com' => 'Person One',
   'person2@example.com' => 'Person Two',
   // ..
);
foreach($recipients as $email => $name)
{
   $mail->AddAddress($email, $name);
}

更好的是,将他们作为抄送收件人添加。

$mail->AddCC('person1@example.com', 'Person One');
$mail->AddCC('person2@example.com', 'Person Two');
// ..
为了简化操作,你应该通过循环数组来实现这一点。
$recipients = array(
   'person1@example.com' => 'Person One',
   'person2@example.com' => 'Person Two',
   // ..
);
foreach($recipients as $email => $name)
{
   $mail->AddCC($email, $name);
}

1
不得不说,你的简化版本看起来不必要地更长了。 - Marcel
2
这是Alan Orozco在此处的答案副本:https://dev59.com/zXA75IYBdhLWcg3ws7Yi - redburn
@redburn 是的,我们不能将链接作为答案提供,所以想在这里添加。 - Mahendra Jella

6

以上有一些很好的答案,结合这些信息,以下是我今天解决同样问题所做的:

$to_array = explode(',', $to);
foreach($to_array as $address)
{
    $mail->addAddress($address, 'Web Enquiry');
}

4
foreach ($all_address as $aa) {
    $mail->AddAddress($aa); 
}

假设$all_address是一个数组。 - Stelian

0
所有答案都很好。以下是一个多个添加地址的示例用例: 使用 Web 表单可以按需添加任意数量的电子邮件:

在此处使用 jsfiddle 查看实际操作 (除了 PHP 处理器)

### Send unlimited email with a web form
# Form for continuously adding e-mails:
<button type="button" onclick="emailNext();">Click to Add Another Email.</button>
<div id="addEmail"></div>
<button type="submit">Send All Emails</button>
# Script function:
<script>
function emailNext() {
    var nextEmail, inside_where;
    nextEmail = document.createElement('input');
    nextEmail.type = 'text';
    nextEmail.name = 'emails[]';
    nextEmail.className = 'class_for_styling';
    nextEmail.style.display = 'block';
    nextEmail.placeholder  = 'Enter E-mail Here';
    inside_where = document.getElementById('addEmail');
    inside_where.appendChild(nextEmail);
    return false;
}
</script>
# PHP Data Processor:
<?php
// ...
// Add the rest of your $mailer here...
if ($_POST[emails]){
    foreach ($_POST[emails] AS $postEmail){
        if ($postEmail){$mailer->AddAddress($postEmail);}
    }
} 
?>

基本上它所做的就是在每次点击时生成一个名为“emails[]”的新输入文本框。
末尾添加的[]使其在发布时成为一个数组。
然后我们通过PHP端的“foreach”遍历数组的每个元素,添加:
    $mailer->AddAddress($postEmail);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接