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?
| Comment | File | Size | Author |
|---|---|---|---|
| #25 | mimemail-mail-alter-1.0-patched2.patch | 2.75 KB | gnindl |
| #6 | mimemail-call_mail_alter2.patch | 1.06 KB | matthewbot |
| #2 | mimemail-call_mail_alter.patch | 771 bytes | liquidcms |
| #1 | mimemail-add_mail_alter.patch | 27.52 KB | liquidcms |
Comments
Comment #1
liquidcms commentedwell 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
Comment #2
liquidcms commentedjust realized that patch got a little messed up.. here's a cleaner one.
Comment #3
pillarsdotnet commentedSubscribe, plus title change for better searching
Comment #4
allie mickaInteresting. 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?
Comment #5
matthewbot commentedWell, 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().
Comment #6
matthewbot commentedupdated this, I still think its useful.
Comment #7
jerdavisAfter further review and testing, this has been commited. Thank you Matthewbot!
Comment #8
liquidcms commentedwhoo 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?
Comment #9
Anonymous (not verified) commentedAutomatically closed -- issue fixed for two weeks with no activity.
Comment #10
hanoiiSorry 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)
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)
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)
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.
Comment #11
hanoiiComment #12
Anonymous (not verified) commentedSubscribing.
Comment #13
hanoiiBumping 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.
Comment #14
pillarsdotnet commentedI 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.
Comment #15
hanoiiYes, 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.
Comment #16
sgabe commentedClosing issue, Drupal 5 is no longer officially supported.
Comment #17
tstackhouse commentedReopening 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.
Comment #18
sgabe commentedThere is no such module as "Mail Logging", what module are you using exactly? I think that module uses Mime Mail in the wrong way.
Comment #19
tstackhouse commentedThis 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.
Comment #20
sgabe commentedThe 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.
Comment #21
tstackhouse commentedI 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().
Comment #22
sgabe commentedWell... Sadly, but true. :) I'm going to close this again. Please, open a new issue with your findings. Patches are most welcome!
Comment #23
sgabe commented@tstackhouse: I have opened a new issue for this, see #1090286: Prepare action messages with drupal_mail() to allow alteration.
Comment #24
tstackhouse commented@sgabe: So subsribed.
Comment #25
gnindl commentedI'll give it another try, call hook_mail_alter manually from mimemail() function, please see patch.
Comment #26
gnindl commentedChange status
Comment #28
jimmyko commentedI know it is a very old thread. But I would like to point out that there is
mimemail_alterdefined inmimemail_disable()andmimemail_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 ishook_update_70xxregarding to this change. Please put two cents in if you have information. Thanks.Comment #29
tr commentedI 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.