Hi everyone,
Since I began to work with simplenews/simplenews template + mimemail I had problems with sending "correct" email/newsletter. In fact a lot of newsletter (originally conceived as pure html), was received by subscribers in a strange manner, in part plain and in part html, melt into a single email:
First part: Content-Type: text/plain; charset=utf-8: all the content is plain, with a css preamble (ugly and unreadable)
Second part: Content-Type: text/html; charset=utf-8: html email correctly rendered
I've investigated much this issue. I found (with the help of Thomas Barregren, simplenews template maintainer, and the logic behind print_mail submodule) that mimemail creates by itself a multipart email when newsletter subscribers aren't defined to receive neither plain nor html content (this is the case for subscribers that aren't registered users).
But multipart email isn't an actual standard as stated here:
http://en.wikipedia.org/wiki/Html_e-mail#Multi-part_formats
and a lot of email clients reject or misunderstand emails built in this manner.
I thought that the best solution was that used by print_mail (print submodule) that builds an html email without mimemail module.
I noticed that regarding headers information, print_mail sets up an header array variable:
$headers = array('Content-Type' => 'text/html; charset=utf-8');
and then sends emails with a drupal_mail command:
drupal_mail('print_mail_sendpage', $to, $params['subject'], $params['body'], $from, $headers);
On the contrary, basing the work on mimemail, simplenews/simplenews template sends the meta tag from a pre-defined html code to a mimemail function:
mimemail($from, $mail->to, $mail->subject, $mail->body, $plain_text_only, $headers, $plain_text_body, array(), 'simplenews-send-mail');
I think that the best approach is within print_mail (I know, we lose plain email... but nowadays who reads emails with a plain reader?).
Then... How to apply these concepts into simplenews, discarding mimemail?
I found a fast solution that works well: into simplenews.module (latest -dev version), around 1338 line there is simplenews_mail_send function. Ground on the existence (or not) of mimemail module, it's used a different send function:
mimemail($from, $mail->to, $mail->subject, $mail->body, $plain_text_only, $headers, $plain_text_body, array(), 'simplenews-send-mail');
or
drupal_mail('simplenews-send-mail', $mail->to, $mail->subject, $mail->body, $from_email, $headers);
So... my solution is to substitute mimemail call with the same drupal_mail call, with a preamble statement regarding utf-8 header:
$headers['Content-Type'] = 'text/html; charset=utf-8';
So the final code is:
if (module_exists('mimemail')) {
if ($mail->s_format == 'plain') {
$plain_text_only = TRUE;
$plain_text_body = $mail->body;
}
else {
$plain_text_only = FALSE;
$plain_text_body = NULL;
}
if (variable_get('simplenews_debug', FALSE)) {
watchdog('simplenews', t('Outgoing email via mimemail.<br />Subject: %subject<br />Recipient: %to', array('%to' => $mail->to, '%subject' => $mail->subject)), WATCHDOG_NOTICE);
}
//return mimemail($from, $mail->to, $mail->subject, $mail->body, $plain_text_only, $headers, $plain_text_body, array(), 'simplenews-send-mail');
$headers['Content-Type'] = 'text/html; charset=utf-8';
return drupal_mail('simplenews-send-mail', $mail->to, $mail->subject, $mail->body, $from_email, $headers);
}
else {
if (variable_get('simplenews_debug', FALSE)) {
watchdog('simplenews', t('Outgoing email.<br />Subject: %subject<br />Recipient: %to', array('%to' => $mail->to, '%subject' => $mail->subject)), WATCHDOG_NOTICE);
}
return drupal_mail('simplenews-send-mail', $mail->to, $mail->subject, $mail->body, $from_email, $headers);
}
I think this is a good approach, that solves problems joining print_mail logic with simplenews.
Maybe this can be inserted as a new feature into simplenews...
Alessandro
Comments
Comment #1
alex72rm commentedhttp://drupal.org/node/373242