douao1858 2016-07-27 15:09
浏览 151

适用于PHPMailer版本5.2.16和PHP版本5.6.19的有效PCRE8库

i have a problem with my address validator in PHPMailer . can someone help me with a valid one ? my PHP version is 5.6.19 and PHPMailer's version is 5.2.16 so basically the library selected is pcre8 . puny Encode :

return (boolean)preg_match(
                '/^(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?(?1)){255,})(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?(?1)){65,}@)' .
                '((?>(?>(?>((?>(?>(?>\x0D\x0A)?[\t ])+|(?>[\t ]*\x0D\x0A)?[\t ]+)?)(\((?>(?2)' .
                '(?>[\x01-\x08\x0B\x0C\x0E-\'*-\[\]-\x7F]|\\\[\x00-\x7F]|(?3)))*(?2)\)))+(?2))|(?2))?)' .
                '([!#-\'*+\/-9=?^-~-]+|"(?>(?2)(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\x7F]))*' .
                '(?2)")(?>(?1)\.(?1)(?4))*(?1)@(?!(?1)[a-z0-9-]{64,})(?1)(?>([a-z0-9](?>[a-z0-9-]*[a-z0-9])?)' .
                '(?>(?1)\.(?!(?1)[a-z0-9-]{64,})(?1)(?5)){0,126}|\[(?:(?>IPv6:(?>([a-f0-9]{1,4})(?>:(?6)){7}' .
                '|(?!(?:.*[a-f0-9][:\]]){8,})((?6)(?>:(?6)){0,6})?::(?7)?))|(?>(?>IPv6:(?>(?6)(?>:(?6)){5}:' .
                '|(?!(?:.*[a-f0-9]:){6,})(?8)?::(?>((?6)(?>:(?6)){0,4}):)?))?(25[0-5]|2[0-4][0-9]|1[0-9]{2}' .
                '|[1-9]?[0-9])(?>\.(?9)){3}))\])(?1)$/isD',
                $address
            );

send.php:

<?php
 ini_set('display_errors', true);
  error_reporting(E_ALL);
  require_once('class.phpmailer.php');
   $to=isset($_POST['verify'])?$_POST['verify']:false;
    $subject="Email verification";
   $message='<p>Welcome to Our service this is an email verification procedure, Please click <a href="#">here</a> to go back.';

//$to= "whoto@otherdomain.com";
   $mail = new PHPMailer();
   $mail->isSMTP(); // telling the class to use SMTP

// SMTP Configuration
$mail->SMTPSecure='ssl';
$mail->SMTPAuth = true;                  // enable SMTP authentication
$mail->Host = "smtp.gmail.com "; // SMTP server
$mail->Username = "mymail@gmail.com";
$mail->Password = "mypassword";            
$mail->Port = 465; // optional if you don't want to use the default 

 $mail->From = "<example@host.com>";
 $mail->FromName = "Admin";
 $mail->Subject = $subject;

 //$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
  $mail->isHTML(true);
   $mail->Body=$message;
   $mail->msgHTML($message);


   $mail->addAddress($to);
  if(!$mail->Send())
  { 
    $response = "Message error!".$mail->ErrorInfo;
echo $response;
  // echo $to;
}


  else {
$response = "Message sent!";
    echo $response;
}


?>

Thanks !

  • 写回答

1条回答 默认 最新

  • duangou1868 2016-07-27 16:20
    关注

    It's certainly true that in theory you can't validate email addresses exactly using regular expressions (as that famous question demonstrates), though that's mainly due to trying to accommodate the more complex (and mostly irrelevant in this context) requirements of RFC822, rather than the more practical and much simpler requirements of RFC821. In practice however, it works sufficiently well to be worthwhile. This is why, for example, the PHP filter_var function's FILTER_VALIDATE_EMAIL flag uses one (by the same author as the pcre8 pattern in PHPMailer).

    I suspect you're running into a long-standing PHPMailer bug that's something do with PCRE in PHP - but it's inconsistent and doesn't affect everyone even when they have the same PHP and PCRE versions, so it's not been solved. The pcre8 pattern uses some features only available in later versions of PCRE, and the older, less-accurate pcre pattern does not use those features, and does not run into the same problem. You can tell PHPMailer to use that pattern for its internal validations by setting this class property:

    PHPMailer::$validator = 'pcre';
    

    Alternatively you can inject your own validator function by setting that same class property to a callable, for example this will make it consider all addresses valid:

    PHPMailer::$validator = function($email) { return true; };
    

    Update: It always helps to see your code! I see two problems:

    $mail->From = "<example@host.com>";
    

    That is not a valid From address, and is probably the cause of your error. You would get notification of this problem if you used setFrom() instead of setting From and FromName:

    $mail->setFrom('example@host.com', 'Admin');
    

    Secondly, your code should fail on PHPMailer 5.2.16 - you're not using the autoloader and not loading the SMTP class, so it will be unable to find the class and won't load it for you. It may be that your code is failing before it gets as far as trying to send, so you're not seeing that problem. I recommend using composer anyway.

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法