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.

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

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

$mail->setFrom('info@example.com''CodexWorld');
$mail->addReplyTo('info@example.com''CodexWorld');

// Add a recipient
$mail->addAddress('john@gmail.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 "<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
if(!$mail->send()){
    echo 
'Message could not be sent.';
    echo 
'Mailer Error: ' $mail->ErrorInfo;
}else{
    echo 
'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('docs/codexworld_1.pdf');
$mail->addAttachment('docs/codexworld_2.docs');
$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
$mail->addAddress('john@gmail.com');
$mail->addAddress('doe@gmail.com');

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.
    send-email-php-gmail-smtp-account-settings-codexworld
  • Scroll down the Password & sign-in method section and turn Off the 2-Step Verification.
    send-email-php-gmail-smtp-off-2-step-verification-codexworld
  • Scroll down the Connected apps & sites section and turn On Allow less secure apps.
    send-email-php-gmail-smtp-allow-less-secure-apps-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.

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

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

Recommended Tutorials For You

2 Comments

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

Leave a reply