PHPMailer Invalid address

寵の児 提交于 2019-12-10 15:56:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!