Correcting The Special Character Issue When sending HTML Mail

I don't know if anyone else had this problem before when sending HTML email. The characters would come out &#039 for a quote symbol and HTML Tags would have similar issues. The site sits on an IIS server, and the modules being used are Mail System, Mime Mail, and SMTP Authentication support. From some of the posts that I have read it's possible the issue stems from the site sitting on an IIS server. It seemed that other sites that used Apache were ok. It took awhile to find an answer that I was comfortable with. Some solutions had me changing code in core files which while they might have worked, altering core files should be a last resort. Here's the solution I implemented.

Credit for the idea goes to Everett Zufelt who worked out the solution. This is his solution:

  1. copied the file from .../files/mailsystem/filename.inc
  2. place in a custom module's includes dir and .info file using files[] = includes/filename.inc remove original file from {registry} table DELETE FROM r egistry WHERE name='[appropriate-name]' AND module='mailsystem';
  3. drush cc all

I used his solution in module form. This the file structure of the module:

folderstructure

The .info and .module files you need at minimum for a module. The Smtp_Html_Mail folder goes into the sites/all/modules directory. The Smtp_Html_Mail.module file is a shell and has no code in it. The Smtp_Html_Mail.info file has the following code: 

name = Custom HTML SMTP Email Class
description = Defines custom class for used with MailSystem Module.
package = GagaBaby
core = 7.x

files[] = includes/HTMLMailSystem__SmtpMailSystem.mail.inc 

The include file, HTMLMailSystem__SmtpMailSystem.mail.inc, has the main code which is:


formatClass = new HTMLMailSystem;
    }
    else {
      $this->formatClass = new DefaultMailSystem;
    }
    if (drupal_autoload_class('SmtpMailSystem')) {
      $this->mailClass = new SmtpMailSystem;
    }
    else {
      $this->mailClass = new DefaultMailSystem;
    }
  }
  public function format(array $message) {
    return $this->formatClass->format($message);
  }
  public function mail(array $message) {
    return $this->mailClass->mail($message);
  }
}

That's all there is for the code portion. Go to the Administration menu > Modules. You will now install the new module.

Module Go to the Administration menu>Configuration>System and click on Mail System. Set the Site-wide default MailSystemInterface class setting to your new module. 

MailSysSetting

 So before this module was implemented I would get something like this:

Email1

 Now it comes out like this:

Email2

 I'm really surprised this has not been addressed before. It appears it's an issue that goes back at least a few years. It may be that since most Drupal sites are on Apache that it doesn't come up as often. This took a bit to find a solution so hopefully this will fix the problem for someone else.

Author: Christian Apostolu
Christian Apostolu's picture
About:

Owner/founder of Crush The Soul, software developer, amateur athlete, frustrated musician. 

"Once we accept our limits, we go beyond them." - Albert Einstein

 

Category: