让swiftmailer忽略php中的坏电子邮件

时间:2013-06-03 17:03:13

标签: php swiftmailer

我最多有5封电子邮件要通过Swift Mailer发送,但我不知道其中有多少会无效。 无效的电子邮件=“”

我想知道是否有办法让swift邮件忽略无效的电子邮件而不是发出致命错误而不发送任何内容。我在网上看了看,他看到他们提到我应该尝试捕捉不好的电子邮件,但我不太明白语法或他们在哪里试图说,无论如何这里是我的代码到目前为止

//发送欢迎信     function send_email($ info){

    //format each email
    $body = format_email($info,'html');
    $body_plain_txt = format_email($info,'txt');

    //setup the mailer
    $transport = Swift_MailTransport::newInstance();
    $mailer = Swift_Mailer::newInstance($transport);
    $message = Swift_Message::newInstance();
    $message ->setSubject('Your Party Information');
    $message ->setFrom(array('noreply@stackoverflow.net' => 'noreply@stackoverflow.net'));
    $message ->setTo(array($info['Email1'],$info['Email2'],$info['Email3'],$info['Email4'],$info['Email5'] => $info['$firstname']));

    $message ->setBody($body_plain_txt);
    $message ->addPart($body, 'text/html');

    $result = $mailer->send($message);

    return $result;

}

$emails1 = $user2['Email'] or " ";
$emails2 = $user2['Email'] or " ";
$emails3 = $user2['Email'] or " ";
$emails4 = $user2['Email'] or " ";
$emails5 = $user2['Email'] or " ";

1 个答案:

答案 0 :(得分:2)

通过filter_var查看电子邮件是否有效,如何直接剥离无效的电子邮件呢?

...

$emails = array();
if (filter_var($info['Email1'], FILTER_VALIDATE_EMAIL)) 
    $emails[] = $info['Email1'];
if (filter_var($info['Email2'], FILTER_VALIDATE_EMAIL)) 
    $emails[] = $info['Email2'];
if (filter_var($info['Email3'], FILTER_VALIDATE_EMAIL)) 
    $emails[] = $info['Email3'];
if (filter_var($info['Email4'], FILTER_VALIDATE_EMAIL)) 
    $emails[] = $info['Email4'];
if (filter_var($info['Email5'], FILTER_VALIDATE_EMAIL)) 
    $emails[$info['Email5']] = $info['$firstname'];

...

$message ->setTo($emails);