How to send email using phpmailer

→ Are you a new visitor? Please visit the page guidance for new visitors ←

How to send email using phpmailer

I have seen a lot of users having issues using their own hosting account to relay emails from the basic mail() function. I’m writing this to show you a way to send email using phpmailer library using a SMTP server like gmail or any other SMTP server. This library can still be used to send mails using the mail() function, and most scripts out there, even WordPress, Drupal, SugarCRM, Yii and Joomla!  if I’m not wrong, uses phpmailer. This is a really good library to be honest and has grown over the years a lot.

The features offered by this library are:

  • It has integrated SMTP support
  • You can send emails with multiple TOs, CCs, BCCs and REPLY-TOs
  • Multipart/alternative emails for mail clients that do not read HTML email
  • You have support for UTF-8 content and 8bit, base64, binary, and quoted-printable encodings
  • It provides SMTP authentication with LOGIN, PLAIN, NTLM and CRAM-MD5 mechanisms over SSL and TLS transports
  • Native language support
  • DKIM and S/MIME signing support
  • It is compatible with PHP 5.0 and later

So as you can see you get a pretty good amount of features here.

You can download the latest version of phpmailer from here:

Download phpMailer

Using phpmailer

Using this is not really that hard, rather is really easy. What you do need to do is after you downloaded the zip archive to your computer, unzip it to your document root of your site. Then include the library into your own application using something similar to:

Once you’ve done this, you are all set to go and start using the library.

A simple example, based on phpmailer snippet is bellow:

When testing and working with this, is best to make sure you use the debug option so that you receive an output back from the script when it finished sending the message or if it failed sending the message.

Its also best to know that if you are using SMTP, make sure to enable Authentication by specifying $mail->SMTPAuth to true

For localization you can also use something like:

You can download the languages from:

That’s it for now, will see you again in our next tutorial.

Request an article ←