Updated: June 20, 2007: RTL HTML Email now possible with my patch to the new htmlmail module.
Requirements for Drupal 6 core RTL email support - Request for comments
It has been 8 months since I've implemented a Drupal 4.7 user.module patch to support RTL emails (attached).
My next target is to ask for the RTL/i18n community help to update the code and migrate those proven changes into the Drupal 6 core. This will save tons of later headache.
Here are the basic requirements - I will need your feedback on the concept (implementation is quite straightforward).
Why do we need special treatment for RTL emails?
1.1. In a perfect world, all people would read RTL emails with RTL mail readers.
In practice, this is not the case. People read RTL emails on LTR readers, for various reasons, e.g.:
* Even though gmail has Hebrew support, chat is only available on the English interface, so if I want to see which of my gmail friends is online, I must use the english interface.
* People love Yahoo's new unlimited storage capacity, but then they must read RTL emails in an LTR direction.
1.2 In a perfect world, RTL email don't contain any English words.
In practice, this is not the case. Technical RTL emails contain many words in English. This causes the email to look terrible on RTL mail readers. Each English word in the middle of the line, breaks the line to two more parts, scrambled in an opposite direction.
Technology Considerations
Theoretically, there are two ways to go to get RTL email - CSS and HTML.
In a perfect world, CSS would be the ultimate answer.
However, in practice, CSS Email is not compatible and causes lots of trouble. GMail, Msn, Thunderbird, Outlook - all have different CSS implementations, if at all. Stylesheets are not supported at all. Some webmails don't crop the local CSS, since it's in the browser anyway, so sometimes it has effect, if it doesn't contradict the global CSS. But this has lots of variations.
HTML Email support is now the lowest common denominator for RTL email readers.
Virtually all mail readers now support html.
I have good experience with sending HTML formatted Hebrew emails since 2003.
This is the proven, compatible, solution.
To get RTL HTML email, all we need to do is to surround the text with a
<
div> tag with a 'dir=rtl' attribute.
Suggested implementation
Take a look at the new drupal_mail function found in includes/common.inc
The following requirements are sufficient to get RTL on all outgoing emails.
- The default message content type should be text/html, not text/plain (at least for RTL languages). See lines 3-6 and line 10 of the code below.
<?php
function drupal_mail($mailkey, $to, $subject, $body, $from = NULL, $headers = array()) {
$is_rtl = defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL; /* Same as drupal_get_css */
$encoding='text/plain'; if ($is_rtl){ $encoding='text/html;'}
$defaults = array(
'MIME-Version' => '1.0',
'Content-Type' => $encoding . ' charset=UTF-8; format=flowed',
'Content-Transfer-Encoding' => '8Bit',
'X-Mailer' => 'Drupal'
);
?> - Next, we should wrap the message in a div tag, with dir="rtl" , replacing newlines with
tags:
<?php
$message = '<div dir="rtl">' . str_replace("\n", '<br />', $message) . "</div>";
?> - Process the message with URL filter, since some HTML mail readers doesn't automatically highlight URLs.
URL filtering was done in version 4.7 in the following way (How to implement it? a new implementation is needed since URL filter is now embedded in core):
<?php
if (module_exist('urlfilter')) $message = urlfilter_filter ('process', 0, 300, $message);
?>
Notes
- Your comments are welcome.
- The filtering process exposes other new exiting possibilities for compatible HTML emails in general. Virtually, we can use any other drupal filter we want! I am sure that some LTR developers would like HTML emails to be an option for their emails a well, so I this might later become a config option - but I wouldn't want to hold the whole issue until such an issue is decided.
- The RTL email patch for Drupal 4.7 was quite simple - I am attaching it here for reference.
<?php
*** user.module 2006-10-31 11:31:19.163008880 +0200
--- /home/levavie/www/modules/user.module 2006-10-31 11:35:59.613373944 +0200
***************
*** 423,433 ****
** http://www.rfc-editor.org/rfc/rfc2646.txt
**
*/
return mail(
$mail,
! mime_header_encode($subject),
! str_replace("\r", '', $message),
! "MIME-Version: 1.0\nContent-Type: text/plain; charset=UTF-8; format=flowed\nContent-transfer-encoding: 8Bit\n" . $header
);
}
}
--- 423,434 ----
** http://www.rfc-editor.org/rfc/rfc2646.txt
**
*/
+ $message = '<div dir="rtl">' . str_replace("\n", '<br />', $message) . "</div>";
+ if (module_exist('urlfilter')) $message = urlfilter_filter ('process', 0, 300, $message);
return mail(
$mail,
! mime_header_encode($subject), $message,
! "MIME-Version: 1.0\nContent-Type: text/html; charset=UTF-8; format=flowed\nContent-transfer-encoding: 8Bit\n" . $header
);
}
}
?>My Questions
Please give feedback on the best place to put the abovementioned code, and give me the green light to go ahead with asking people to help with the patch.
Amnon
-
Professional: Drupal Search | Drupal Israel | Web Hosting Strategies
Personal: Hitech Dolphin: Regain Simple Joy :)

