PHPMailer 无效地址

PHPMailer Invalid address

我想用 PHPMailer 发送邮件。我使用此代码,但出现此错误:

Invalid address: example@gmail.com

(我为 Whosebug 使用假地址。我在真实代码中使用真实地址。)

<?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);
?>

因为已经有一个 SMTP 服务器 运行,我不需要配置它,PHP 功能 mail 正在工作。

我该如何解决这个错误?

嗯。 Actually, there is a problem with the regular expression which is checking if the mail address is valid when the pattern selected is pcre8.我改成了

/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i

现在可以使用了。

感谢@Synchro。