1

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

我想知道是否有一种方法可以让快速邮件忽略无效的电子邮件,而不是给出致命错误而不发送任何内容。我在网上看了看,我看到他们提到我应该尝试捕捉不良电子邮件,但我不太了解语法或他们试图说的内容,无论如何这是我的代码

//发送欢迎信函数 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 " ";
4

1 回答 1

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);
于 2013-06-03T17:09:09.083 回答