The example code in mail.inc suggests that a mail template should set the body as a string rather than an array.
$message['body'] = t("Dear !username\n\nThere is new content available on the site.", $variables, $language->language);
This is bad practice, and should be changed to make the $message['body'] in to an array so that other modules can attach additional content to the outbound emails.
$message['body'][] = t("Dear !username\n\nThere is new content available on the site.", $variables, $language->language);
Modules such as mollom and webform (from a quick check) already expect $messages['body'] to be an array. Other modules such as authorcontact have followed the above example code from the api.drupal.org site and thus cause mollom.module to crash due to $message['body'] not being an array.
A spot check on other core modules indicates this rule of using an array is already obeyed, so I presume it is just a documentation fix required in mail.inc
Comment | File | Size | Author |
---|---|---|---|
#9 | durpal-mail-D6.patch | 788 bytes | andypost |
#2 | drupal_455172_2.patch | 806 bytes | RoboPhred |
Comments
Comment #1
brianV CreditAttribution: brianV commentedSetting as a novice issue
Comment #2
RoboPhred CreditAttribution: RoboPhred commentedJumping into core.
The documentation already mentions that the message body should be an array...
...so it looks that only the example code needs to be changed.
Comment #3
RoboPhred CreditAttribution: RoboPhred commentedComment #4
brianV CreditAttribution: brianV commentedLooks good!
Comment #5
Dries CreditAttribution: Dries commentedCommitted to CVS HEAD. Thanks!
Comment #6
andypostTake a closer look to drupal_mail()
body can be an array and a plain text - it's not generalized, suppose bug?
So this docs are not valid!
Comment #7
RoboPhred CreditAttribution: RoboPhred commentedIs there any case where a module should explicitly deny other modules from adding content to the message? I feel that the documentation is correct and that only an array should be accepted so that modules will always play nice with each other.
It's a very minor change anyway, the coder module could easily take care of it when converting 6-7.
I will roll a patch for whatever is decided, I'm just the novice here.
Comment #8
RoboPhred CreditAttribution: RoboPhred commentedAPI site also needs to be updated.
http://api.drupal.org/api/function/drupal_mail/6
Comment #9
andypostSo here patch to change this for D6 at api
Comment #11
andypostforget to change version
Comment #12
andypostJust a tag
Comment #13
RoboPhred CreditAttribution: RoboPhred commentedComment #14
andypostComment #15
jhodgdonThe patch on #9 above applies cleanly to the D6 branch, and in my opinion should be applied. It conforms to the change already applied to D7.
Comment #16
Gábor HojtsyCommitted this doc change to Drupal 6. However I believe that since the core API allows either string or array, modules should be able to work with both. Those altering email can still alter the string to be an array and go on with that. At least in Drupal 6, it is not an option to convert to supporting arrays only.
Comment #17
Dave ReidMarked #448908: Only allow $message['body'] to be an array in drupal_mail() / hook_mail_alter() as a duplicate of this issue.
Comment #18
Dave ReidWe still need to fix drupal_mail() in 7.x:
$message['body'] = is_array($message['body']) ? drupal_wrap_mail(implode("\n\n", $message['body'])) : drupal_wrap_mail($message['body']);
Comment #22
Dave ReidRe-opening to fix drupal_mail() to only accept arrays.
Comment #23
jhodgdonThe original issue filed here has been fixed in both Drupal 6 and Drupal 7.
If you'd like to change the API to only allow arrays, can you file a separate issue against Drupal 7?