在 phpmailer 批量邮件发送期间收到无效邮件错误

Getting invalid mail error during phpmailer bulk mail sending

php mailer 是群发邮件,但如果记录中的邮件地址不正确,它将停止发送邮件。我怎样才能跳过那个地址并继续。

我 RETURN 我的数据库中的所有代码都在这里用 FOREACH 列出 当我在我的两封不同电子邮件的数组上尝试我的代码时,我没有遇到问题

<?php

    $mail = new PHPMailer(true);
    try {
        //Server Ayarları
        $mail->SMTPDebug = SMTP::DEBUG_SERVER;
        $mail->Host="x";
        $mail->SMTPAuth= true;
        $mail->Username="x@x";
        $mail->isSMTP();
        $mail->Password="x;
        $mail->CharSet="utf8";
        $mail->Port=587;
        //Alıcı Ayarları
        $mail->setFrom('x', 'x');
        $gonulluler=$database->select($_SESSION["type"],"eposta");
        foreach ($gonulluler as $gonullu){
            $mail->addBCC($gonullu);
        }

        //Gönderi ayarları
        $mail->isHTML(true);
        $mail->Subject=$baslik;
        $mail->Body=$icerik;
        if($mail->send()){
            $alert=array(
                "message"=>"Mail Başarılı Bir Şekilde Gönderildi",
                "type" => "success"
            );
        }else{
            $alert=array(
                "message"=>"Mail Gönderilirken Bir Hata İle Karşılaşıldı",
                "type" => "danger"
            );
        }

    }catch (Exception $e){
        $alert=array(
                "message"=>$e->getMessage(),
                "type" => "danger"
            );
    }
        

您可以在使用 $mail->addBCC() 之前检查电子邮件地址是否有效,以及邮件服务器是否会响应。这个函数可以做到这两点:

function isValidEmailAddress($email, $performDomainCheck = FALSE)
// validate an email address, TRUE if the email address has the email address format and the domain exists
{
   $atSignIndex = strrpos($email,'@');
   // no @ sign
   if (is_bool($atSignIndex) && !$atSignIndex) return FALSE;
   // take parts
   $domain    = substr($email,$atSignIndex+1);
   $name      = substr($email,0,$atSignIndex);
   $domainLen = strlen($domain);
   $nameLen   = strlen($name);
   // name part length exceeded
   if ($nameLen < 1 || $nameLen > 64) return FALSE;
   // domain part length exceeded
   if ($domainLen < 1 || $domainLen > 255) return FALSE;
   // local part starts or ends with '.'
   if ($name[0] == '.' || $name[$nameLen-1] == '.') return FALSE;
   // local part has two consecutive dots
   if (preg_match('/\.\./', $name)) return FALSE;
   // character not valid in domain part
   if (!preg_match('/^[A-Za-z0-9\-\.]+$/', $domain)) return FALSE;
   // domain part has two consecutive dots
   if (preg_match('/\.\./', $domain)) return FALSE;
   // character not valid in local part unless local part is quoted
   if (!preg_match('/^(\\.|[A-Za-z0-9!#%&`_=\/$\'*+?^{}|~.-])+$/',str_replace("\\","",$name)))
   {
     if (!preg_match('/^"(\\"|[^"])+"$/',str_replace("\\","",$name))) return FALSE;
   }
   // domain not found in DNS?
   if ($performDomainCheck)
   {
//     $domain = idn_to_ascii($domain);   function not known yet (php 5.3 on 24-10-2015)
     if (!(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) return FALSE;
   }
   // it's oke
   return TRUE;
}

我没有更新这个函数的样式。它并不完美,但使用类似的东西会使您的群发邮件程序工作得更好。您的代码看起来像这样:

    foreach ($gonulluler as $gonullu){
        if (isValidEmailAddress($gonullu, TRUE) {
            $mail->addBCC($gonullu);
        }
    }

您可以添加一个 else 部分来通知您自己电子邮件地址无效。