How to configure SMTP server to Send Email using PHP ?

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?

  • Before you can send Enable IMAP status to access Gmail  from other clients using IMAP
How to configure SMTP server to Send Email using PHP ?
  • 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.
  • Make sure that 2-Step-Verification is disabled
  • Turn ON the “Less Secure App” access or click here.
  • If 2-step-verification is enabled, then you will have to create app password for your application or device.
  • 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

  • Download PHPMailer library from this github link
  • Writing the PHP Code to make an SMTP connection
  • Include packages and files for PHPMailer and SMTP protocol:
  • Initialize PHP Mailer and set SMTP as mailing protocol:
  • A Simple Example for Send Email:
  1. Download the PHPMailer library from this link on github. Use this link to download the zip file directly.
  2. Writing the PHP Code to make an SMTP connection
  • Using your Gmail credentials, connect to host ”smtp.gmail.com”
  • Click here for some more Examples and Tutorials of PHPMailer

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

use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerSMTP;
use PHPMailerPHPMailerException;

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

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

5. A Simple Example for Send Email:

<?php

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       = 'smtp.gmail.com';                       // 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 

    //Recipients
    $mail->setFrom('from@gmail.com', 'From name');
    $mail->addAddress('to@gmail.com', 'Webs Codex');            // Add a recipient
    $mail->addReplyTo('info@gmail.com', 'Replay to name');
    $mail->addCC('cc@gmail.com');
    $mail->addBCC('bcc@gmail.com');

    // 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';

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

Conclusion

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.😊

3 Comments

Leave a Reply

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