Current implementation of drupal_wrap_mail uses PHP wordwrap function, which wraps lines only bytewise. This produces incorrectly wrapped narrow results if text contains lots of 2-byte encoded (or longer) utf-8 characters. For example, all Cyrillic letters are encoded with 2 bytes in utf-8 and Russian text will is wrapped at about 38 characters instead of 77.

Steps to reproduce:
1. Install anything that sends emails from drupal.
2. Insert long text in Russian to be sent (you may copy-paste some from here).
3. Send the email.
4. Observe a result with very short-wrapped lines.

The attached patch tries to fix the problem with a custom implementation of wordwrap function which uses mbstring functions to wrap the lines properly regardless of utf-8 byte encoding.

Comments

brianV’s picture

Version: 6.9 » 7.x-dev
Status: Active » Needs work

I don't think mb_wrap is a good title for this function. Perhaps something like drupal_wordwrap should be used so it's clear this is part of the Drupal API.

Also, This definately needs more comments on the code, including a header comment.

Finally, place it above the comment for _drupal_mail_wrap_line() instead between the comment and the other function.

catofcheshir’s picture

subscribe.

maximn’s picture

Status: Needs work » Needs review
StatusFileSize
new3.62 KB

Status: Needs review » Needs work
OnkelTem’s picture

Issue summary: View changes
Status: Needs work » Needs review
StatusFileSize
new3.44 KB

Re-rolled patch from #3 for 7.34

Status: Needs review » Needs work
OnkelTem’s picture

Status: Needs work » Needs review
StatusFileSize
new3.39 KB

Sorry, there was wrong path.

Status: Needs review » Needs work
OnkelTem’s picture

Status: Needs work » Needs review
StatusFileSize
new2.87 KB

The patch stopped applying after 7.39 so I manually edited it a bit. Unfortunately it is failing some tests, but I have no time to fix this.

Status: Needs review » Needs work
maximpodorov’s picture

Status: Needs review » Needs work
hijera’s picture

Re-worked patch for Drupal 7.64

Status: Needs work » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.