Send Email via SMTP Server in PHP using PHPMailer

Any web application may need send email through it’s script. In PHP, we use mail() function to send from web server .However sometimes it may cause delivery issue.
Means when we send email from php using just mail() function , sometimes recipient don’t receive the mail .To overcome this issue we need SMTP to send email .
SMTP send email using PHPmailer library

SMTP is the most recommended way to send email .Using this email send from mail server rather than web server .
There a famous library available in PHP called ‘PHPMailer library’ .It allow to send email via SMTP having lots of configuration .
It allows to send plain text, HTML text .

We have created below tutorials on how to send HTML email with SMTP in PHP using PHPMailer library. you can use this script in your PHP website to send email.

php mysql video tutorial in Hindi language by UnitedWebSoft

Send HTML email

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

$mail->setFrom('', 'UnitedWebSoft');
$mail->addReplyTo('', 'UnitedWebSoft');

// 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
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
    echo 'Message has been sent';

Send HTML Email with Attachments

Below demonstrate using addAttachment() function to send email with attachment. you can use multiple times the same function to send multiple attachment as shown below .

Send Email to Multiple Recipients

We can also send the same email to multiple recipients using addAddress() function as shown below .

// Add multiple recipients

Send Email using Gmail SMTP

We can also send email using our gmail SMTP details. to do so,first of all you need to change some setting in your gmail account .
Follow below steps :

  • After logging in gmail, go to the My Account page. Click the Signing in to Google link from Sign-in & security section.
  • Now on the same page, scroll down below ,find ‘Password & sign-in method ‘ section and turn Off the ‘ 2-Step Verification’
  • Again scroll down ,find ‘Connected apps & sites’ . Turn on there ‘Allow less secure apps’

Finally we can now send email using Gmail SMTP details. find below script . You have to mention your gmail details like email,password, SMTP host and port.

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

If you have any query on this tutorials, write below on comment section . I will try my best to reply comments .
We also provide advance PHP course with e-commerce project based training. visit our website