我不知道AddAddress PHPMailer函数需要什么格式的数据;我想要将电子邮件发送给多个收件人,因此尝试了以下代码:
$to = "me@example.com,you@example.net,she@example.it";
$obj->AddAddress($to);
但是没有成功。
我不知道AddAddress PHPMailer函数需要什么格式的数据;我想要将电子邮件发送给多个收件人,因此尝试了以下代码:
$to = "me@example.com,you@example.net,she@example.it";
$obj->AddAddress($to);
但是没有成功。
您需要为每个要发送电子邮件的地址调用一次AddAddress
函数。此函数只有两个参数:recipient_email_address
和recipient_name
。收件人名称是可选的,如果不存在,则不会使用。
$mailer->AddAddress('recipient1@example.com', 'First Name');
$mailer->AddAddress('recipient2@example.com', 'Second Name');
$mailer->AddAddress('recipient3@example.com', 'Third Name');
您可以使用数组来存储收件人,然后使用 for
循环。
AddAddress()
后使用 AddCC()
添加其他地址更有意义。 - doub1ejackAddAddress
方法。就像这样:$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);
}
以上有一些很好的答案,结合这些信息,以下是我今天解决同样问题所做的:
$to_array = explode(',', $to);
foreach($to_array as $address)
{
$mail->addAddress($address, 'Web Enquiry');
}
foreach ($all_address as $aa) {
$mail->AddAddress($aa);
}
在此处使用 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);}
}
}
?>
$mailer->AddAddress($postEmail);