How to send email via SMTP server using PHPMailer

How to send mail via SMTP server using php mailer

Send email via SMTP server using PHPMailer

 

In web application, sending mail from the script is an important and frequently used functionality. PHP mail() function and SMTP are the two most used way to send email from the PHP script.

Sending an email using PHP mail() fucntion sometimes cause issues and fails to deliver mail to the recipient, because at this time mail is sent from your web server. Using SMTP you can resolve this issue because when you send an email via SMTP, email is sent from the mail server rather than the web hosting server. Thatswhy, SMTP is the most preffered way to send email from the PHP script.

PHPMailer is the easiest way to send email in PHP with SMTP.  The PHPMailer provided an ability to send email via SMTP server using PHP. PHPMailer library provides various configuration options which allow you to configure and customize the email sending functionality as per your needs. PHPMailer allows you to send a text or HTML email with single or multiple attachments.

In this blog, 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 the PHPMailer to send email via SMTP server. So, include the PHPMailer library files and initialize the PHPMailer object.

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

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

$mail = new PHPMailer;

Note that: You don’t need to download 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), and port ($mail->Port) as per your SMTP server credentials.

// SMTP configuration
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$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 as HTML format.

$mail->addReplyTo('[email protected]', 'RexcelIT');

// Add a recipient
$mail->addAddress('[email protected]');

// Add cc or bcc
$mail->addCC('[email protected]');
$mail->addBCC('[email protected]');

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

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

// Email body content
$mailContent = '

 

Send HTML Email using SMTP in PHP :
$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 attach multiple attachments to the email by adding addAttachment() method multiple times.

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

 

Send Email to Multiple Recipients –

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

// Add multiple recipients
$mail->addAddress('[email protected]', 'John Doe');
$mail->addAddress('[email protected]'); // name is optional