playSMS Forum

MailSms encoding

Hi!
I’ve using playsms 1.3.1 with latest smstools on debian 8.2

Compose sms works fine, even with russian cyrillic (unicode option on).

Now need to use cyrillic symbols in emails, with mailsms module, but i can’t find a code to convert it.

Hint: it’s realy important to patch these things in future :slight_smile:

Firstly, i found a problem with mime-encoded (ru) sender. My solution:

/html/playsms/plugin/feature/mailsms/fn.php
$email_sender = trim($overview[0]->from);
add below
$email_sender = iconv_mime_decode($email_sender,0,“UTF-8”);

But this (and simmilar) trick don’t work with $email_body or $message.
And, i think, this plugin send sms not thru smstools gateway module with “unicode=0” and convertation.
How to enable convertation?

P.S. webservices send cyrillic with unicode=1.

yes, current mailsms handler does not handle unicodes

I’ll made some changes in a few days, and let you know for tests

anton

Thank you, can’t wait!

I am waiting for a solution too :slight_smile:

Anton, how do you think when new solution will be ready to use?

Hello! Since month, no news about this problem :frowning:

I was faster:) I have solution. It’s working.

in …/html/playsms/plugin/feature/mailsms/fn.php
(//org means the original line in the file)

after

// NikeRossXP
// convert to UTF-8
$email_subject = trim($overview[0]->subject);
$email_subject = iconv_mime_decode($email_subject,0,“UTF-8”);
$email_sender = trim($overview[0]->from);
$email_sender = iconv_mime_decode($email_sender,0,“UTF-8”);

add

// JustL
// check unicode
if (strlen($email_subject) != strlen(utf8_decode($email_subject)))
{
$unicode = 1;
}
else $unicode = 0;

add option [text / flash] in subject email

// text or flash
$type = $f[2];
// org $c_message = preg_split("/[\s]+/", $email_subject, 3);
$c_message = preg_split("/[\s]+/", $email_subject, 4);

// org $message = $c_message[2];
$message = $c_message[3];

and replace the line
// org list($ok, $to, $smslog_id, $queue, $counts, $sms_count, $sms_failed) = sendsms_helper($sender_username, $sms_to, $message, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, $reference_id);

on
list($ok, $to, $smslog_id, $queue, $counts, $sms_count, $sms_failed) = sendsms_helper($sender_username, $sms_to, $message, $type, $unicode, ‘’, ‘’, ‘’, ‘’, $reference_id);

that’s all :slight_smile:

Now, if you are sending mail in the subject line use the syntax:
Email subject: [username] [PIN] [text/flash] [message]
Unicode is recognized automatically.

1 Like

Hi,

can you test these for me:
don’t use iconv_mime_decode() and use $unicode = core_detect_unicode($email_subject); to detect unicode and set $unicode

ref:

anton

Thanks, but don’t work. Even $unicode = 1; don’t work.

mailsms function is really need for detect charset of email, many e-mail clients send differently encoded text.

you mean you need the iconv_mime_decode() ?

ok use that, but detect using core_detect_unicode()

anton

i mean, that zabbix sends base64, thunderbird sends other and etc. could you add detect charset feature? And, $unicode isn’t work…

please confirm, it would help me understand better if you can confirm this:

if you only change your code this part:

// JustL
// check unicode
if (strlen($email_subject) != strlen(utf8_decode($email_subject)))
{
$unicode = 1; 
}
else $unicode = 0;

with this single line:

$unicode = core_detect_unicode($email_subject);

will it work just like your original code ?

anton

I’m really sorry, i forget to say, that i use a fork for subject=“to”, body = message. All the code, with base64 decode check:

<?php

/**
 * This file is part of playSMS.
 *
 * playSMS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * playSMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with playSMS. If not, see <http://www.gnu.org/licenses/>.
 */
defined('_SECURE_') or die('Forbidden');

function mailsms_hook_playsmsd() {
    global $core_config;
    
    // get global mailsms registry data
    $items_global = registry_search(0, 'features', 'mailsms');
    
    // fetch interval
    $c_fetch_interval = (int) $items_global['features']['mailsms']['fetch_interval'];
    $c_fetch_interval = ($c_fetch_interval > 10 ? $c_fetch_interval : 60);
    if (!core_playsmsd_timer($c_fetch_interval)) {
        return;
    }
    
    // _log('fetch now:'.$now, 2, 'mailsms_hook_playsmsd');
    

    $enable_fetch = $items_global['features']['mailsms']['enable_fetch'];
    $protocol = $items_global['features']['mailsms']['protocol'];
    $port = $items_global['features']['mailsms']['port'];
    $server = $items_global['features']['mailsms']['server'];
    $username = $items_global['features']['mailsms']['username'];
    $password = $items_global['features']['mailsms']['password'];
    
    if (!($enable_fetch && $protocol && $port && $server && $username && $password)) {
        return;
    }
    
    // _log('fetch uid:' . $uid, 3, 'mailsms_hook_playsmsd');
    

    $param = 'mailsms_fetch';
    $is_fetching = (playsmsd_pid_get($param) ? TRUE : FALSE);
    if (!$is_fetching) {
        $RUN_THIS = "nohup " . $core_config['daemon']['PLAYSMS_BIN'] . "/playsmsd playsmsd once " . $param . " >/dev/null 2>&1 &";
        
        // _log('execute:' . $RUN_THIS, 3, 'mailsms_hook_playsmsd');
        shell_exec($RUN_THIS);
    }
}

function mailsms_hook_playsmsd_once($param) {
    if ($param != 'mailsms_fetch') {
        return;
    }
    
    // get username
    $username = user_uid2username($uid);
    
    // _log('fetch uid:' . $uid . ' username:' . $username, 3, 'mailsms_hook_playsmsd_once');
    
    $items_global = registry_search(0, 'features', 'mailsms');
    
    $enable_fetch = $items_global['features']['mailsms']['enable_fetch'];
    if (!$enable_fetch) {
        return;
    }
    
    $ssl = ($items_global['features']['mailsms']['ssl'] == 1) ? "/ssl" : "";
    $novalidate_cert = ($items_global['features']['mailsms']['novalidate_cert'] == 1) ? "/novalidate-cert" : "";
    $email_hostname = '{' . $items_global['features']['mailsms']['server'] . ':' . $items_global['features']['mailsms']['port'] . '/' . $items_global['features']['mailsms']['protocol'] . $ssl . $novalidate_cert . '}INBOX';
    $email_username = $items_global['features']['mailsms']['username'];
    $email_password = $items_global['features']['mailsms']['password'];
    
    // _log('fetch ' . $email_username . ' at ' . $email_hostname, 3, 'mailsms_hook_playsmsd_once');
    
    // open mailbox
    $inbox = imap_open($email_hostname, $email_username, $email_password);
    
    if (!$inbox) {
        $errors = imap_errors();
        foreach ($errors as $error ) {
            
            // _log('error:' . $error, 3, 'mailsms_hook_playsmsd_once');
        }
        return;
    }
    
    $emails = imap_search($inbox, 'UNSEEN');
    if (count($emails)) {
        rsort($emails);
        foreach ($emails as $email_number ) {
        $overview = imap_fetch_overview($inbox, $email_number, 0);
        $email_subject = trim($overview[0]->subject);
        $email_sender = trim($overview[0]->from);
    $email_sender = iconv_mime_decode($email_sender,0,"UTF-8");
        $email_body = trim(imap_fetchbody($inbox, $email_number, 1));

function validBase64($check_string){
       $decoded = base64_decode($check_string, true);
       // Check if there is no invalid character in strin
        if (!preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $check_string)) return false;

        // Decode the string in strict mode and send the responce
         if(!base64_decode($check_string, true)) return false;

        // Encode and compare it to origional one
        if(base64_encode($decoded) != $check_string) return false;

        return true;
   }
    

    $check_string = $email_body;
    if (validBase64($check_string)){

$email_body=base64_decode($email_body);
}
    $email_body = imap_qprint($email_body);
     $email_body = mb_convert_encoding($email_body, "UTF-8", "auto");
    $unicode = 1;

        _log('email from:[' . $email_sender . '] subject:[' . $email_subject . '] body:[' . $email_body . ']', 3, 'mailsms_hook_playsmsd');

        $e = preg_replace('/\s+/', ' ', trim($email_body));
        $f = preg_split('/ +/', $e);
        $sender_username = str_replace('@', '', $f[0]); // in case user use @username
        $sender_pin = $f[1];
        //$message = str_replace($sender_username . ' ' . $sender_pin . ' ', '', $email_subject);
        $c_message = preg_split("/[\s]+/", $email_body, 3);
        $message = $c_message[2];


        $sender = user_getdatabyusername($sender_username);

        if ($sender['uid']) {
            $items = registry_search($sender['uid'], 'features', 'mailsms_user');
            $pin = $items['features']['mailsms_user']['pin'];
            if ($sender_pin && $pin && ($sender_pin == $pin)) {
                if ($items_global['features']['mailsms']['check_sender']) {
                    preg_match('#\<(.*?)\>#', $email_sender, $match);
                    $sender_email = $match[1];
                    if ($sender['email'] != $sender_email) {
                        _log('check_sender:1 unknown sender from:' . $sender_email . ' uid:' . $sender['uid'] . ' e:' . $sender['email'], 3, 'mailsms_hook_playsmsd_once');
                        continue;
                    }
                }
            } else {
                _log('invalid pin uid:' . $sender['uid'] . ' sender_pin:[' . $sender_pin . ']', 3, 'mailsms_hook_playsmsd_once');
                continue;
            }
        } else {
            _log('invalid username sender_username:[' . $sender_username . ']', 3, 'mailsms_hook_playsmsd_once');
            continue;
        }

        // destination numbers is in array and retrieved from email body
        // remove email footer/signiture
        $sms_to = preg_replace('/--[\r\n]+.*/s', '', $email_subject);
        $sms_to = explode(',', $sms_to);
            
            // sendsms
            if ($sender_username && count($sms_to) && $message) {
                _log('mailsms uid:' . $sender['uid'] . ' from:[' . $sender_email . '] username:[' . $sender_username . ']', 3, 'mailsms_hook_playsmsd_once');
                list($ok, $to, $smslog_id, $queue, $counts, $sms_count, $sms_failed) = sendsms_helper($sender_username, $sms_to, $message, $unicode, '', '', '', '', $reference_id);
            }
        }
    }
    
    // close mailbox
    imap_close($inbox);
}

