#!/bin/sh

# This script can be used with procmail to build an "email to sms" gateway.
# Simply put the receivers number instead of the name in the To: field of 
# the eMail. The subject field is copied to the SMS file but not used.
# example eMail:

# From: "Stefan Frings" <stefan@localhost>
# To: "+491721234567" <sms@localhost>
# Subject: Not used
# 
# Hello, this is the message text.

# Use procmail to deliver local eMail. Create the user sms and create
# the file ~/.procmailrc for this user:

# VERBOSE=off
# MAILDIR=/var/spool/mail
# DEFAULT=/var/spool/mail/sms
# LOGFILE=/var/log/procmail
# 
# :0
# * ^TOsms
# | /usr/local/bin/email2sms

# If you use QMail and vpopmail you need the file
# /home/vpopmail/domains/your-domain/.qmail-sms with this content:

# | /usr/local/bin/email2sms



PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

INFILE=$(mktemp /var/tmp/smsgw.in.XXXXXX) || exit 1
OUTFILE=$(mktemp /var/tmp/smsgw.out.XXXXXX) || exit 1
cat >$INFILE
from=`formail -zx From: < $INFILE | sed -e 's/"//g' -e 's/<.*>//g'` 
to=`formail -zx To: < $INFILE | sed -e 's/"//g' -e 's/<.*>//g'`
subject=`formail -zx Subject: < $INFILE`
echo "From: $from" > $OUTFILE
echo "To: $to" >> $OUTFILE
echo "Subject: $subject" >> $OUTFILE
formail -I "" < $INFILE >> $OUTFILE
rm $INFILE
mv $OUTFILE /var/spool/sms/outgoing
echo "SMS queued"
