This siteAll blogsLe 17ClémentCCBCuisineLe 3ContactSe connecterS'inscrire
  • All blogs
  • Le 17
  • Clément
  • CCB
  • Cuisine
  • Le 3
  • Contact

  • S'inscrire

Recettes informatiques

  • Page de garde
  • Contact
  • Se connecter
  • « Disabling hyperthreading
  • Migrate from mbox to MailDir »

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.

No feedback yet

Catégories

  • Toutes
  • Non catégorisé
  • Programmation
    • Awk
    • Bash
    • C
    • C++
    • Javascript
    • LaTeX
    • Perl
    • Tcl/Tk
  • Systeme
    • Android
    • Debian
    • Ubuntu

Contenu

  • Linux Router
  • Creating a network between 2 virtual machines on Windows with Qemu
  • Build a Debian package for pdmenu
  • Extract informations from epub file
  • Connexion PostgreSql / Java par Socket Unix
  • Example of fork and respawn
  • Start SSH daemon on Git portable distribution
  • Create a git mirror
  • Color diff
  • Create on Debian a Minecraft server
  • GIT/HG/SVN on multiple repos simultaneously
  • Convert Comics into CBZ format
  • Random password generator function
  • Convert to camelCase
  • DKIM and SPF
  • Migration from BackupPC 3 (Debian package) to BackupPC 4 (Standalone Installation)
  • Minimal BC for Mingw
  • Diskless Debian cluster
  • Colorize log file
  • Generate certificates with (or without) a certificate authority
Août 2025
Lun Mar Mer Jeu Ven Sam Dim
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
 << <   > >>
  • Accueil
  • Récemment
  • Archives
  • Catégories
  • Derniers commentaires

Rechercher

Flux XML

  • RSS 2.0: Posts
  • Atom: Posts
More on RSS

©2025 by Laurent Mazet • Contact • Aide • CMS software

CMS

Cookies are required to enable core site functionality.