How to configure SMTP server to Send Email using PHP ?

php code to send email from contact form, send mail in PHP, php code to send email using smtp server, download php mailer, send email using php, smtp

Hello friends today, I will tell you in this blog one of the most important topics, How to configure SMTP server to Send Email using PHP ?

Most web projects have email capabilities. Before uploading to an e-mail server, the e-mail functionality on the local server must be checked. However, the PHP mail() function does not work on localhost. In this article we show how to send email from localhost in PHP. With this simple PHP script you can send email from a local host server such as XAMPP, WAMP, or others.  

We will use PHPMailer to send email from localhost. In addition to sending text emails, you can also send HTML emails from localhost to PHP using PHPMailer. Download PHPMailer from GitHub, you can also get it from our download package. There are some steps to set Gmail SMTP settings in below.

How To Enable Email Sending In Gmail?

1. Before you can send Enable IMAP status to access Gmail from other clients using IMAP

2. Before you can send email using Gmail SMTP server, you need to set some security settings and permission levels in your Google account security settings.

3.  Make sure that 2-Step-Verification is disabled

4.  Turn ON the “Less Secure App” access or click here.

5. If 2-step-verification is enabled, then you will have to create app password for your application or device.

6. For security measures, Google may require you to complete this additional step while signing-in. Click here to allow access to your Google account using the new device/app.

Note: It may take an hour or more to reflect any security changes

Writing the PHP Code to Send Email using Gmail SMTP

Step 1: Download PHPMailer library from this github link.

Step 2: Writing the PHP Code to make an SMTP connection.

Step 3: Include packages and files for PHPMailer and SMTP protocol:

Step 4: Initialize PHP Mailer and set SMTP as mailing protocol:

Step 5: A Simple Example for Send Email:

Step 1:  Download the PHPMailer library from this link on github. Use this link to download the zip file directly.

Step 2: Writing the PHP Code to make an SMTP connection

  • Using your Gmail credentials, connect to host “”
  • Click here for some more Examples and Tutorials of PHPMailer

Step 3: Include packages and files for PHPMailer and SMTP protocol:

use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerSMTP;
use PHPMailerPHPMailerException;

Step 4: Initialize PHP Mailer and set SMTP as mailing protocol:

$mail = new PHPMailer();
$mail->Mailer = "smtp";

Step 5: A Simple Example for Send Email:


require 'PHPMailerAutoload.php';

// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output
    $mail->isSMTP();                                            // Send using SMTP
    $mail->Host       = '';                       // Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = 'your usrename';                        // SMTP username
    $mail->Password   = 'your password';                        // SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; 
    $mail->Port       = 587;                                    // TCP port to connect to, use 465 for 

    $mail->setFrom('', 'From name');
    $mail->addAddress('', 'Webs Codex');            // Add a recipient
    $mail->addReplyTo('', 'Replay to name');

    // Attachments
    $mail->addAttachment('your file/image name');               // Add attachments
    // Content
    $mail->isHTML(true);                                         // Set email format to HTML
    $mail->Subject = 'Hello, Webs Codex';
    $mail->Body    = 'This is a Test Email sent via Gmail SMTP Server using PHP mailer class';

   	echo 'Message has been sent';
    } catch (Exception $e) {
    	echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";



We hope that the steps above were helpful and that you have successfully sent email from the SMTP server to Gmail using PHP. Feel free to contribute if you have an issue that is not listed as part of this lesson. Use the comments section below to ask / share feedback.

You can always support by sharing on social media or recommending my blog to your friends and colleagues. If you have any suggestions / problems about this tutorial, please comment on the  form below.😊

Leave a Reply

This Post Has One Comment