How to send HTML mail using SMTP authentication on Drupal

Yesterday I was helping a colleague who needed to send HTML formatted mail from a Drupal 7 site. I did some quick research and found the Mail system and HTML mail modules. I enabled the modules and configured them and figured I was done.

My colleague came back to me and told me that now, not only was the mail not sending as HTML, it wasn't sending at all!

Some research brought me to Using HTML Mail together with SMTP Authentication Support -- excellent!

I followed the steps in the tutorial and after creating the HTML mail / SMTP auth class I received a PHP error stating that Drupal had failed to load the class.

When you use the Mail system module to create a new class, behind the scenes it actually creates a new PHP (.inc) file and stores it in your sites /mailsystem/filename.inc . Unfortunately the file directory for the site I'm working on is outside of the Drupal root, and not easily accessible to the class loader. Along with this, we use a git deployment workflow, and the files directory is not version controlled.

Solution

I first grabbed a copy of the auto-generated file from the server, and then deleted the copy on the server. Then I added that file to a custom module using the files[] directive in the module's .info file (this tells Drupal to load the class). Finally I had to remove the existing entry for the file from the registry table of the database, making sure to remove the correct file, and that I had the generating module "mailsystem" as a WHERE clause.

Now we have a mail system class that does HTML formatting and SMTP authenticated delivery working with the appropriate modules, and it's version controlled too.

See also: #1369736: Alternative locations for "created" Class files?