PHPMailer AddAddress()

时间:2009-11-20 14:26:46

标签: php formatting phpmailer

我不知道如何为AddAddress PHPMailer函数格式化数据;我需要将电子邮件发送给多个收件人,所以我尝试了

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

但没有成功。任何帮助将不胜感激。

5 个答案:

答案 0 :(得分:66)

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

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

您可以使用数组存储收件人,然后使用for循环。我希望它有所帮助。

答案 1 :(得分:10)

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

$mail->AddAddress('person1@domain.com', 'Person One');
$mail->AddAddress('person2@domain.com', 'Person Two');
// ..

为了简单起见,你应该循环一个数组来做到这一点。

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

更好的是,将它们添加为Carbon Copy收件人。

$mail->AddCC('person1@domain.com', 'Person One');
$mail->AddCC('person2@domain.com', 'Person Two');
// ..

为了简单起见,你应该循环一个数组来做到这一点。

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

答案 2 :(得分:4)

上面的一些很好的答案,在这里使用这些信息是我今天为解决同样的问题所做的:

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

答案 3 :(得分:3)

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

答案 4 :(得分:0)

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

See it in action with jsfiddle here (除了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);