2022年6月29日星期三

PHPMailer gmail smtp

1.set app password at Google

 https://support.google.com/accounts/answer/185833?hl=zh-Hant


2. Go to My Account/App Passwords

Point your web browser to https://myaccount.google.com/apppasswords. Even though you just logged in to your account, you'll most likely be prompted to type your account password again.

3. Create your first app password

At the bottom of the page, you should see two drop-downs, one titled "Select app" and the other "Select device." Click the "Select app" drop-down and choose Other. You will then be prompted to name the app password. I would suggest naming the app password for the app or service you'll use it for. After naming the app password, click GENERATE.

The Gmail App Password generator.

Generating an app password for Gmail is quite simple.

Screenshot by Jack Wallen/ZDNET

4. Copy your new app password

After you click GENERATE, a new pop-up window will appear giving your new app password. Copy that password so you can use it for the third-party app or service you want to connect to Gmail. Once you have the password copied, click DONE.

It's very important to understand that these app passwords can only be viewed once. After clicking DONE, you won't be able to view the password again, which is why it's important to copy the password and use it immediately. I would suggest not clicking DONE until you know the app password works for the app or service you want to connect to your Gmail.

Also: The best LastPass password manager alternatives

If you disconnect an app or service from Gmail, you should also delete the app password by clicking the associated trash can icon in the App Passwords listing. It's always better to be safe than sorry.

And that's all there is to creating a Gmail app password. If you don't already have 2FA enabled for Gmail, I would highly recommend you do so. 


2.go to Gmail Settings, allow IMAP

3.

composer require phpmailer/phpmailer


use PHPMailer\PHPMailer\PHPMailer;

use PHPMailer\PHPMailer\Exception;


/*


require __DIR__.'/..' . '/vendor' . '/phpmailer/phpmailer/src/Exception.php';

require __DIR__.'/..' . '/vendor' . '/phpmailer/phpmailer/src/PHPMailer.php';

require __DIR__.'/..' . '/vendor' . '/phpmailer/phpmailer/src/SMTP.php';

*/


require base_path() . '/vendor' . '/phpmailer/phpmailer/src/Exception.php';

require base_path() . '/vendor' . '/phpmailer/phpmailer/src/PHPMailer.php';

require base_path() . '/vendor' . '/phpmailer/phpmailer/src/SMTP.php';


---------------------------------------------------------------------------------------------------------

    $mail = new PHPMailer();

    $mail->IsSMTP();

    $mail->Mailer = "smtp";


    $mail->SMTPOptions = [

        'ssl' => [

            'verify_peer' => false,

            'verify_peer_name' => false,

            'allow_self_signed' => true

        ]

    ];


    $mail->SMTPDebug  = 1;

    $mail->SMTPAuth   = TRUE;

    //$mail->SMTPSecure = "ssl";
    //$mail->Port       = 465;

    $mail->SMTPSecure = "tls";

    $mail->Port       = 587;

    $mail->Host       = "smtp.gmail.com";

    $mail->Username   = "bennywong0515@gmail.com";

    $mail->Password   = "vzaumcivkrvupkhs";


    $mail->IsHTML(true);



    $mail->AddAddress("bennywong0515@gmail.com", "recipient-name");



    $mail->SetFrom("bennywong0515@gmail.com", "from-name");

    //$mail->AddReplyTo("reply-to-email@domain", "reply-to-name");

    $mail->AddCC("news5641@gmail.com", "cc-recipient-name");

    $mail->Subject = "Test is Test Email sent via Gmail SMTP Server using PHP Mailer";

    $content = "<b>This is a Test Email sent via Gmail SMTP Server using PHP mailer class.</b>";


    $mail->MsgHTML($content);

    if (!$mail->Send()) {

        return "Error while sending Email.";

        //var_dump($mail);

    } else {

        return "Email sent successfully";

    }

---------------------------------------------------------------------------------------------------------

refernce:

https://netcorecloud.com/tutorials/send-an-email-via-gmail-smtp-server-using-php/

Ok I fixed it. What I did was first making sure to do all the steps which are written in this blog

1.http://help.warmupinbox.com/en/articles/4934806-configure-for-google-workplace-with-two-factor-authentication-2fa .

2.Then make sure that you redeploy the functions.





沒有留言:

發佈留言

[Fixed] some windows chrome, scrolling has glitch position not accurate

   [Fixed] some windows chrome, scrolling has glitch position not accurate Smooth Scrolling Flag 📜 Chrome has an experimental "smooth ...