Comments
diff, HTML
A few comments:
Having said that, RTL emails in Drupal core would be nice (given that we have RTL themes, which still have some way to go). I am not sure that this is the proper way, there can be a lot of stuff in an email to simply break if you HTMLize it this way (ie. < chars, fixed width intended "drawings" and indentation like in drupal.org issue emails and so on)
Suggested architecture:
Hi Gabor,
I am still not on the implementation level. This was just requirements definition.
On a second thought, I think that the following three changes are sufficient and much more Generic, simple to implement, and have good benefit even for non-RTL developers:
Amnon
do we need HTML for this?
The most basic question is if we need HTML at all for this. Unicode has control chars with which we can signal that RTL text is coming. That should be sufficient for clients treating UTF as is (not converting them to some other encoding).
http://unicode.org/reports/tr9/#Directional_Formatting_Codes - Seems like we would do well with a "Right-to-Left Embedding" marker before and a "Pop Directional Format" marker after the email. This is not approriate in HTML email (http://www.w3.org/TR/unicode-xml/#Bidi) but otherwise it could work. These two are the markers, which could be tested.
$start = "\x20\x2B";
$end = "\x20\x2C";
Yes, we do need HTML
Yes, Gabor, we do need HTML.
Those separators are character level separators. They are used for embedding RTL text inside LTR text and vice versa, in order to display the characters on the right order on the screen. They are not intended to format a whole document, and they don't change the document direction, only the character order.
Regarding your previous questions:
hook_mail_alter() is clearly not the way to go. Quoting Boris Mann's comment on the 'Module black-hole revival suggestion:
drupal_mail() is used in so many places - I don't think it's wise to force the developer to install yet another module where we need a standard solution to this issue. That's why we are moving i18n to core. I think the whole point in drupal is about collaborating to make better solutions.
Yes, the HTML in the first example was intentionally a tag soup - I didn't want to be to abstract when explaining requirements, otherwise people won't understand. The time to abstract is after the basic need becomes clear.
However, here is a more concrete example. Here is how Drupal's registration email (and all other contrib module emails - ecommerce, organic groups, logintoboggan & others) looks when translated to my RTL language. Notice we were forced to break the translation such that each LTR text to be on a separate line, otherwise it would all mix up like in line 14.
test,
תודה על הרשמתך לאתר 'בניית אתרים בדרופל'.
אפשר עתה להיכנס לאתר בכתובת
http://www.drupal.org.il/user
תוך שימוש בכינוי ובסיסמה הבאים:
כינוי: test,
סיסמה: DfDUq5kAYu.
לאחר הכניסה, מומלץ לשנות את הסיסמה בכתובת זו:
http://www.drupal.org.il/user/507/edit.
החברות החדשה שלך באתר '%site' מאפשרת לך גם להיכנס לאתרים אחרים, מבוססי דרופל (כדוגמת http://www.drupal.org) ללא הרשמה. יש להשתמש בכינוי דרופל ובסיסמה הבאים:
כינוי: test@www.drupal.org.il,
סיסמה: DfDUq5kAYu.
בברכה,
צוות 'בניית אתרים בדרופל'.
RTL themes are advancing in parallel - volunteer themers promised to be available from June 25.
RTL Email now possible with my patch to the new htmlmail module
I've modified the new htmlmail module to allow custom HTML mail templates, and submitted it as a patch.
This allows me to customize the template to get the desired functionality of RTL emails (and many other customizations as well). Tested to work with GMail and Yahoo and my local mail client.
Still, core support would be nice - but for me the current situation is good enough for now so this issue may be considered closed from my point of view.
Sorry I've opened it as a group article - next time I hope to open it as an issue.
Amnon
some solution
Well, this at least provides some solution to RTL users. We already got a big patch (which is not progressing to a commit-ready state these days) to let Drupal send emails in the users language actually, which is still an important issue to fix for multilanguage sites (especially where LTR and RTL languages coexist). http://drupal.org/node/82499 I would not suggest anyone to extend that patch to include RTL support, as it is itself overly complex, and calls for a simplified direction.