PHPMailer powerful and simple class for sending email from PHP

Featured class in the PHP ecosystem that greatly facilitates the job of sending email from PHP, especially especially complex emails.

The process to send email from PHP is very simple. It is limited to a function called “mail()” that sends according to a series of parameters that we send. The truth is that it is not easy to imagine another easier way to send email, but what happens when the email to be sent is more complex than usual?

At .com we have discussed in various articles topics related to sending emails from PHP, since it is one of the most basic and repetitive topics in the world of web applications. For example, we recommend reading the article or the article.

The problem comes when the email you have to create has some advanced features, like attaching files or authentication on the SMTP server. In those cases, the mail() function falls short and we have to resort to other means.

PHPMailer to send emails

PHPMailer is a class created specifically to make it easy to send emails that have complex features, and the truth is that it is quite useful and powerful. It allows sending email with attachments, different types of SMTP servers (with or without user authentication), while supporting other features of PHP’s native mail() function, but in a slightly simpler way.

It is available with a free distribution and use license, open source and can be obtained through the following link:

The truth is that the class is very easy to understand and use. To guide you in its use, you have the examples folder of the project, with some examples of use that will serve as a template to send emails in a quick and simple way.

See also  Games in HTML5

Take a look at the list of examples that you can find explained in the index.html of the examples folder, of the download Zip. There you will find different codes already ready to send emails with different characteristics.

Sending basic email with PHPMailer

To give this article a little more utility, we are going to review some of the PHPMailer examples that are on the documentation’s examples page itself, or among the download files that you get when you download PHPMailer.

The code is quite easy to understand, so I am simply going to put some comments in Spanish so that it can be understood better.

//include the PHPMailer class
require_once(‘../class.phpmailer.php’);

//instantiate an object of the PHPMailer class
$mail = new PHPMailer(); // defaults to using php “mail()”

//define the body of the message in a variable $body
// get the content of a text file
//we could also do $body=”content…”;
$body = file_get_contents(‘contents.html’);
// I had to comment this line
//because if I put it, it leaves the $body empty
// $body = preg_replace(‘//i’,”,$body);

//define the email and name of the sender of the message
$mail>SetFrom(’email@sender.com’, ‘Full name’);

//define the “reply” email address, to which to reply to messages
//Obs: it is good to leave the same address as the From, so as not to fall into spam
$mail>AddReplyTo(“email@sender.com”,”FullName”);
// Define the email address to which the message is sent
$address = “email@recipient.com”;
//add it to the class, indicating the name of the recipient person
$mail>AddAddress($address, “Full name”);

// Add a subject to the message
$mail>Subject = “Sending email with PHPMailer in PHP”;

//I can define an alternative body of the message, containing only text
$mail>AltBody = “Alternative body of the message”;

// insert the text of the message in HTML format
$mail>MsgHTML($body);

//assign an attachment to the message
$mail>AddAttachment(“path/attachment_file.gif”);

// send the message, checking if it was sent correctly
if(!$mail>Send()) {
echo “Error sending message: ” . $mail>ErrorInfo;
} else {
echo “Message sent!!”;
}

As you have been able to verify, the content of the script is quite elementary, since the complexity remains all in the PHPMailer itself.

But notice how easy it is to send an attachment in this email, with a single line of code, calling the “addAttachment()” method.

Accents in email and subject lines

One of the problems that I have found when using PHPMailer is that the accents in the subject line appear with strange characters, instead of being displayed correctly. I am using the UTF8 character set in my .php file and to ensure that the Latin characters are displayed correctly in the subject and/or body of the message, PHPMailer must be told what character set to use. You can proceed by indicating this pair of values ​​in attributes of the PHPMailer class.

$mail>CharSet = “UTF8”;
$mail>Encoding = “quotedprintable”;

With this you will experience that the accents and other special characters of the Spanish language are displayed correctly.

Conclusion to the introduction to PHPMailer and next steps

The PHPMailer class is very interesting, an essential to simplify many of the email management tasks from PHP. It is worth taking a closer look at it and see how simple it solves some of the most requested needs when it comes to sending email with PHP, such as the use of attachments.

In the next article we will continue working with PHPMailer, taking some interesting details about sending email from PHP with authenticated SMTP and using GMail servers.

Loading Facebook Comments ...
Loading Disqus Comments ...