This website uses cookies in order to offer you the most relevant information and services online. Please accept cookies for optimal performance.
Learn More

Send Email with PHPMailer and SMTP

This article describes how you can send email with PHPMailer and SMTP. Websites and web applications built on PHP language mostly send transactional emails.

Websites and web applications built on PHP language mostly send transactional emails. PHP has a built-in function called mail() to send an email from the web server itself. However, there are some drawbacks to use this function. So, it is recommended to use an external mail server to handle the transactional emails.

PHPMailer is a third party library that can be used to send transactional emails by SMTP.

To use SMTP in your PHP code, follow the steps below.

Download PHP Mailer.

Add the files 'class.phpmailer.php' and 'class.smtp.php' to your server.

Include the library and instantiate a PHPMailer object.

require_once('class.phpmailer.php');
include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch

Next, set the object to use SMTP.

$mail->IsSMTP(); // telling the class or object to use SMTP

Next, add configuration settings to point the object to the server.

$mail->SMTPDebug  = 1;   // enables SMTP debug information (for testing)
$mail->SMTPAuth   = true; // enable SMTP authentication
$mail->Host       = "smtp.mandrillapp.com"; // sets the SMTP server
$mail->Port       = 25;  // set the SMTP port for the GMAIL server
$mail->Username   = "xxxx.#####@abc.com"; // SMTP account username
$mail->Password   = "************";

Next, set the particulars of the email you want to send.

$mail->AddReplyTo($email, stripslashes($name));
$mail->AddAddress($sReciverEmail, $sReciverName);
$mail->SetFrom($email, stripslashes($name));
$mail->Subject = 'Subject of Email';
$mail->MsgHTML($sEmailBody);
if($sUploadFile) $mail->AddAttachment($sUploadFile); 

Finally, call the send() method. If the method returns true, then email is sent successfully else then there is a problem.

if($mail->Send()) {
print "Email sent successfully";
} else {
echo "Email Error: " . $mail->ErrorInfo;
}

The complete code to send email using PHPMailer and SMTP is below.

require_once('class.phpmailer.php');
include("class.smtp.php");
						
$mail = new PHPMailer(true);
						
$mail->IsSMTP();
						
try {
$mail->SMTPDebug  = 1;
$mail->SMTPAuth   = true;
$mail->Host       = "smtp.mandrillapp.com";
$mail->Port       = 25;
$mail->Username   = "xxxx.#####@abc.com";
$mail->Password   = "************";
$mail->AddReplyTo($email, stripslashes($name)); 
$mail->AddAddress($sReciverEmail, $sReciverName);
$mail->SetFrom($email, stripslashes($name));
$mail->Subject = 'Subject of Email';
$mail->MsgHTML($sEmailBody);
if($sUploadFile) $mail->AddAttachment($sUploadFile); 
$mail->Send();
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
					
Give your feedback to improve the article

Latest Product Updates

Get the latest news and updates about Batoi Products - articles and product releases.

Need our assistance? We are available with you.

Existing Customer?
Get Support
Talk To Consultant?
Get An Appointment
Contact Us

Batoi global offices and contact details

Stay Connected

Join our community, newsletters, and social media.

Batoi's new website is in the process of release and will be completed by the end of September 2020. Please bear with us.