0

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 !

Jean Ouédraogo
  • 234
  • 2
  • 6
  • 16
  • That is quite the regex. Have you looked at some of the other threads on validating emails with PHP? http://stackoverflow.com/questions/12026842/how-to-validate-an-email-address-in-php – chris85 Jul 27 '16 at 15:13
  • This isn't a general question about validating email addresses with regexes; it's about a validation mechanism within PHPMailer. – Synchro Jul 27 '16 at 16:46
  • @Synchro how is this diffrent? PHPMailer seems to use a regex for the validation, and the question is literally asking for a valid regex expression for this validation. – oɔɯǝɹ Jul 27 '16 at 19:22
  • Possible duplicate of [Using a regular expression to validate an email address](http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address) – oɔɯǝɹ Jul 27 '16 at 19:23
  • It's not a question about the regex, it's about PHPMailer internals. The address he's using validates correctly with that pattern - i.e. the regex is not the problem. If you think it's about the pattern, you are not reading the question correctly, nor my answer - he's still seeing the same problem if the validator simply returns `true`, i.e it's nothing to do with the validation pattern. In this case I think he assumed that it was the pattern that was at fault, but it's not, so this is not a question about regex patterns at all. – Synchro Jul 27 '16 at 19:29
  • His later update showed that the address he was trying to validate was not valid after all, so even when bypassing the validator, it failed when he tried to use that address - notice the `puny Encode` reference in the question, which is where the error arises in PHPMailer. So, still not a regex question, but more an "I don't know what email addresses look like" question. – Synchro Jul 27 '16 at 19:42

1 Answers1

0

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.

Synchro
  • 35,538
  • 15
  • 81
  • 104
  • I tried it it still does not work,i have tried all the patterns cases but didn't work , i have changed the validation function to filter_var but it throws an error `you must at list provide one recipient`. – Jean Ouédraogo Jul 27 '16 at 16:26
  • In that case the problem isn't with the validation patterns, but elsewhere, probably to do with the puny code handling. Please can you post the rest of your code in your question? – Synchro Jul 27 '16 at 16:45
  • Your sending code - I already know what the PHPMailer code looks like. – Synchro Jul 27 '16 at 17:20
  • it's might be the latter i wrote example@host.com just as an example. I'm using a valid Email Thanks – Jean Ouédraogo Jul 31 '16 at 05:35