I admit i don't quite understand the ins/outs of drupal mail and how mimemail fits into it; but my guess is something like this:

drupal_mail() is a wrapper that defaults to using php mail() which can't handle (among other things) smtp authentication, html, or attachments.

I have always used phpmailer class to provide html email and mostly to allow for smtp authentication.

I think (but have no idea) that people (i.e. module designers for modules like simplenews and send) use mimemail instead of drupal_mail to be able to add attachments into the mix.

SO - here's my question:

I have written a couple mail related modules. Both use the mail_alter hook to intercept mail and do something to it:

1. removes $to domain and replaces it with a test domain - very useful for testing sites that use a copy of the live db that has real email addresses. To avoid spamming site's clients; all the email can be redirected to a different mail server

2. my new notices.module which logs all email sent from the site to each user and allows them to review online in case they didnt get the email.

so my question is: what is mimemail's version of hook_mail_alter so that i can make these modules compatible?

I am looking at code for _prepare and mimemail_engine and i think this is where i need to be looking - but also seems like I need to recreate these entire functions to do what i want - is that really necessary? Can't we just have a mail alter hook call?

Comments

liquidcms’s picture

Title: how do i mail_alter a mimemail? » need to invole mail_alter call
Category: support » feature
Priority: Normal » Critical
Status: Active » Needs review
StatusFileSize
new27.52 KB

well no response.. so i simply created a patch to add the mail_alter hook invoke call to mimemail.module

Since this seems like such an obvious addition as it now lets other legacy modules "see" and "alter" mail being processed by the mimemail module - i would assume there is some reason i am overlooking as to why this is not already there. Please let me know if this is the case.

NOTE: I debated but ended up adding a mail_alter invoke as opposed to creating a new mimemail_alter invoke. The approach taken here allows legacy modules which have mail_alter hooks to now work with the mimemail.module. It does NOT however take advantage of all the variables passed to mimemail() function (e.g. attachments). There should likely be both a mail_alter invoke (this patch) and a mimemail_alter invoke.

Peter Lindstrom
LiquidCMS - Content Management Solution Experts

liquidcms’s picture

StatusFileSize
new771 bytes

just realized that patch got a little messed up.. here's a cleaner one.

pillarsdotnet’s picture

Title: need to invole mail_alter call » support hook_mail_alter for better integration with other modules

Subscribe, plus title change for better searching

allie micka’s picture

Interesting. In principle, I think this is a great idea, but don't have a lot of time to look into it. Anyone else care to identify ramifications?

matthewbot’s picture

Well, I know the Simplenews 6.x branch goes through lots of hoops to fire off mail hooks when using mimemail as a backend. Meanwhile, the 5.x branch exhibits the behavior that the OP noted in that it simply doesn't fire off hooks at all. So we can simplify other's code and enforce consistent behavior with a simple addition. +1 from me.

Before we do this though, mimemail's hook_mail_alter() needs to be addressed, as it will cause #215310: stripped html tags from body to apply even to things sent out directly via mimemail().

matthewbot’s picture

StatusFileSize
new1.06 KB

updated this, I still think its useful.

jerdavis’s picture

Status: Needs review » Fixed

After further review and testing, this has been commited. Thank you Matthewbot!

liquidcms’s picture

whoo hoo.. thanks guys.. now check out my very cool mail_redirect and notices modules which require this patch.

BTW - how is matthewbot's patch any different than the one i initially posted here?

Anonymous’s picture

Status: Fixed » Closed (fixed)

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

hanoii’s picture

Version: 5.x-1.x-dev » 5.x-1.0

Sorry to bring this issue back to life, but, I found a problem... after considerable debugging on this module I found a problem with this patch, only $body is sent through the mail_alter, but what happens with $text if it's called within the function, it doesn't get modified because it's never sent to mail_alter hooks?

I am currently working on a module to personalize newsletters, and using mimemail with this patch (or dev version for that matters) I was noticing that although I was modifying the body of the mail in my mail_alter hook the mail I was receiving was still unmodified.

Now, I am thinking a few ways to overcome this, I think I am in favor of number 3 first and number 2 second.

1. Send both parts through the same mail_alter hook with the same key. Pros: Drupal standard. Cons: The hook is defined to work with plaintext, not HTML, because HTML is not inside the core, so by doing this, quite some modules that might be modifying a text mail might not work. The hooks will need to realize if they need to modify the content as HTML or plain text, not very flexible.

Line 217 (after applying the patch)

  foreach (module_implements('mail_alter') as $module) {
    $function = $module .'_mail_alter';
    // HTML alter
    $function($mailkey, $recipient, $subject, $body, $sender, $headers);
    // Plaintext alter
    $function($mailkey, $recipient, $subject, $text, $sender, $headers);
  }

2. Send $text through the mail_alter with the calling $mailkey and send $body through mail_alter with the $mailkey appended with -html. Something like:

Line 217 (after applying the patch)

  // Plaintext alter
  foreach (module_implements('mail_alter') as $module) {
    $function = $module .'_mail_alter';
    $function($mailkey, $recipient, $subject, $text, $sender, $headers);
  }
  // HTML alter
  foreach (module_implements('mail_alter') as $module) {
    $function = $module .'_mail_alter';
    $function($mailkey . '-html', $recipient, $subject, $body, $sender, $headers);
  }

