How to Solve SMTP: Could Not Authenticate using Gmail + PHPMaile
- 时间:2020-09-13 14:33:25
- 分类:网络文摘
- 阅读:91 次
Sending a mail through PHPMailer is often scripted via PHP if you intend to send email notifications when new comments are recieved on your wordpress blog, or when the server is overloaded with high CPU spikes (via uptime showing load-averages in the last 1, 5 and 15 minutes).
If you are using Gmail, most likely is that the Google GMail will report back with a error: Could Not Authenticate even you have typed in the correct password for your Gmail account.
Putting your account password directly somewhere in your script is not a good idea, and the correct way to solve this problem and avoid leaking your main account password is to use the App Password. Alternatively, you can customize your security settings for less-secured applications – which it may not work as it is just a PHP script here.
App Password can be set separately for each individual application and you can also re-generate one if one is compromised.
You would need to visit Google Security Dashboard: https://myaccount.google.com/security

google-app-passwords-security
Then, add a App Password (Select GMail, and Others – give the App a Name) – which can be used in the PHPMailer – in case this password is leaked, you can always delete it and regenerate a new one.

google-app-passwords
Then, the following PHPMailer sample code should be used to do the email testing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #!/usr/bin/php <?php use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; require 'PHPMailer/src/Exception.php'; require 'PHPMailer/src/PHPMailer.php'; require 'PHPMailer/src/SMTP.php'; $host = "smtp.gmail.com"; $port = 587; $secure = "tls"; // or the following configurations through SSL should work as well. // $port = 465; // $secure = "ssl"; $username = "Your GMAIL email"; $password = "Your GMAIL App Password"; try { $mailer = new PHPMailer(true); $mailer->IsHTML(true); $mailer->IsSMTP(); $mailer->From = $username; $mailer->FromName = $username; $mailer->ClearAllRecipients(); $mailer->AddAddress("Recipent Email Address", "Recipent"); $mailer->Subject = "Subject "; $mailer->Body = "Hello, time is: ". date("Y-m-d h:i:s"); $mailer->SMTPAuth = true; // enable SMTP authentication $mailer->SMTPSecure = $secure; // sets the prefix to the servier $mailer->Host = $host; // sets GMAIL as the SMTP server $mailer->Port = $port; // set the SMTP port for the GMAIL server $mailer->Username = $username; // GMAIL username $mailer->Password = $password; // GMAIL password $result = $mailer->Send(); echo "Mail sent\n"; } catch (Exception $e) { echo 'Message could not be sent. Mailer Error: '; var_dump($e); } |
#!/usr/bin/php <?php use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; require 'PHPMailer/src/Exception.php'; require 'PHPMailer/src/PHPMailer.php'; require 'PHPMailer/src/SMTP.php'; $host = "smtp.gmail.com"; $port = 587; $secure = "tls"; // or the following configurations through SSL should work as well. // $port = 465; // $secure = "ssl"; $username = "Your GMAIL email"; $password = "Your GMAIL App Password"; try { $mailer = new PHPMailer(true); $mailer->IsHTML(true); $mailer->IsSMTP(); $mailer->From = $username; $mailer->FromName = $username; $mailer->ClearAllRecipients(); $mailer->AddAddress("Recipent Email Address", "Recipent"); $mailer->Subject = "Subject "; $mailer->Body = "Hello, time is: ". date("Y-m-d h:i:s"); $mailer->SMTPAuth = true; // enable SMTP authentication $mailer->SMTPSecure = $secure; // sets the prefix to the servier $mailer->Host = $host; // sets GMAIL as the SMTP server $mailer->Port = $port; // set the SMTP port for the GMAIL server $mailer->Username = $username; // GMAIL username $mailer->Password = $password; // GMAIL password $result = $mailer->Send(); echo "Mail sent\n"; } catch (Exception $e) { echo 'Message could not be sent. Mailer Error: '; var_dump($e); }
Remember, you would also need to update the Email settings in WordPress Plugin – SMTP. And make sure you have the following credentials for Gmail updated in the wp-settings.php
1 2 | define( 'WPMS_ON', true ); define( 'WPMS_SMTP_PASS', 'You GMAIL App Password' ); |
define( 'WPMS_ON', true ); define( 'WPMS_SMTP_PASS', 'You GMAIL App Password' );
–EOF (The Ultimate Computing & Technology Blog) —
推荐阅读:六年级作文:我和秋天有个约会 数学故事:流传久远的算术趣题 趣味数学:什么是完全数 数学小故事:高斯巧解算术题 数学趣味故事:测量金字塔的高度 湖南卫视在线直播-湖南卫视直播在线观看「高清」 东方卫视直播-东方卫视在线直播观看「高清」 江苏卫视直播-江苏卫视在线直播观看「高清」 浙江卫视直播-浙江卫视在线直播观看「高清」 河南卫视在线直播-河南卫视直播在线观看「高清」
- 评论列表
-
- 添加评论