Skip to content

Instantly share code, notes, and snippets.

@speters
Last active March 14, 2016 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save speters/74f30274e8886616115b to your computer and use it in GitHub Desktop.
Save speters/74f30274e8886616115b to your computer and use it in GitHub Desktop.
SMS to email forwarding handler for SMStools
#! /usr/bin/env python
from sys import argv
import smtplib, email
import ConfigParser, os
config = ConfigParser.SafeConfigParser({'smtphost': 'localhost', 'smtpuser':'', 'smtppass':'', 'forwardto':''})
config.read(['/etc/smtpclient.ini', os.path.expanduser('~/.smtpclient.ini')])
if config.has_section('smsdmailforward'):
section = 'smsdmailforward'
else:
try:
section=config.sections()[0]
except IndexError:
section = 'DEFAULT'
smtphost = config.get(section, 'smtphost')
smtpuser = config.get(section, 'smtpuser')
smtppass = config.get(section, 'smtppass')
forwardto = config.get(section, 'forwardto')
if (len(argv) < 2 or forwardto == ''):
print "smsdmailforward.py "
print ""
print "An event handler script for smstools. Forwards SMS via SMTP."
exit(0)
statuscode = argv[1]
smsfilename = argv[2]
if (statuscode == 'RECEIVED'):
smsfile = open(smsfilename)
msg = email.message_from_string(smsfile.read())
msg['Original-From'] = msg['From']
msg['To'] = forwardto
if msg['Alphabet'] == 'ISO':
msg['Content-Type'] = 'text/plain; charset=ISO-8859-15'
server = smtplib.SMTP(smtphost)
server.ehlo()
server.starttls()
server.ehlo()
if (smtpuser!='' and smtppass != ''):
server.login(smtpuser, smtppass)
server.sendmail('mailcatch-forwarder@localhost', forwardto, msg.as_string())
server.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment