Send Email via SMTP Server in PHP using PHPMailer

Send email from the script is the most used functionality in the web application. The PHP mail() function is used to send emails from the PHP script. When you send an email using the mail() function in PHP, the mail is sent from your web server. Sometimes it may cause issues in sending an email and fails to deliver mail to the recipient. With SMTP you can overcome this issue, SMTP is the most recommended way to send email from the PHP script. When you send an email via SMTP, the email is sent from the mail server rather than the web hosting server.

The easiest way to send email in PHP with SMTP is to use the PHPMailer library. PHPMailer provides the ability to send an email via an SMTP server using PHP. Various configuration options of the PHPMailer library allow you to configure and customize the email sending functionality as per your needs. You can send a text or HTML email with single or multiple attachments using PHPMailer. In this tutorial, we will show you how to send HTML email with SMTP in PHP using PHPMailer.

In the example script, we will integrate PHPMailer in PHP and send SMTP mail using PHPMailer library. Use the following example code to send HTML email with attachment using PHP.

Send HTML Email via SMTP Server

PHPMailer Library:
We will use PHPMailer to send emails via SMTP server. So, include the PHPMailer library files and initialize the PHPMailer object.

// Import PHPMailer classes into the global namespace 
use PHPMailer\PHPMailer\PHPMailer;
use 
PHPMailer\PHPMailer\SMTP;
use 
PHPMailer\PHPMailer\Exception;

// Include PHPMailer library files
require 'PHPMailer/Exception.php';
require 
'PHPMailer/PHPMailer.php';
require 
'PHPMailer/SMTP.php';

// Create an instance of PHPMailer class
$mail = new PHPMailer;

Note that: You don’t need to download the PHPMailer library separately. All the required PHPMailer library files are included in the source code and you can install PHPMailer without composer in PHP.

SMTP Configuration:
Specify the SMTP server host ($mail->Host), username ($mail->Username), password ($mail->Password), encryption type ($mail->SMTPSecure), and port ($mail->Port) as per your SMTP server credentials.

// SMTP configuration
$mail->isSMTP();
$mail->Host     'smtp.example.com';
$mail->SMTPAuth true;
$mail->Username 'user@example.com';
$mail->Password '******';
$mail->SMTPSecure 'tls';
$mail->Port     587;

Configure Email:
Specify some basic email settings (like, sender email & name, recipient email, subject, message, etc). Set isHTML() to TRUE for sending email in HTML format.

// Sender info 
$mail->setFrom('sender@example.com''SenderName');
$mail->addReplyTo('reply@example.com''SenderName');

// Add a recipient
$mail->addAddress('recipient@example.com');

// Add cc or bcc 
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

// Email subject
$mail->Subject 'Send Email via SMTP using PHPMailer';

// Set email format to HTML
$mail->isHTML(true);

// Email body content
$mailContent '
    <h2>Send HTML Email using SMTP Server in PHP</h2>
    <p>It is a test email by CodexWorld, sent via SMTP server with PHPMailer using PHP.</p>
    <p>Read the tutorial and download this script from <a href="https://www.codexworld.com/">CodexWorld</a>.</p>'
;
$mail->Body $mailContent;

// Send email
if(!$mail->send()){
    echo 
'Message could not be sent. Mailer Error: '.$mail->ErrorInfo;
}else{
    echo 
'Message has been sent.';
}

Send HTML Email with Attachments

Use the addAttachment() method of PHPMailer class to add an attachment to the email. You can attach multiple attachments to the email by adding addAttachment() method multiple times.

// Add attachments
$mail->addAttachment('files/codexworld.pdf');
$mail->addAttachment('files/codexworld.docx');
$mail->addAttachment('images/codexworld.png''new-name.png'); //set new name

Send Email to Multiple Recipients

Add addAddress() method multiple times for sending email to multiple recipients.

// Add multiple recipients
$mail->addAddress('john.doe@gmail.com''John Doe');
$mail->addAddress('mark@example.com'); // name is optional

Send Email using PHPMailer in CodeIgniter

Send Email using Gmail SMTP

If you want to use Gmail SMTP to send email, you need to make some changes in Google account settings. Follow the below steps to use Gmail SMTP in PHPMailer library.

  • Login to your Google account. Go to the My Account page.
  • Navigate to the Security page and scroll down to the Signing in to Google section » Turn Off the 2-Step Verification
  • .

    google-account-security-2-step-verification-gmail-smtp-codexworld
  • Scroll down the Less secure app access section and turn On Less secure app access.
    google-account-security-less-secure-app-access-gmail-smtp-codexworld

You are done! Now you can use Gmail SMTP to send email from the PHP script.

Specify your Gmail account credentials (email address and password), SMTP host, and port to send email using Gmail SMTP.

// SMTP configuration
$mail->isSMTP();
$mail->Host     'smtp.gmail.com';
$mail->SMTPAuth true;
$mail->Username 'codexworld@gmail.com';
$mail->Password '********';
$mail->SMTPSecure 'tls';
$mail->Port     587;

Send Email from Localhost in PHP

Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request

7 Comments

  1. Naushad Ansari Said...
  2. Shifanawas Said...
    • CodexWorld Said...
  3. Mufleeh Said...
  4. RaviKant Said...
  5. Thanks Said...
  6. Saquib Rizwan Said...

Leave a reply

keyboard_double_arrow_up