在Cc和Bcc中找出被拒绝的地址也 - 如何?

时间:2015-12-11 07:33:39

标签: php swiftmailer

我可以从 - > setTo(array(。使用以下代码:

)中找到swiftmailer中被拒绝的地址
$mailer = Swift_Mailer::newInstance( ... );
$message = Swift_Message::newInstance( ... )
->setFrom( ... )
->setTo(array(
'receiver@bad-domain.org' => 'Receiver Name',
'other@domain.org' => 'A name',
'other-receiver@bad-domain.org' => 'Other Name'
))
->setBody( ... )
;

// Pass a variable name to the send() method
if (!$mailer->send($message, $failures))
{
echo "Failures:";
print_r($failures);
}

/*
Failures:
Array (
0 => receiver@bad-domain.org,
1 => other-receiver@bad-domain.org
)
*/

现在我想找出来自Cc和Bcc字段的拒绝地址。我该如何添加类似的代码?是否有教程或解决方法? swiftmailer教程中没有示例。

1 个答案:

答案 0 :(得分:0)

您可以使用 if (impClass.simpleName().contentEquals("OnionSoup")){ OnionSoup.getInstance(); } private void buildMap(){ soups = new HashMap<>(); //(soupClassNames map) Code here that gets the Soup implementation //class names by iterating class names in the package. for (String impClass : soupClassNames.values()){ Class soupClass = Class.forName(impClass); soups.add(soupClassName,soupClass); } 块来设置to,cc和bcc电子邮件地址。

来自the manual

  

如果您根据可能的数据源自动添加收件人   包含无效的电子邮件地址,可以防止可能的异常   通过使用try-catch和。验证地址   仅添加验证的地址。另一种方法是包装你的   Swift_Validate::email($email)setTo()setCc()调用try-catch块和句柄   阻止块中的setBcc()

因此,您可以在Swift_RfcComplianceException对象上使用try-catch

$message

如果您依赖于处理它们的方式,可以将失败设置为变量。

修改:如果您有一组具有单独名字和姓氏的用户,您可以使用以下名称:

$message = Swift_Message::newInstance();
$emails = array(
    'receiver@bad-domain.org' => 'Receiver Name',
    'other@domain.org' => 'A name',
    'other-receiver@bad-domain.org' => 'Other Name'
);
// loop through emails and set the individually to catch exceptions
foreach($emails as $email => $name)
{
    try {
        $message->setTo(array($email => $name));
    } catch(Swift_RfcComplianceException $e) {
        echo "The email ".$email." seems invalid";
    }
}
// And do the same thing with cc and bcc emails
$ccEmails = array(
    'receiver@ccemail.org' => 'CC Receiver Name'
);
foreach($ccEmails as $email => $name)
{
    try {
        $message->setCc(array($email => $name));
    } catch(Swift_RfcComplianceException $e) {
        echo "The email ".$email." seems invalid";
    }
}