There is a limit to what you can do to a mail with hook_mail_alter() because your module won't have access to the $params variable that was used to build the $message in the first place.
Example: emails sent by contact module. http://api.drupal.org/api/function/drupal_mail/7 invokes http://api.drupal.org/api/function/contact_mail/7. The latter has the full $params. If you want to modify the message using http://api.drupal.org/api/function/hook_mail_alter/7 you only get the $message to play with, so if you want to do anything major to it you will probably need to override the caller of drupal_mail(). In this case that would be http://api.drupal.org/api/function/contact_personal_form_submit/7 or http://api.drupal.org/api/function/contact_site_form_submit/7; seems this could be more efficient if $params were passed to the mail alter hooks.
Note that the $params concept (and http://api.drupal.org/api/function/hook_mail/7) only arrived with Drupal 6, so extending $params to the mail alter hooks seems a natural follow-up.
Comment | File | Size | Author |
---|---|---|---|
#17 | hook_mail_alter_doc7.patch | 3.23 KB | dale42 |
#13 | hook_mail_alter_doc6.patch | 3.01 KB | dale42 |
#11 | hook_mail_alter_doc5.patch | 2.5 KB | dale42 |
#8 | hook_mail_alter_doc4.patch | 2.51 KB | dale42 |
#6 | hook_mail_alter_doc3.patch | 2.51 KB | awmckinley |
Comments
Comment #1
gpk CreditAttribution: gpk commentedCloser inspection of http://api.drupal.org/api/function/drupal_mail/7 shows that $params is available as $message['params'], so this is just a matter of documentation in http://api.drupal.org/api/function/hook_mail_alter/7.
Comment #2
dale42Comment #3
dale42This is $message array in DRUPAL-7-0-UNSTABLE-8 when a user is created.
Comment #4
dale42Attached patch updates the function comments adding the 'params' and 'language' keys. Also updated some of the text to make it more compliant to the style guidelines.
Comment #5
dale42Emma Jane did a quick look and gave feedback via IRC. Newly updated text:
Comment #6
awmckinley CreditAttribution: awmckinley commentedI noticed that "mesage" is misspelled on the third line, and there seems to be a little line-break inconsistency. Otherwise, it makes perfect sense to me!
Here's the patch with "message" and line-breaks fixed.
and
to match "'id':" line above.
Comment #8
dale42Rerolled patch with corrected text
Comment #9
awmckinley CreditAttribution: awmckinley commentedWhoops! Sorry about that.
Comment #10
boombatower CreditAttribution: boombatower commented"site footer" seems odd, maybe just "footer".
Seems to be an extra * before modify.
Comment #11
dale42Good catch, errant * removed. Text updated. 5th time a charm?
Comment #12
gpk CreditAttribution: gpk commentedWell if we are being picky about layout etc... ;)
The initial function description should be on one line.
Then any additional info/examples can be in a block underneath.
This makes the layout better on pages such as http://api.drupal.org/api/file/modules/system/system.api.php/7.
"for example: add a common footer, an additional message header, or modify the message text."
I find this a bit confusing, because "message header" refers to the email headers, whereas "add a common footer" presumably means add text to the bottom of the body, i.e. is a specific example of modifying the message text. This was less confusing in the original IMO.
The problem with indentation in the original was merely that the first "bullet" line was missing a space. Otherwise it was all correct.
Suggest changing "An id identifying the message." to "The message identifier"
"Message body line endings are formatted for RFC 2822 compliance."
We have lost the important point that Drupal does the line ending formatting.
TBH I'm wondering whether the textual changes are worth doing in this issue, since we are now diverging from the documentation in drupal_mail(), on which the comments here seem to have been based originally, and also have diverged from the original intent of this issue.
Would it be better just to fix the original $params question, and the simple layout/formatting issue with the indent of the first bullet, and then pick up any textual improvements in a separate issue (and perhaps look at the documentation of hook_mail() at the same time)?
That might move things along more quickly.
Comment #13
dale42@gpk: Regarding the initial function description being one line. I checked the existing api doc and there is no consistency, and I can't find a writing guideline reference. If you know where this is specified, would appreciate the URL. Since the first paragraph appears in the hook summary, I suspect the guideline is understandability. In this specific case, I agree with you.
Regarding simply adding the new parameters, it started that way and one of the doc team suggested further improvements so here we are :-)
Your comments made me take a closer look at the actual function. This description should be more accurate all round.
Comment #14
jhodgdonDoc header conventions are found here: http://drupal.org/node/1354
This documentation is good, and a vast improvement over the previous doc.
Comment #15
jhodgdonSorry, finger slipped.
Comment #16
webchickWow! Great job on this!! Some comments...
This sounds scary. Could you explain this more? Under what circumstances would a contrib module not be calling drupal_mail()?
I actually think the old text was more helpful here. Or maybe not helpful, but it gave me a clue where to start looking.
Since this is the most important aspect of this function, we should probably take the opportunity to document it well here.
What sort of parameters?
This review is powered by Dreditor.
Comment #17
dale42I wouldn't attempt to second-guess a contrib author's decision process on why they did or didn't use drupal_mail()! :-) The text was added to make it clear there could be situations where messages could be sent without using hook_mail()/drupal_mail() Paragraph updated to read:
For "id" I've added the sentence, "Look at module source code or drupal_mail() for possible id values.", back in.
For "params" I've used a variation of the language used in the hook_mail() description: "'params': An array of optional parameters supplied by the caller of drupal_mail() that is used to build the message before hook_mail_alter() is invoked." If params is going to be documented, I'd recommend documenting it in hook_mail().
Comment #18
Dries CreditAttribution: Dries commentedI read through this and it looked great. Committed.