after installing mimemail, my users see this send as plain text.
They did not select it as they want HTML mail, but now they send me messages saying that they receive mails in plain text whereas they did NOT selected plain text.

Off course those messages are emails send from different modules that use the core user_mail() function.
As such I changed the code so that the mails send by user_mail() go trough mimemail() and I though I share my code with you in case you feel it's interresting.

CAUTION: I did this in 4.7.x-1.x-dev

In user.module I changed the user_mail() function as follows:

 if (variable_get('smtp_library', '') && file_exists(variable_get('smtp_library', ''))) {
   include_once './' . variable_get('smtp_library', '');
    return user_mail_wrapper($mail, $subject, $message, $header);
  }
  else if (module_exist('mimemail')) {
    return user_mail_to_mimemail($mail, $subject, $message, $header);
  }
  else {

here you see that I added a call to the user_mail_to_mimemail() function when the mimemail is activated and this function is added to your mimemail.module

/**
 * User mail to mimemail.
 * Converts the generic user_mail function to use mime mail
 * add this in the user_mail() of the user module:
 * else if (module_exist('mimemail')) {
 *   return user_mail_to_mimemail($mail, $subject, $message, $header);
 * }
 */
function user_mail_to_mimemail($mail, $subject, $message, $header) {
  // retreieve the from address from the headers 
  // and make headers an array
  // step 1 name: value\r\nname: value 
  // convert \r\n into a seperator (sometimes only \n)
  $headerlist = str_replace("\r", '|', $header);
  $headerlist = str_replace("\n", '|', $headerlist);
  $headerlist = str_replace("||", '|', $headerlist); // make sure we ain't got doubles...
  // split into array namevalues[0] = 'name: value'; namevalues[1] = 'name: value'
  $namevalues = explode("|",$headerlist);
  // loop trough namevalues and build the headers array
  foreach ($namevalues as $namevalue) {
    // split name: value in tmp[0] = name; tmp[1] = value
    $tmp = explode(":",$namevalue); 
    if (strtolower (trim($tmp[0])) == "from"){
      // set the sender !
      $sender = trim($tmp[1]);
    } else {
      // do not make from header
	  $headers[trim($tmp[0])] = trim($tmp[1]);
	}
  }
  return mimemail($sender, $mail, $subject, $message, variable_get('mimemail_textonly', 0), $headers);
}

Finally I had to change all the settings in the admin where you can set some text that is mailed (like when a user is registered) so that it now is HTML.

Comments

allie micka’s picture

Status: Active » Closed (won't fix)

Thanks for your patch! It's very handy. This functionality basically exists in 5.x, and we're no longer maintaining 4.7 releases.

Thanks again!