in ,

How To Send Email Using PHP Via Gmail SMTP?

Send email using php

In this lab, we will try to send Email using PHP via the Gmail SMTP server, we will use the PHPmailer class to achieve this lab.

So we will learn how to:

  • Install PHP.
  • Install PHP Composer.
  • Install PHPmailer Using Composer.
  • Set Up Gmail Authentication.
  • Code Out Our Simple PHP Script To Send Email.

Install PHP On Ubuntu Linux 

To install PHP and the required packages, we need to run the following installation command.

  • First: Update Our system
# sudo apt update
# sudo apt upgrade
  • Second: Install The PHP And Required Packages
# sudo apt install php php-common php-mbstring php-zip php-net-socket php-gd php-bcmath php-json php-curl php-intl php-soap php-xml php-xmlrpc unzip wget git curl sendmail

Install PHP On Centos Stream

If you are on Centos Stream, we can install the PHP and required packages as the following.

  • First: Update Our System
# sudo dnf update
  • Second: Install The PHP And Required Packages
# sudo dnf install php php-common php-mbstring php-zip php-gd php-bcmath php-json php-curl php-intl php-soap php-xml php-openssl unzip wget git curl sendmail

Install PHP On Windows

On Windows, we need to move forward as a professional, so we will use the Chocolatey Windows Package Manager To install PHP, Composer, PHPMailer, and all required packages on Windows.
You can follow the step-by-step guide: A Simple Way To Installing PHP And Run Script Files On Windows.

Install PHP Composer On Linux

To install PHP Composer we can run the following simple 3 commands to download and install it.

# curl -sS https://getcomposer.org/installer | php
# chmod +x composer.phar
# mv composer.phar /usr/local/bin/composer

Install PHP Composer On Windows

And On Windows, we can move forward to install The PHP Composer following the same presented guide in the previous step.

Install PHPMailer Using Composer

Now we need to install PHPMailer class, we can get the source code from the PHPMailer GitHub page.

And to install PHPMailer Using Composer we need to run the following command on Linux or in Windows Powershell/CMD terminal, as we make preparing for our windows before.

# composer require phpmailer/phpmailer

Set Up Gmail Authentication

The Gmail SMTP information needed is:

Server: smtp.gmail.com 
Requires SSL: Yes
Requires TLS: Yes (if available)
Requires Authentication: Yes 
Port for SSL: 465 
Port for TLS/STARTTLS: 587

Gmail/Gsuite considers our PHP script code as an application, so we need to set up an application password to successfully connect our Gmail with PHP Code.

Creating Google Application Password

1- Go to your Google account panel

Go to My Account Panel, Then select the Security tab, from the menu, as shown in the below screenshot, then get into the App passwords settings.

Google Setup Gmail Application Password

2- Create Your App Password

We have to select Other (Custom name), Then name our application, and press Generate to get our password.

Google Gmail Application Password

Google Gmail Application Password

Google Setup Gmail Application Password

Code Out Our PHP Script To Send Email Via Gmail SMTP

The PHP Script can be like the following, of course, you need to replace email information with yours

<?php
// Use the PHPMailer Class
use PHPMailer\PHPMailer\PHPMailer;

// Use SMTP
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

// Load Composer's autoloader
require 'vendor/autoload.php';

// Sender and Email information
$senderEmail='sender.email@gmail.com';
$senderName='Sender Name';
$replyTo='replyto.email@gmail.com';
$subJect='RE : please reply with the result';
$messageLetter='plese reply with the result';

// Target Email address
$emailTarget='target.email@domain.xyz';

// Buiding the email object attributes
$mail = new PHPMailer;

// SMTP Configuration

$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(true); //Send using SMTP
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'sender.email@gmail.com'; // SMTP username
$mail->Password = 'ciawupvmezekittr'; // The Google Application password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; //Enable implicit TLS encryption
$mail->Port = 587; //TCP port to connect to; 
$mail->SMTPKeepAlive = true;

// End Of SMTP Configuration


$mail->setFrom($senderEmail,$senderName);
$mail->addReplyTo($replyTo);
$mail->addAddress($emailTarget);
$mail->Subject = trim($subJect);
$mail->Body = $messageLetter;

// The Html Message Type, so message body can contain html tags.
$mail->IsHTML(true);

// Clean Body
$mail->AltBody =strip_tags($messageLetter);
$mail->CharSet = 'UTF-8';
$mail->Encoding = '8bit';

// Fire your Email.
if (!$mail->send()) {
echo '[Error:]'.htmlspecialchars($mail->ErrorInfo);
}
else {
echo '[Status:] Sent';
}

?>

Fire Our Code And Send Email Using PHP Via Gmail SMTP

To fire our PHP code and send the email message, we can run the following CLI command

# php -f sendemail.php

What do you think?

13 Points
Upvote Downvote

Leave a Reply

Your email address will not be published. Required fields are marked *

Domain Authority

Simple 5 Ways to Increase Your Domain Authority For 2022

python pygame course

Free Python PyGame Video Tutorials for Beginners