Send Email via SMTP Server in PHP using PHPMailer

Send email from the script is the most used feature for the web application. Basically, mail() function in PHP is used to send email from the PHP script. When you sending email using the PHP mail() function, the mail is sent from your web server. Sometimes it may cause issues on 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, email is sent from the mail server rather than the web server.

The easiest way to send email in PHP with SMTP is to use PHPMailer library. PHPMailer provides an ability to send email via SMTP server in PHP. Various configuration options of PHPMailer library allow you to send a text email, HTML email, and attachments.

In this tutorial, you’ll show how you can send HTML email with SMTP in PHP using PHPMailer. You can use the following example script to send SMTP mail using PHPMailer library.

Send HTML Email

Use isHTML(true) to set email format to HTML.

// Include and initialize phpmailer class
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;

// SMTP configuration
$mail->Host '';
$mail->SMTPAuth true;
$mail->Username '';
$mail->Password '******';
$mail->SMTPSecure 'tls';
$mail->Port 587;


// Add a recipient

// Add cc or bcc 

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

// Set email format to HTML

// Email body content
$mailContent "<h1>Send HTML Email using SMTP in PHP</h1>
    <p>This is a test email has sent using SMTP mail server with PHPMailer.</p>"
$mail->Body $mailContent;

// Send email
'Message could not be sent.';
'Mailer Error: ' $mail->ErrorInfo;
'Message has been sent';

Send HTML Email with Attachments

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

// Add attachments
$mail->addAttachment('images/codexworld_3.png''new-name.png'); //set new name

Send Email to Multiple Recipients

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

// Add multiple recipients

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. Click the Signing in to Google link from Sign-in & security section.
  • Scroll down the Password & sign-in method section and turn Off the 2-Step Verification.
  • Scroll down the Connected apps & sites section and turn On Allow less secure apps.

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.

// SMTP configuration
$mail->Host '';
$mail->SMTPAuth true;
$mail->Username '';
$mail->Password '********';
$mail->SMTPSecure 'tls';
$mail->Port 587;

Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request

Recommended Tutorials For You


  1. Thanks Said...
  2. Saquib Rizwan Said...

Leave a reply