Hello,

We are using mimemail for some of our sites.
One client got a particular request, asking us to send all mails in HTML except for a particular form.

I did this small patch allowing us to use in $message a key: plaintext.
Once this key is set, it means that the message have to be sent in plaintext.

You can easily put in in a hook_mail_alter() and have your mail sent in plain text, with or without attachements.

Comments

pol’s picture

StatusFileSize
new796 bytes

The patch version 1 is for Mimemail 6.x-1.x.

pol’s picture

StatusFileSize
new784 bytes

The patch version 2 is for Mimemail 6.x-1.0-alpha8.

sgabe’s picture

Title: Patch allowing plaintext mail. » Accept plaintext and text parameters for system messages
Status: Active » Needs work

Seems reasonable, but I would stick with the current coding style and do this:

     $plaintext = isset($message['plaintext']) ? $message['plaintext'] : NULL;
     $text =  isset($message['text']) ? $message['text'] : NULL;
     return mimemail($from, $to, $subject, $body, $plaintext, $headers, $text, $attachments, $mailkey);

It's enough to attach patches against the development branches. Patches against previous versions are quite useless. :)

pol’s picture

StatusFileSize
new819 bytes

Hello sgabe,

If the $plaintext var is set and $text empty, the mail sent will be empty, that's why I did this way.
if and only if the $plaintext is set, it will be sent in plaintext.
Now it's up to you to decide of course, but both solutions are good for me.

About the patches for alpha8, we are using Jenkins at work and we avoid using dev versions, that's why I always post a patch against the latest 'stable' release, so Jenkins can download it during the build, and we are sure that this won't fail.

Anyway, this is the patch with your modification for the dev version.

sgabe’s picture

If $text is empty, mimemail_html_body() should create automatically the text alternative. If that's not the case, it seems we have a bug.

function mimemail_html_body($body, $subject, $plaintext = FALSE, $text = NULL, $attachments = array()) {
  if (empty($text)) {
    // todo: remove this preg_replace once filter_xss() is properly handling
    // direct descendant css selectors '>' in inline CSS. For now this cleans
    // up our plain text part. See mimemail #364198, drupal #370903
    $text = preg_replace('|<style.*?</style>|mis', '', $body);
    $text = drupal_html_to_text($text);
  }
  [...]
}
pol’s picture

StatusFileSize
new1.59 KB

Hello sgabe,

I made further tests this morning and I have found a working solution.

The assumption in #6 is not working.

If you do not provide the $text variable, mimemail_html_body() should create it.
But, the line $text = preg_replace('|<style.*?</style>|mis', '', $body); always returns an empty string.

I cleaned the whole stuff by doing this:

function mimemail_html_body($body, $subject, $plaintext = FALSE, $text = NULL, $attachments = array()) {
  $text = empty($text) ? drupal_html_to_text($body) : $text; 
  [...]

And that's working pretty good.

If you do not submit the $message['text'], mimemail will built it.
If it's submitted, then it will use it, as is.

This patch is for the 6.X-1.X version, I'll provide the patch for 6.X-1.0-alpha8 in a few minutes.

We are already using the code in pre-production project and we are happy with it.

pol’s picture

StatusFileSize
new1.59 KB

This patch is for Mimemail 6.x-1.0-alpha8.

sgabe’s picture

Then we need to deal with #1116930: No text alternative if the CSS is too large first. So far I couldn't reproduce this, I will take a look at it again.

pol’s picture

StatusFileSize
new3.37 KB

New patch, also fixing #1116930: No text alternative if the CSS is too large (hard one!) for Mimemail 6.X-1.X.

pol’s picture

StatusFileSize
new3.37 KB

Same as #10 for Mimemail 6.X-1.0-alpha8

pol’s picture

StatusFileSize
new3.37 KB

Small php typo in #11, fixed in this patch.

pol’s picture

GRRR !

Bad function name, these patch are for 6.X-1.X and alpha8.
These patches cancel #12, #11, #10.

pol’s picture

Adding a trim() to remove useless spaces in the cleaned body.

sgabe’s picture

Thank you for your work on this, Pol! Please do not create aggregated patches, let's deal with the issues separately.

sgabe’s picture

Status: Needs work » Fixed
StatusFileSize
new1.81 KB

Committed to the development snapshot. Thank you Pol!

pol’s picture

That's great :-) You're welcome !

Thanks !

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

pol’s picture

Please, do not forget to include authoring informations when you commit...

Thanks !