SMS gateway (from mail to sms)
Posted by mazet on Oct 26 2013 in Systeme
This gateway will allow us to send SMS via email using the following address scheme: smsto+YOURNUMBER@mydomain.com
Install a SMS gateway such as SMS Gateway (from APK Soft) on an Android phone which can connect to your LAN. Configure
- Listen for HTTP sens SMS commands
- Prevent CPU sleep mode
- Start SMS Gateway automatically after phone boot
- Long SMS messages: Sens all
- In HTTP settins
- Select a port for listen HTTP commands
- Set a password
Configure your DNS and your DHCP to name your phone such as smsbox
Create a user
Shell
adduser --disabled-login --disabled-password --gecos 'SMS Agent' --ingroup mail smsto |
We assume that your postfix server is confure as followed
Code
mailbox_command = procmail -a "$EXTENSION" | |
recipient_delimiter = + |
Put in /home/smsto/.procmailrc
Code
PATH=$HOME/bin:/usr/bin:/bin:/usr/local/bin | |
LOGFILE=$HOME/log.`date +%y-%m-%d` | |
LOCKFILE=$HOME/.lockmail | |
VERBOSE=yes | |
| |
SMSDEST=$1 | |
| |
:0 | |
{ | |
:0 c | |
| $HOME/bin/convert2sms.sh -p $SMSDEST | |
| |
:0 | |
Maildir/transmitted/ | |
} |
Then add following script in /home/smsto/bin/ as convert2sms.sh
Shell
#!/bin/bash | |
| |
# constants | |
MYDIR=$(dirname $0) | |
PROGNAME=$(basename $0) | |
| |
# configuration variables | |
CONFIG=$MYDIR/convert2sms.rc | |
DOMAINS=$(hostname -d | awk '{ print "@" $0 }') | |
[ -f "$CONFIG" ] && source $CONFIG | |
[ -z "$GATEWAY" -o -z "$PASSWORD" ] \ | |
&& { echo "No valid configuration"; exit 1; } | |
| |
# default variables | |
DRYRUN= | |
PHONE= | |
TEMPFILE=$HOME/tmp/smsto.$$ | |
| |
# urlencode function | |
function urlencode () { | |
string=$* | |
| |
#[ "$DRYRUN" ] && { echo $string; return; } | |
| |
for ((i = 0 ; i < ${#string} ; i++ )); do | |
c=${string:$i:1} | |
case "$c" in | |
[-_.~a-zA-Z0-9]) echo -n $c;; | |
*) printf '%%%02x' "'$c" | |
esac | |
done | |
} | |
| |
# log function | |
function log () { | |
[ "$DRYRUN" ] && echo -e "\033[1;34m$*\033[0;0m" \ | |
|| logger -i -t SMSTO "$*" | |
} | |
| |
# usage function | |
function usage () { | |
echo "usage: $PROGNAME [-h] [-p ..........]" | |
echo " -d: dryrun" | |
echo " -h: help message" | |
echo " -p: phone number" | |
exit $1 | |
} | |
| |
# get phone number from argument | |
while [ $# -ne 0 ]; do | |
case "$1" in | |
-d) DRYRUN=1;; | |
-h) usage 0;; | |
-p) shift; PHONE=$1;; | |
*) usage 1;; | |
esac | |
shift | |
done | |
[ "$PHONE" ] || usage 1 | |
| |
# clean up when done | |
trap "/bin/rm -f $TEMPFILE" 0 1 2 3 15 | |
| |
# get mail from standard input | |
while read -t 1 line; do | |
echo "$line" >> $TEMPFILE | |
done | |
[ -f "$TEMPFILE" ] || { echo "No input email"; exit 1; } | |
| |
# analyse message to get sender and subject | |
FROM=$(awk '$1 == "From:" { sub(/[^ ]*: */, ""); print }' $TEMPFILE) | |
SUBJECT=$(awk '$1 == "Subject:" { sub(/[^ ]*: */, ""); print }' $TEMPFILE) | |
MESSAGE=$(awk '/^$/,0 { if (/^--/) exit; if (!/^$/) print }' $TEMPFILE) | |
| |
# check sender domain | |
ERR=1 | |
[ "$DOMAINS" ] || ERR=0 | |
for d in $DOMAINS; do | |
[ "$FROM" ] || break; | |
[[ $FROM =~ $d ]] && { ERR=0; break; } | |
done | |
[ $ERR -ne 0 ] && { log "Reject from '$FROM'"; exit 1; } | |
| |
# check the right pattern of the number | |
[[ "$PHONE" =~ ^0[0-9]*$ ]] \ | |
|| { log "Incorrect phone number '$PHONE' from '$FROM'"; exit 1; } | |
[[ "$PHONE" =~ ^00 ]] \ | |
&& { log "International phone number '$PHONE' from '$FROM'"; exit 1; } | |
[ ${#PHONE} -eq 10 ] \ | |
|| { log "Non standard phone number '$PHONE' from '$FROM'"; exit 1; } | |
| |
# log message | |
log "Accept from '$FROM' to $PHONE" | |
| |
# build message | |
EMAIL=${FROM/#*<}; EMAIL=${EMAIL/%>*} | |
FROM=${FROM/%<*}; FROM=${FROM/% } | |
[ "$FROM" ] || FROM=EMAIL | |
[ "$SUBJECT" ] && SUBJECT=" "$SUBJECT":" | |
[ "$MESSAGE" ] && MESSAGE=$'\n'$MESSAGE | |
| |
MESSAGE=$(urlencode "[$FROM]$SUBJECT$MESSAGE") | |
| |
# send http command | |
[ "$DRYRUN" ] && echo "SMS: $MESSAGE" \ | |
|| wget --quiet --output-document=/dev/null \ | |
"http://$GATEWAY:9090/sendsms?phone=$PHONE&text=$MESSAGE&password=$PASSWORD" | |
| |
exit $? |
and a configuration file convert2sms.rc
Shell
# -*- shell-script -*- | |
| |
# configuration variables | |
GATEWAY=smsbox | |
PASSWORD=my_password | |
DOMAINS='@softndesign.org @oelm.org' |
This entry was posted by mazet and filed under Systeme.