Email to SMS custom scripts

In this tutorial we are going to explain how EMAIL to SMS function works and how to build a custom gateway.
The gateway downloads every 30 seconds the email messages from the mailbox specified in the configuration.

Use Email to SMS gateway function

The Email to SMS gateway feature require only 4 configuration parameters. On the left menu click on configuration and take a look to the last section on bottom.




For example we configure the email retrieval of This e-mail address is being protected from spam bots, you need JavaScript enabled to view it In the first field you have to put the server that host the email account designed for SMS operations. (example. mail.kdev.it) In the second filed you can specify wich protocol to use for mail retrieval between POP3 and IMAP. (Default is POP3) In the third field you have to type the login name for accessing the email account. (example. gsm) In the fourth field you have to put the password for access the mailbox.


Click on save to apply changes and proceed enabling the gateway engine on the control panel screen.


Now that the gateway engine is started, if you send a message to This e-mail address is being protected from spam bots, you need JavaScript enabled to view it , using the following Synatx: Subject: TOsms From: To: 39349000111222 Text of your SM message
The e-mail messages will be automatically diverted to the phone number 39349000111222. A screencast that demostrate this functionality is available here.

The architecture is based on FETCHMAIL + PROCMAIL.

Fetchmail is the program that downloads the emails, procmail is the program used to filter it. A predefined set of procmail rules is saved into your SMS FoxBox /etc/ folder in the file procmailrc.
The default rule is  explainded below:

SHELL=/bin/sh
VERBOSE=OFF
MAILDIR=/mnt/flash/root/procmail
DEFAULT=/mnt/flash/spool/mail/mails
LOGFILE=/mnt/flash/spool/mail/procmail.log

:0
*  ^Subject:.*TOsms
{
:0 b:  mail.body.lock
| /etc/sms/scripts/email2sms
}

 

In the first part, some environment variables are declared, the "TRUE" rule  start with :0 and we define the subject that the email message should have to be diverted to the SMS engine. Feel free to change the ^Subject:.*WHATYOUWANT, if you leave the ^Subject:.* all mails will be forwarded as SMS.
In the second part :0 b: we ask procmail to extract the WHOLE body of the email and forward it as SMS.  Thanks to some users experience we noticed that with Lotus  Notes the body of the email is altered with some extra carriage return so please:
if YOU ARE A LOTUS NOTES USER, update the line
| /etc/sms/scripts/email2sms
with
| grep -v ^$ | sed '/To:/G'  | /etc/sms/scripts/email2sms

If you want to modify how the email message is processed, you have to learn some BASE RULES of procmail that are well described on this UNOFFICIAL WEBSITE.

Here I will show you a rule to divert an email message "catching" the number from the email sender and the message from the body of the email.  So if you send an email FROM: This e-mail address is being protected from spam bots, you need JavaScript enabled to view it to the defined mailbox (used by fetctmail and setted up on the profile of your box) the message will be sent to 393494603237.

SHELL=/bin/sh
VERBOSE=OFF
MAILDIR=/mnt/flash/root/procmail
DEFAULT=/mnt/flash/spool/mail/mails
LOGFILE=/mnt/flash/spool/mail/procmail.log
#OUTFILE="/mnt/flash/spool/smsgw.out.XXXXXX"
OUTFILE=$(/bin/mktemp /mnt/flash/spool/outgoing/smsgw.out.XXXXXX)

:0
*  ^Subject:.*TOsms
{
:0
* ? OUTFILE=$(/bin/mktemp /mnt/flash/spool/outgoing/smsgw.out.XXXXXX); echo $OUTFILE > /var/temp.txt; echo "From: MYNAME" >$OUTFILE; /etc/procmail/formail -t -x"From:" | awk 'BEGIN { FS = "<" } ; {print $2}' | awk 'BEGIN { FS = "@" } ; {print "To: "$1}' >> $OUTFILE

:0 b:  mail.body.lock
| OUTFILE=`/bin/cat /var/temp.txt`; echo "" >> $OUTFILE; /bin/cat  |grep -v ^$ | sed '/To:/G' >> $OUTFILE
}


Put this script in /etc/procmailrc and restart the EMAIL TO SMS Gateway to check it.

Another interesting  feature should be to send the SUBJECT of the email instead of the body as text message. To perform this simply  edit the script as follows:

SHELL=/bin/sh
VERBOSE=OFF
MAILDIR=/mnt/flash/root/procmail
DEFAULT=/mnt/flash/spool/mail/mails
LOGFILE=/mnt/flash/spool/mail/procmail.log
#OUTFILE="/mnt/flash/spool/smsgw.out.XXXXXX"
OUTFILE=$(/bin/mktemp /mnt/flash/spool/outgoing/smsgw.out.XXXXXX)

:0
*  ^Subject:.*TOsms
{
:0
* ? OUTFILE=$(/bin/mktemp /mnt/flash/spool/outgoing/smsgw.out.XXXXXX); echo $OUTFILE > /var/temp.txt; echo "From: MYNAME" >$OUTFILE; /etc/procmail/formail -t -x"To:" | awk 'BEGIN { FS = "<" } ; {print $2}' | a
* ? OUTFILE=`/bin/cat /var/temp.txt`; echo "" >> $OUTFILE; /etc/procmail/formail -t -x"Subject:" |grep -v ^$ | sed '/To:/G' >> $OUTFILE
   # | egrep -i "@kdev\.it" >> /mnt/flash/test.txt
}

Obviously, with procmail you can process each header of the email and build the rules for your purposes. For example exract the To: header instead of the from header or check that the body does not contain some words (moderation), this is the power of Open Source, the power of SMS FoxBox!