Php Mail Using Gmail Smtp Tutorial

Email plays an important role on the website. Every website usually needs to send emails to users. These emails can be sent from contact us page, newsletter, registration form, etc.

PHP provides a
mail()
function which is used to send an email. But there are limitations while using
mail()
method. You can’lengkung langit send email from a local development server. Another drawback is, there is a high possibility of your email ending up into a Spam.


In most cases,
mail()
method even does not send an email. This may be because of the wrong server configuration or something else.

To get out of these problems or limitations, one can use the SMTP server to send the emails.

In this article, we study how to use PHPMailer and Gmail SMTP server for sending emails.

Installation

You first need to install the PHPMailer library in your project. Recommended way to install a library is through Composer.




Open the command prompt in your project root directory and run the below command.

composer require phpmailer/phpmailer

As we are using Gmail SMTP, you need to change some settings on your Google account. Login to your Google account and click on My Account. Once you are on My Account page, click on Security. Scroll down to the bottom and you will find ‘Less secure app access’ settings. Set it to ON.

less-secure-apps

After this, we need to write a code that sends an email using the PHPMailer library and Gmail SMTP server.

PHP Script for Sending Email using Gmail SMTP Peladen

Open your PHP file where you need to write a code for emails. For instance, I am assuming you have a
sendemail.php
file in the root directory.

sendemail.php

<?php //Import PHPMailer classes into the mondial namespace use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception;  require_once 'vendor/autoload.php';  $mail = new PHPMailer(true); ?>
      

In the above code, I have included the environment of the PHPMailer library in the PHP file.




Next, for sending emails using PHPMailer the user needs to pass the Gmail SMTP peladen address, SMTP port for Gmail, and SMTP authentication(which is nothing but your username and password of a Google account).

$mail->isSMTP(); $mail->Host = 'smtp.googlemail.com';  //gmail SMTP server $mail->SMTPAuth = true; $mail->Username = 'GMAIL_USERNAME';   //username $mail->Password = 'GMAIL_PASSWORD';   //password $mail->SMTPSecure = 'ssl'; $mail->Port = 465;                    //SMTP port
      

That’s it! You are done with the configuration. Now, you are good to go ahead with sending an email to a user.

$mail->setFrom('FROM_EMAIL_ADDRESS', 'FROM_NAME'); $mail->addAddress('RECEPIENT_EMAIL_ADDRESS', 'RECEPIENT_NAME');  $mail->isHTML(true);  $mail->Subject = 'Email subject'; $mail->Body    = '<b>Email Body</b>';  $mail->send(); echo 'Message has been sent';
      

Replace the placeholders with actual values. Run this file on a browser and your email should send on the recipient’s email address.

Sending Attachments in an Email

Using the PHPMailer library one can send single or multiple attachments in an email. You all need to do is pass a directory path of your attachments to the method
addAttachment
as follows.

$mail->addAttachment(__DIR__ . '/attachment1.png'); $mail->addAttachment(__DIR__ . '/attachment2.jpg');
      

Our final code is as follows.

sendemail.php

<?php use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception;  require_once "vendor/autoload.php"; require_once "constants.php";  $mail = new PHPMailer(true);  try {     $mail->isSMTP();     $mail->Host = 'smtp.googlemail.com';  //gmail SMTP server     $mail->SMTPAuth = true;     $mail->Username = GMAIL_USERNAME;   //username     $mail->Password = GMAIL_PASSWORD;   //password     $mail->SMTPSecure = 'ssl';     $mail->Port = 465;                    //smtp port       $mail->setFrom('FROM_EMAIL_ADDRESS', 'FROM_NAME');     $mail->addAddress('RECEPIENT_EMAIL_ADDRESS', 'RECEPIENT_NAME');      $mail->addAttachment(__DIR__ . '/attachment1.png');     $mail->addAttachment(__DIR__ . '/attachment2.png');      $mail->isHTML(true);     $mail->Subject = 'Email Subject';     $mail->Body    = '<b>Email Body</b>';      $mail->send();     echo 'Message has been sent'; } catch (Exception $e) {     echo 'Message could not be sent. Mailer Error: '. $mail->ErrorInfo; } ?>
      

I hope you understand how to send email using Gmail SMTP server from a PHP script. Please share your thoughts and suggestions in a comment below.

Related Articles

  • Sending Email Via Gmail SMTP Server In Laravel
  • Send Email Using Gmail SMTP Server And Swift Mailer Library
  • Send Email Using Mailjet in PHP

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

Source: https://artisansweb.net/send-email-using-gmail-smtp-server-php-script/