try this line
list($ok, $to, $smslog_id, $queue, $counts, $sms_count, $sms_failed) = sendsms_helper($sender_username, $sms_to, $message, $unicode, ‘’, ‘’, ‘’, ‘’, $reference_id);
replace on
list($ok, $to, $smslog_id, $queue, $counts, $sms_count, $sms_failed) = sendsms_helper($sender_username, $sms_to, $message, ‘’, $unicode, ‘’, ‘’, ‘’, ‘’, $reference_id);

@JustL

Thank you very much, this works for me, too.
Now I can use “Umlaute” like ä, ö, ü in my mails.

But why do you made the differentiation between text and flash?
I used “text” für my tests and it worked.

This option [text / flash] in the subject of the email is an addition.
It is equalization of functionality that is available from web application and webservice level.
If “flash” is used an SMS will appear directly on the main screen without user interaction and is also not automatically stored in the inbox.
Now you can send flash sms via email too :slight_smile:

@anton

Thank you so much, it’s worked!

I think you should commit this functional. And, maybe, all my code :smiley:

Hi,

Am trying to configure mail2sms too.

0000000@domain.com to kannel to send SMS with subject as text
or
user@domain.com to mailserver (dovecote and postfix) within the same playsms server and route to the user mobile number to kannel to send the subject.

Thanks.