php mail function overloading or why we shouldn't use mb_send_mail as sending mail function.

As a programmer I have a nice cool helpers - classes I've written to help me do my daily work.
Such class is my Mailer class which I use almost daily.
It has all kind of fancy options and can do all kind of stuff - from sending simple mail to just setting an array with files which should be attached in the mail.
It used to work very good until today.
I was very surprised when I saw a broken email coming from it.
I've investigated a few minutes what has changed and I've found that the problem
I've set
mbstring.func_overload = 7
according to the description in file:
; For example, 7 for overload everything.
; 0: No overload
; 1: Overload mail() function
; 2: Overload str*() functions
; 4: Overload ereg*() functions
it turned out that when set to 7 instead of using mail() function, php starts using mb_send_mail.
mb_send_mail works only with base64 emails.
It automatically encodes the body and subject (but not the sender wtf?) and it breaks mail ugly!
If you manually add/set headers do not put \r\n at the very end of the string, mb_send_mail adds two headers after yours:
Mime-Version: 1.0
Content-Transfer-Encoding: BASE64
and if you put one new line(\r\n) to separate the headers from body then you get the email totally messed up, because the base64 transfer won't apply and you'll get encoded string as plain text or html or whatever content-type you set previously.
After I've lost another 20 minutes figuring out this, I've finally achieved receiving my email as it should be... well... almost.
When the mail contained only ASCII symbols everything was ok, but when I tried to send email in utf8 Bulgarian... omfg?!
The encoding was broke and for some reason the UTF8 symbols looked like you have page in UTF8 but your browser is set to WINDOWS-1251....
WTF?!!! I've read few posts that said they never got it successfully working fine with mb_send_mail.
It took me some time to find something very useful - there is an undocumented php function: mb_orig_mail (http://osdir.com/ml/php.internationalization/2003-01/msg00054.html).
When you use overloaded function you can fall back to the standart mail function by calling this one.
So here is what I did to solve my problem - I use exactly the same class that used to work until the overload get active.
But when it comes to sending I simply check if the mail function is overloaded and if yes, then call the mb_orig_mail.
Here is the actual code:

$mailFunct = 'mail';
if (ini_get('mbstring.func_overload') == 1 || ini_get('mbstring.func_overload') > 4) {
$mailFunct = 'mb_orig_mail';
}
if(! $mailFunct($tostr, $subject, $tmpBody, $tmpHeaders) ) {
echo 'sent';
} else {
echo 'error sending';
}

It's as simple as that.
Hope that helps to anyone.
Comments are welcome.

Trackbacks

Trackback specific URI for this entry

This link is not meant to be clicked. It contains the trackback URI for this entry. You can use this URI to send ping- & trackbacks from your own blog to this entry. To copy the link, right click and select "Copy Shortcut" in Internet Explorer or "Copy Link Location" in Mozilla.

No Trackbacks

Comments

Display comments as Linear | Threaded

No comments

Add Comment

You can use [geshi lang=lang_name [,ln={y|n}]][/geshi] tags to embed source code snippets.
Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.
To leave a comment you must approve it via e-mail, which will be sent to your address after submission.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.