How to Solve SMTP: Could Not Authenticate using Gmail + PHPMaile

  • 时间:2020-09-13 14:33:25
  • 分类:网络文摘
  • 阅读:103 次

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 How to Solve SMTP: Could Not Authenticate using Gmail + PHPMailer? email smtp gmail php

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 How to Solve SMTP: Could Not Authenticate using Gmail + PHPMailer? email smtp gmail php

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) —

推荐阅读:
Marketing Tips for E-Commerce Websites  5 Tips for Staying Safe Online  10 Signs Your Blog is Working  The Key to Risk-Free Cloud-Based IT Equipment Shipping  Bring Your Multi-Site Development Work To The Next Level With A   
How to Create Killer SEO Content on a Budget  Learn to Achieve Your Goals with 5 Secrets of Great Bloggers  What is Cornerstone Content And Why It’s Important  How to Attract Female Readership For Your Blog  Double your traffic with White Hat SEO techniques 
评论列表
添加评论