简体   繁体   English

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

[英]Valid PCRE8 Library for PHPMailer Version 5.2.16 and PHP version 5.6.19

i have a problem with my address validator in PHPMailer . 我在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 . 我的PHP版本是5.6.19,PHPMailer的版本是5.2.16,所以基本上选择的库是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: 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 ! 谢谢 !

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. 从理论上讲,您不能完全使用正则表达式来验证电子邮件地址(正如该著名问题所表明的那样),尽管这主要是由于试图满足RFC822的更复杂(且在此情况下最不相关)的要求,而不是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). 这就是为什么,例如,PHP filter_var函数的FILTER_VALIDATE_EMAIL标志使用一个(与PHPMailer中的pcre8模式由同一作者)。

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. 我怀疑您遇到了一个长期存在的PHPMailer错误 ,该错误与PHP中的PCRE有关-但是它是不一致的,即使每个人具有相同的PHP和PCRE版本,也不会影响每个人,因此尚未解决。 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. pcre8模式使用仅在PCRE的更高版本中可用的某些功能,而较旧的,精度较低的pcre模式不使用这些功能,也不会遇到相同的问题。 You can tell PHPMailer to use that pattern for its internal validations by setting this class property: 您可以通过设置以下类属性来告诉PHPMailer使用该模式进行内部验证:

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 : 如果您使用setFrom()而不是设置FromFromName则会收到此问题的通知:

$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. 其次,您的代码在PHPMailer 5.2.16上应该失败-您没有使用自动加载器,也没有加载SMTP类,因此它将无法找到该类并且不会为您加载它。 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. 我还是建议使用作曲家。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM