[solved] Mail2sms : encoding problem


When I send sms by email, the french word “t’arrête” (without double quote !) is send by kannel (and received by the recipient) like this : “=?UTF-8?B?dCdhcnLDqnRl?=” (without double quote !)

In “Main configuration”, the french language is selected.

Thanks for help


The string received seems to have base64 encoded strings. I found and tried this fix (MailSms encoding) but it didn’t work for me.

Any idea of how to fix this?

At last I wrote this workaround. It can decode most of base64 and quoted-printable strings. Inaccentuated chars are sent well, except for diaerasis and circumflex accents. These are displayed as question marks but this due to kannel, not playsms.

The file to edit is /playsms/plugin/feature/mailsms/fn.php around line 104.

// Removed    
            // ori $e = preg_replace('/\s+/', ' ', trim($email_subject));
            // ori $email_subject = preg_replace('/\s+/', ' ', trim($email_subject));
// Added
$email_subject = preg_split('/\s+/', $email_subject);
$glue = ' ';
foreach($email_subject as $key => $part){
    if(strpos($part,'?B?') !== false){ // base64
        preg_match('#^=\?.+\?B\?([a-zA-Z0-9/\+]*={0,2})\?=$#', $part, $match);
        $email_subject[$key] = base64_decode($match[1]);
    else if(strpos($part,'?Q?') !== false){ // quoted-printable
        $glue = '';
        preg_match('#^=\?.+\?Q\?(.*)\?=$#', $part, $match);
        $email_subject[$key] = quoted_printable_decode($match[1]);
        $email_subject[$key] = str_replace('_', ' ', $email_subject[$key]);
$email_subject = implode($glue, $email_subject);
_log('Sujet : ' . $email_subject);

$f = preg_split('/ +/', $email_subject);

// Removed            
            // ori $f = preg_split('/ +/', $e);

This works well with Thunderbird, Gmail webmail, Firstclass client and so on. However there is no warranty this will work for all existing clients and webmails because some of them might use different and unhandled encoding patterns

better than nothin, I will just add this and I hope you or someone else will improve this later


did you add here (in between line 111 and 112):


My line numbers don’t match yours. I may have a slightly different file. But the code should go between the lines you quoted.

However, I went a bit deeper and I found this core php function I was not aware of :

php docs reads it is implemented since php5. However I tried it with php 5.3 and I got unexpected results. Upgrading to php 5.6 fixed the issue and it works well now.

So here is an updated version. Code has to be thrown after line 109, according to the file on Gihub.

            $email_body = trim(imap_fetchbody($inbox, $email_number, 1));
            _log('email from:[' . $email_sender . '] subject:[' . $email_subject . '] body:[' . $email_body . ']', 3, 'mailsms_hook_playsmsd');
$email_subject = iconv_mime_decode($email_subject);
_log('Decoded subject : '. $email_subject);
            $e = preg_replace('/\s+/', ' ', trim($email_subject));
            $f = preg_split('/ +/', $e);

This works like my previous workaround (circumflex and diaresis still display as question marks, still kannel issue).
Hope this will help…

so thats it, just those 2 lines ?

update mailsms/fn.php:


Only the first line is really useful, the second one is only here for logging and debugging purposes.

if (function_exists('iconv_mime_decode'))
iconv_mime_decode exists in php 5.3 but gives unexpected results. I can’t figure out why though.

Again, some inaccentuated chars will display as question marks, but at least, the sms will become readable.

Can you test the new fn.php file again, thanks, this file:


The new file fixes the issue. All chars display well now.