Categories
Tag Cloud
Setup PHPmailer to work with google workspace email
- 3
-
Tired of reading? Press play to listen instead. N/B. Might not work on some articles
Ready

To set up PHPMailer to work with Google Workspace (Gmail), you need to do the following steps:-
Step One: Install the PHPMailer library
Installing the PHPMaier library varies on the hosting platform and the Framework being used, it can be as simple as copying and extracting your folder in cPanel the requiring the path in your sending-mail.php file or using a composer.
Step Two: Configure your Google Workspace account to allow less secure app access
This is achieved only by:-
First Enabling 2-step verification in your G Suite account
Secondly generating an App Password to use with your script.
Step Three: Set the appropriate SMTP details
SMTP Details include host, port, username, and password within your PHPMailer code
N/B For the email to work ensure you use your Google Workspace email address and app password for authentication; you can also consider using OAuth2 authentication for enhanced security.
Simple Working Example Using cPanel
<?php
require("PHPMailer-master/src/PHPMailer.php"); //location of your PHPMailer folder and file
require("PHPMailer-master/src/SMTP.php"); //location of your PHPMailer folder and file
require("PHPMailer-master/src/Exception.php"); //location of your PHPMailer folder and file
$mail = new PHPMailer\PHPMailer\PHPMailer();
try {
//Server settings
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'smtp.gmail.com'; // Set the SMTP server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'your_email@gmail.com'; // SMTP username
$mail->Password = 'your_password'; // SMTP password generated from https://myaccount.google.com/apppasswords
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->SMTPOptions=array('ssl'=>array(
'verify_peer'=>false,
'verify_peer_name'=>false,
'allow_self_signed'=>false
));
//Recipients
$mail->setFrom('your_email@gmail.com', 'Mailer');
$mail->addAddress('recipient_email@example.com', 'Recipient Name'); // Add a recipient
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
Credits: ProgrammingwithVishal
I hope this helps, hit the thumbs-up button to help someone else. God bless you in Christ Jesus