Sending Emails in PHP with PHPMailer

Originally published at: http://www.sitepoint.com/sending-emails-php-phpmailer/

PHPMailer is the most popular open source PHP library to send emails with. It was first released way back in 2001 and since then it has become a PHP developer’s favorite way of sending emails programatically, beside a few other fan favorites like Swiftmailer.

Emails flying stock picture

In this article we’ll talk about why you should use PHPMailer instead of PHP’s mail() function and we’ll show some code samples on how to use this library.

Is it an alternative to PHP’s mail() function?

In most cases, it’s an alternative to PHP’s mail() function, but there are many other cases where the mail() function is simply not flexible enough to achieve what you need.

First of all, PHPMailer provides an object oriented interface, whereas mail() is not object oriented. PHP developers generally hate to create $headers strings while sending emails using the mail() function because they require a lot of escaping – PHPMailer makes this a breeze. Developers also need to write dirty code (escaping characters, encoding and formatting) to send attachments and HTML based emails when using the mail() function whereas PHPMailer makes this painless.

Continue reading this article on SitePoint

I’ve been using phpmailer for years and it works real fine for me.
If you need to send email in languages that have accented characters, don’t forget to set the proper character encoding. I believe phpmailer assumes ISO-latin 1, but if your text is in UTF-8 all the non standard characters will be screwed-up:

$mail->CharSet = 'UTF-8';

Also, it seems recommended to end your script by:

$mail->ClearAllRecipients();
$mail->ClearAttachments();

Don’t forget to set the “sender” option properly as most anti-spam soft will reject mail without a “sender”.

$mail->Sender = 'somebody@domain.com';

Too bad, phpmailer can’t intelligently translate html to text by itself. I use a class html2text available here:

but it does not seem to be updated. Any better option?

You don’t need to call clearAllRecipients or clearAllAttachments unless you’re in a loop where you’re reusing the same instance of PHPMailer, such as in the mailing list example provided with PHPMailer.

Sender is used to provide a path back to the origin - it gets converted into the return-path by the receiver. It’s not just used in a sender header within the message, it’s also used as the SMTP envelope sender, and so is commonly used for VERP addressing. If you don’t set it yourself, your mail server will do it for you, but that may mean it’s not set to what you expect.

html to text conversion is really a bit outside PHPMailer’s scope - there used to be a bundled converter, but that had to be removed for license reasons. Now there is only a basic converter, but you can inject your own through the html2text function that is also used internally when you call msgHTML.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.