问题
I want to send a mail with PHPMailer. I use this code, but I got this error :
Invalid address: example@gmail.com
(I use fake addresses for StackOverflow. I use a real address in my real code.)
<?php
$from = "My Name";
$mail = "example@gmail.com";
require_once('./class.phpmailer.php');
$bodytext = "
<html>
<head>
<title>title</title>
</head>
<body>
<h1 style='text-align:center'>Some text</h1>
<p>more text. Here's a name : $from</p>
</body>
</html>
";
try {
$email = new PHPMailer(true);
$email->From = 'webmaster@mysite.com';
$email->FromName = 'WebMaster';
$email->isHTML(true);
$email->Subject = 'subject';
$email->Body = $bodytext;
$email->addAddress( $mail, "Name" );
$email->AddReplyTo($mail,"Name");
// $file_to_attach = $filePath;
// $email->AddAttachment( $file_to_attach , 'constat.pdf' );
$email->Send();
} catch (Exception $e) {
echo $e->getMessage();
}
// var_dump($email);
?>
Sice there is already a SMTP server running, I don't have to configure it, and the PHP function mail
is working.
How can I fix this error ?
回答1:
Well. Actually, there is a problem with the regular expression which is checking if the mail address is valid when the pattern selected is pcre8
. I changed it to
/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
And it's working now.
Thanks @Synchro.
来源:https://stackoverflow.com/questions/30639192/phpmailer-invalid-address