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
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.
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) —
loading...
Last Post: The Reduce Function in Python
Next Post: Find Numbers with Even Number of Digits using the Reduce Function in Javascript, C++ and Python
Less secure apps and app pw are no longer available from google
So far the best example on how to get PHPMailer to work with Google email. However make sure that extension=openssl is enabled in the PHP.ini file for PHP 7.4,12.