使用配置 PHP 文件和 PHP 邮件程序将电子邮件发送到多个地址
Sending emails to multiple addresses using config PHP file and PHP Mailer
我设置了一个 config.php 文件,以便我的客户可以轻松地为他们网站上的在线表格添加电子邮件、主题和其他发送信息。
它看起来像这样:
<?php
$config = [
"host" => "xxx",
"username" => "xxx",
"password" => "xxx",
"secure" => "ssl", // ssl or tls
"port" => 465,
"sendTo" => "abc@email.com",
"sendToBCC" => "xyz@email.com",
"from" => "no-reply@email.com",
"fromName" => "Contact Form"
];
我现在面临的挑战是发送到多个电子邮件。我试过 "sendTo" => "abc@email.com, efg@email.com, hik@email.com",
但它抛出了无效的电子邮件错误。
在发送文件中,我们有以下代码:
//Recipients
$mail->setFrom($config['from'], $config['fromName']);
$mail->addAddress($config['sendTo']);
$mail->addCC($config['sendToCC']);
$mail->addBCC($config['sendToBCC']);
$mail->addAddress($_POST['email']);
所以我猜 $mail->addAddress($config['sendTo']);
行在选择多封电子邮件时遇到了问题。我们如何编辑此代码以允许多个收件人?非常希望确保我们的客户可以轻松地在 config.php 文件而不是发送文件中添加电子邮件。
基于@Chris Haas 分享的 link 的 examples from PHPMailer 礼貌。我做了这些更改:
<?php
$config = [
"host" => "xxx",
"username" => "xxx",
"password" => "xxx",
"secure" => "ssl", // ssl or tls
"port" => 465,
"sendTo" => "abc@email.com",
"sendTo2" => "efg@email.com",
"sendTo3" => "hik@email.com",
"sendToBCC" => "xyz@email.com",
"from" => "no-reply@email.com",
"fromName" => "Contact Form"
];
在发送文件中我这样做了:
//Recipients
$mail->setFrom($config['from'], $config['fromName']);
$mail->addAddress($config['sendTo']);
$mail->addAddress($config['sendTo2']);
$mail->addAddress($config['sendTo3']);
$mail->addCC($config['sendToCC']);
$mail->addBCC($config['sendToBCC']);
$mail->addAddress($_POST['email']);
问题已解决。
您可以支持这两种情况:
$addrs = preg_split('#[\s;,]+#', $config['sendTo'], -1, PREG_SPLIT_NO_EMPTY);
foreach ($addrs as $addr) {
$mail->addAddress($addr);
}
这会将一个"joe@mail1.com; jack@mail2.com, jill@mail3.com jane@mail4.com"
拆分成四个地址,并将它们全部相加。单个地址将浪费几个周期来转换为单元素数组,但随后也能正常工作。
您可能希望也通过filter_var
过滤$addrs
,使用array_unique,如果count($addrs)是零,表示没有可用的有效地址:
function filterMail($emails) {
$emails = array_map('trim', $emails);
$emails = array_filter($emails);
$emails = array_unique($emails);
$emails = array_map(
function ($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL);
},
$emails
);
$emails = array_filter($emails);
$emails = array_values($emails);
return $emails;
}
我设置了一个 config.php 文件,以便我的客户可以轻松地为他们网站上的在线表格添加电子邮件、主题和其他发送信息。 它看起来像这样:
<?php
$config = [
"host" => "xxx",
"username" => "xxx",
"password" => "xxx",
"secure" => "ssl", // ssl or tls
"port" => 465,
"sendTo" => "abc@email.com",
"sendToBCC" => "xyz@email.com",
"from" => "no-reply@email.com",
"fromName" => "Contact Form"
];
我现在面临的挑战是发送到多个电子邮件。我试过 "sendTo" => "abc@email.com, efg@email.com, hik@email.com",
但它抛出了无效的电子邮件错误。
在发送文件中,我们有以下代码:
//Recipients
$mail->setFrom($config['from'], $config['fromName']);
$mail->addAddress($config['sendTo']);
$mail->addCC($config['sendToCC']);
$mail->addBCC($config['sendToBCC']);
$mail->addAddress($_POST['email']);
所以我猜 $mail->addAddress($config['sendTo']);
行在选择多封电子邮件时遇到了问题。我们如何编辑此代码以允许多个收件人?非常希望确保我们的客户可以轻松地在 config.php 文件而不是发送文件中添加电子邮件。
基于@Chris Haas 分享的 link 的 examples from PHPMailer 礼貌。我做了这些更改:
<?php
$config = [
"host" => "xxx",
"username" => "xxx",
"password" => "xxx",
"secure" => "ssl", // ssl or tls
"port" => 465,
"sendTo" => "abc@email.com",
"sendTo2" => "efg@email.com",
"sendTo3" => "hik@email.com",
"sendToBCC" => "xyz@email.com",
"from" => "no-reply@email.com",
"fromName" => "Contact Form"
];
在发送文件中我这样做了:
//Recipients
$mail->setFrom($config['from'], $config['fromName']);
$mail->addAddress($config['sendTo']);
$mail->addAddress($config['sendTo2']);
$mail->addAddress($config['sendTo3']);
$mail->addCC($config['sendToCC']);
$mail->addBCC($config['sendToBCC']);
$mail->addAddress($_POST['email']);
问题已解决。
您可以支持这两种情况:
$addrs = preg_split('#[\s;,]+#', $config['sendTo'], -1, PREG_SPLIT_NO_EMPTY);
foreach ($addrs as $addr) {
$mail->addAddress($addr);
}
这会将一个"joe@mail1.com; jack@mail2.com, jill@mail3.com jane@mail4.com"
拆分成四个地址,并将它们全部相加。单个地址将浪费几个周期来转换为单元素数组,但随后也能正常工作。
您可能希望也通过filter_var
过滤$addrs
,使用array_unique,如果count($addrs)是零,表示没有可用的有效地址:
function filterMail($emails) {
$emails = array_map('trim', $emails);
$emails = array_filter($emails);
$emails = array_unique($emails);
$emails = array_map(
function ($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL);
},
$emails
);
$emails = array_filter($emails);
$emails = array_values($emails);
return $emails;
}