Invalid PHPMailer Address

I want to send a letter to PHPMailer. I am using 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 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 already has an SMTP server, I don’t need to configure it, and the PHP function works mail.

How can I fix this error?

+4
source share
1 answer

Well. There is actually a problem with the regex that checks if the mailing address is valid when the selected template pcre8. I changed it to

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

And now it works.

Thanks @Synchro.

+2
source

All Articles