Guest User

Untitled

a guest
Jul 16th, 2018
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. #!/usr/bin/python2
  2. """
  3. Simple SMTP Gmail proxy
  4. By Gregory Eric Sanderson Turcot Temlett MacDonnell Forbes
  5.  
  6. One of the servers that I maintain has a crappy Exim that randomly crashes. In a fit of frustration,
  7. while looking for a solution that would notify me when problems occured, I hacked together this script.
  8. When monit can't send me warning messages through Exim, it sends them to my gmail account through
  9. this script that acts as a backup SMTP server.
  10.  
  11. This script is licensed under the WTFPL license.
  12. """
  13. import smtpd
  14. import asyncore
  15. import smtplib
  16. import sys
  17. import logging
  18.  
  19. class GmailProxy( smtpd.SMTPServer ):
  20.  
  21. gmail_smtp = 'smtp.gmail.com'
  22. gmail_port = 587
  23.  
  24. def __init__(self, localaddr, username, passwd, logger=None):
  25. smtpd.SMTPServer.__init__(self, localaddr, None)
  26. self._username = username
  27. self._passwd = passwd
  28.  
  29. if not logger:
  30. logger = logging.getLogger()
  31. logger.addHandler( logging.NullHandler() )
  32.  
  33. self.logger = logger
  34.  
  35. def process_message(self, peer, mailfrom, rcpttos, data):
  36.  
  37. self.logger.debug("Recevied request - peer : %s from : %s to : %s" % (peer, mailfrom, rcpttos) )
  38.  
  39. server = smtplib.SMTP(self.gmail_smtp, self.gmail_port)
  40.  
  41. server.starttls()
  42. server.ehlo()
  43. server.login(self._username, self._passwd)
  44.  
  45. gmail_adress = "%s@gmail.com" % self._username
  46.  
  47. try:
  48. server.sendmail( gmail_adress, rcpttos, data )
  49. except Exception as e:
  50. self.logger.error( e )
  51. else:
  52. self.logger.debug("Request sent")
  53. finally:
  54. server.quit()
  55.  
  56.  
  57. if __name__ == '__main__':
  58.  
  59. if len(sys.argv) < 3:
  60. print """Simple SMTP Gmail Proxy
  61.  
  62. Starts an SMTP server and relays messages through Gmail's SMTP.
  63.  
  64. Usage : gmail_proxy.py username password [host=localhost] [port=1025] [log_file]
  65. """
  66. sys.exit()
  67.  
  68. username, password = sys.argv[1:3]
  69.  
  70. host = 'localhost'
  71. if len(sys.argv) > 3:
  72. host = sys.argv[3]
  73.  
  74. port = 1025
  75. if len(sys.argv) > 4:
  76. port = int(sys.argv[4])
  77.  
  78. logger = None
  79. if len(sys.argv) > 5:
  80. logger = logging.getLogger('gmail_proxy')
  81. logger.setLevel(logging.DEBUG)
  82. handler = logging.FileHandler( sys.argv[5], 'a')
  83. handler.setFormatter( logging.Formatter(fmt='%(asctime)s - %(message)s') )
  84. logger.addHandler(handler)
  85.  
  86. proxy = GmailProxy( (host, port), username, password, logger )
  87. asyncore.loop()
Add Comment
Please, Sign In to add comment