我不知道应该如何为 AddAddress PHPMailer 函数格式化数据;我需要将电子邮件发送给多个收件人,所以我尝试了
$to = "me@domain.com,you@domain.net,she@domain.it";
$obj->AddAddress($to);
但没有成功。任何帮助将不胜感激。
我不知道应该如何为 AddAddress PHPMailer 函数格式化数据;我需要将电子邮件发送给多个收件人,所以我尝试了
$to = "me@domain.com,you@domain.net,she@domain.it";
$obj->AddAddress($to);
但没有成功。任何帮助将不胜感激。
您需要为AddAddress
要发送到的每个电子邮件地址调用一次该函数。这个函数只有两个参数:recipient_email_address
和recipient_name
。收件人姓名是可选的,如果不存在则不会使用。
$mailer->AddAddress('recipient1@domain.com', 'First Name');
$mailer->AddAddress('recipient2@domain.com', 'Second Name');
$mailer->AddAddress('recipient3@domain.com', 'Third Name');
您可以使用数组来存储收件人,然后使用for
循环。我希望它有所帮助。
您需要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);
}
更好的是,将它们添加为抄送收件人。
$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);
}
上面的一些很好的答案,在这里使用该信息是我今天为解决相同问题所做的:
$to_array = explode(',', $to);
foreach($to_array as $address)
{
$mail->addAddress($address, 'Web Enquiry');
}
foreach ($all_address as $aa) {
$mail->AddAddress($aa);
}
所有的答案都很棒。以下是多个添加地址的示例用例:使用 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);