Advertisement
Guest User

Untitled

a guest
Mar 14th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. #! /usr/bin/env python
  2.  
  3. from sys import argv
  4. import smtplib, email
  5. import ConfigParser, os
  6.  
  7. config = ConfigParser.SafeConfigParser({'smtphost': 'localhost', 'smtpuser':'', 'smtppass':'', 'forwardto':''})
  8. config.read(['/etc/smtpclient.ini', os.path.expanduser('~/.smtpclient.ini')])
  9.  
  10. if config.has_section('smsdmailforward'):
  11. section = 'smsdmailforward'
  12. else:
  13. try:
  14. section=config.sections()[0]
  15. except IndexError:
  16. section = 'DEFAULT'
  17.  
  18. smtphost = config.get(section, 'smtphost')
  19. smtpuser = config.get(section, 'smtpuser')
  20. smtppass = config.get(section, 'smtppass')
  21.  
  22. forwardto = config.get(section, 'forwardto')
  23.  
  24. if (len(argv) < 2 or forwardto == ''):
  25. print "smsdmailforward.py "
  26. print ""
  27. print "An event handler script for smstools. Forwards SMS via SMTP."
  28. exit(0)
  29.  
  30.  
  31.  
  32. statuscode = argv[1]
  33. smsfilename = argv[2]
  34.  
  35.  
  36. if (statuscode == 'RECEIVED'):
  37. smsfile = open(smsfilename)
  38. msg = email.message_from_string(smsfile.read())
  39. msg['Original-From'] = msg['From']
  40. msg['To'] = forwardto
  41.  
  42. if msg['Alphabet'] == 'ISO':
  43. msg['Content-Type'] = 'text/plain; charset=ISO-8859-15'
  44.  
  45. server = smtplib.SMTP(smtphost)
  46. server.ehlo()
  47. server.starttls()
  48. server.ehlo()
  49.  
  50. if (smtpuser!='' and smtppass != ''):
  51. server.login(smtpuser, smtppass)
  52.  
  53. server.sendmail('mailcatch-forwarder@localhost', forwardto, msg.as_string())
  54. server.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement