php PHPMailer AddAddress()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1770765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:47:24  来源:igfitidea点击:

PHPMailer AddAddress()

phpformattingphpmailer

提问by kmunky

I don't know how the data should be formatted for AddAddress PHPMailer function; I need the email to be sent to multiple recipients so I tried

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

$to = "[email protected],[email protected],[email protected]";
$obj->AddAddress($to);

but with no success. Any help will be appreciated.

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

回答by doamnaT

You need to call the AddAddressfunction once for each E-Mail address you want to send to. There are only two arguments for this function: recipient_email_addressand recipient_name. The recipient name is optional and will not be used if not present.

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

$mailer->AddAddress('[email protected]', 'First Name');
$mailer->AddAddress('[email protected]', 'Second Name');
$mailer->AddAddress('[email protected]', 'Third Name');

You could use an array to store the recipients and then use a forloop. I hope it helps.

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

回答by Lead Developer

You need to call the AddAddressmethod once for every recipient. Like so:

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

$mail->AddAddress('[email protected]', 'Person One');
$mail->AddAddress('[email protected]', 'Person Two');
// ..

To make things easy, you should loop through an array to do this.

为方便起见,您应该遍历一个数组来执行此操作。

$recipients = array(
   '[email protected]' => 'Person One',
   '[email protected]' => 'Person Two',
   // ..
);
foreach($recipients as $email => $name)
{
   $mail->AddAddress($email, $name);
}

Better yet, add them as Carbon Copy recipients.

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

$mail->AddCC('[email protected]', 'Person One');
$mail->AddCC('[email protected]', 'Person Two');
// ..

To make things easy, you should loop through an array to do this.

为方便起见,您应该遍历一个数组来执行此操作。

$recipients = array(
   '[email protected]' => 'Person One',
   '[email protected]' => 'Person Two',
   // ..
);
foreach($recipients as $email => $name)
{
   $mail->AddCC($email, $name);
}

回答by Purple Tentacle

Some great answers above, using that info here is what I did today to solve the same issue:

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

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

回答by user2720626

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

回答by Tarik

All answers are great. Here is an example use case for multiple add address: The ability to add as many email you want on demand with a web form:

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

See it in action with jsfiddle here(except the php processor)

在此处使用 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);}
    }
} 
?>

So what it does basically is to generate a new input text box on every click with the name "emails[]".

所以它所做的基本上是在每次点击时生成一个名为“emails[]”的新输入文本框。

The [] added at the end makes it an array when posted.

最后添加的 [] 使其在发布时成为一个数组。

Then we go through each element of the array with "foreach" on PHP side adding the:

然后我们在 PHP 端使用“foreach”遍历数组的每个元素,添加:

    $mailer->AddAddress($postEmail);