Changing the way mail2sms works

For my implementation I needed the mail parsed by PlaySMS to be in a different format than it exists currently.
Current implementation is that the Subject holds the @user, PIN and then the message and the Body contains the number(s) to dial.

I needed the opposite so I edited fn.php from:

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_body = trim(imap_fetchbody($inbox, $email_number, 1));
		
		_log('email from:[' . $email_sender . '] subject:[' . $email_subject . '] body:[' . $email_body . ']', 3, 'mailsms_hook_playsmsd');
		
		$e = preg_replace('/\s+/', ' ', trim($email_subject));
		$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_subject, 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_body);
		$sms_to = explode(',', $sms_to);

to

        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_body = trim(imap_fetchbody($inbox, $email_number, 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);
1 Like

Hi Scott,

I also want to use this format too. Since the content is limited in “email subject” format
Can you confirm that your code work with no error?

Regard,
Long

I’ve been using it in production for a few months now with no issues, so yes, it works.

We’ve just changed to API webservices and are deprecating the email2sms due to volume (3500+ per day) so we won’t be using it anymore.

Also note that I stripped authentication from it so we locked down the email service to only accept emails from one sender/ip.

1 Like

Thank you. That is what i’ve searched for. Can yout tell me how an email should look now.
What do I have in Subject now. Did you only changed Message and Phone Number?
I see the difference in the code but i’m not very famililar with php.

Hi Bernhard,
The subject will be the destination number. Depending on whether you strip or add a plus sign in the playsms configuration, you will send with or without + from the email generator.
The body will be the message. Make sure that your email generator sends in plain text, not HTML. Also if you are sending UTF8 codes make sure that your transport can handle the character set. I’m using smstools and it is set in the config.

I also wanted to note that we changed from email to webservices API because email was too slow. We are using ngrok.io to vpn the api into the box since it is behind a NAT firewall.

Hi Scott,

Thank you for your immediate answer. I have another questions

  1. Is it possible to send the message to a Phonebook group instead of one Number.
  2. Could you help me to get a format like “User or Group or only Number” and “PIN” in subject and only the message in body.