Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

php - Getting invalid mail error during phpmailer bulk mail sending


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could check if the email address is valid, and if the mailserver will respond, before using $mail->addBCC(). This function can do both:

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;
}

I didn't update the style of this function. It is not perfect, but using something like this will make your mass mailer work a lot better. Your code would look something like this:

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

You could add an else part to notify yourself of the invalid email addresses.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...