Pros: Current modules will still work without much problem for plain text. Cons: Not very standard as this new key will need to be documented and probably handled by any module that's wanting to modify the HTML part of the mail as well.

3. Create the mimemail_alter hook and launch both hooks for different parts of the calling arguments, this is not difficult to do at all, something like this:
Line 217 (after applying the patch)

  // Plaintext alter
  foreach (module_implements('mail_alter') as $module) {
    $function = $module .'_mail_alter';
    $function($mailkey, $recipient, $subject, $text, $sender, $headers);
  }
  // mimemail alter with al the calling arguments of mimemail().
  foreach (module_implements('mimemail_alter') as $module) {
    $function = $module .'_mimemail_alter';
    $function($mailkey, $sender, $recipient, $subject, $body, $plaintext, $headers, $text, $attachments);
  }

Pros: Same as number 2 but we gain the possibility of altering all parts of the mimemail alltogether. Cons: Also similar cons to number 2.

hanoii’s picture

Version: 5.x-1.0 » 5.x-1.x-dev
Status: Closed (fixed) » Needs review
Anonymous’s picture

Subscribing.

hanoii’s picture

Version: 5.x-1.0 » 5.x-1.x-dev

Bumping this issue, any ideas or comments on #10? I am working on this one more time and would like to get some solution through the module. After re-looking at this, I think I am more in favor of option 1 in my previous post.

pillarsdotnet’s picture

I like number 3 best. If, as you say, "mail_alter" is defined to work with text, not html, then it's better to create a new hook than to change the behavior of one that is already documented.

hanoii’s picture

Yes, well, this is a bit of an assumption of my part. Drupal's core does not handle HTML emails, so when the mail_alter hook is called from drupal_mail() the only part available is the one single piece of text (plaintext). It's not really a definition of the mail_alter function but the only way available. As mimemail handles both plaintext and html it makes sense that both should be somehow called through a hook allowing altering. After a second thought, I think one might just work, but I am worried about the feature of mimemail that makes all email to be run through this module. With such a feature, there's a change that a hook waiting for a particular mail key to alter, ends up altering the HTML part in the wrong way.

Yes, maybe 3 is best.

sgabe’s picture

Priority: Critical » Normal
Status: Needs review » Closed (won't fix)

Closing issue, Drupal 5 is no longer officially supported.

tstackhouse’s picture

Version: 5.x-1.x-dev » 6.x-1.x-dev
Status: Closed (won't fix) » Active

Reopening this issue for 6.x, I've been working with this module to enable HTML mail and have been trying to do some troubleshooting, using the mail logging module and this presents an issue by not using mail_alter, the messages never get logged and I can't trace them.

sgabe’s picture

There is no such module as "Mail Logging", what module are you using exactly? I think that module uses Mime Mail in the wrong way.

tstackhouse’s picture

This is the module that I'm using: http://drupal.org/project/mail_logger

It uses hook_mail_alter and sets itself as the heaviest weighted module to grab anything that generates email just before it gets sent and since mime mail doesn't use mail_alter, it doesn't get caught.

sgabe’s picture

The culprit will be the module that generates the message. The proper use of Mime Mail is to first call drupal_mail() - which will call hook_mail_alter() - then mimemail() should be called with the returned $message.

tstackhouse’s picture

I see. Well than it comes right back to Mime Mail. The rules actions that it provides don't appear to get passed through drupal_mail().

sgabe’s picture

Version: 6.x-1.x-dev » 5.x-1.x-dev
Status: Active » Closed (won't fix)

Well... Sadly, but true. :) I'm going to close this again. Please, open a new issue with your findings. Patches are most welcome!

sgabe’s picture

@tstackhouse: I have opened a new issue for this, see #1090286: Prepare action messages with drupal_mail() to allow alteration.

tstackhouse’s picture

@sgabe: So subsribed.

gnindl’s picture

StatusFileSize
new2.75 KB

I'll give it another try, call hook_mail_alter manually from mimemail() function, please see patch.

gnindl’s picture

Version: 5.x-1.x-dev » 6.x-1.0
Status: Closed (won't fix) » Needs review

Change status

  • jerdavis committed b654c0c on 8.x-1.x
    #215310 by g10, allie, mathewbot: stripped html tags from body #263142...
jimmyko’s picture

I know it is a very old thread. But I would like to point out that there is mimemail_alter defined in mimemail_disable() and mimemail_uninstall() without any code to implement it in mimemail-7.x-1.x. I wonder if the development of this feature is still on-going or has been taken out with some reason. It is weird that I cannot see there is hook_update_70xx regarding to this change. Please put two cents in if you have information. Thanks.

tr’s picture

Status: Needs review » Closed (outdated)

I don't know why this issue is still open.

In Drupal 7 and Drupal 8 (and maybe early, but I'm not interested in researching how old unsupported versions of Drupal like D5 and D6 worked), hook_mail_alter() is invoked by Drupal core (via drupal_mail() in D7 and MailManager::mail() in Drupal 8) and should not be invoked directly from Mime Mail.

Bottom line is that this capability exists in Drupal core and there is nothing Mime Mail still needs to do or should do in regards to this issue.