Skip to content

Instantly share code, notes, and snippets.

@jeaneric
Forked from TheBHump/smtp_relay.py
Last active December 31, 2015 23:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeaneric/8060652 to your computer and use it in GitHub Desktop.
Save jeaneric/8060652 to your computer and use it in GitHub Desktop.
"""
SMTP->SES Relay Server
Author: Brian Humphrey, Loku.com
E-mail: bhump@loku.com
Date: August 17, 2011
A Lightweight SMTP server that accepts all messages and relays them to SES
Credit to http://www.doughellmann.com/PyMOTW/smtpd/ for a tutorial on smtpd with asyncore
"""
import smtpd
import asyncore
from boto.ses.connection import SESConnection
from email.parser import Parser
AWS_KEY = '<YOUR AWS KEY>'
AWS_SECRET_KEY = '<YOUR AWS SECRET KEY>'
SERVER_IP_ADDRESS = "<YOUR SERVER IP ADDRESS>"
SERVER_PORT = 1025
class SESRelaySMTPServer(smtpd.SMTPServer):
def process_message(self, peer, mailfrom, rcpttos, data):
#Print the request information
print 'Receiving message from:', peer
print 'Message addressed from:', mailfrom
print 'Message addressed to :', rcpttos
print 'Message length :', len(data)
#Send out via SES
connection = SESConnection(aws_access_key_id = AWS_KEY, aws_secret_access_key = AWS_SECRET_KEY)
#connection.send_raw_email(mailfrom, data)
#Looks like this have changed:
#https://github.com/boto/boto/issues/195
connection.send_raw_email(data)
#Works ok for me
server = SESRelaySMTPServer((SERVER_IP_ADDRESS, SERVER_PORT), None)
asyncore.loop()
"""
SMTP->SES Relay Server w/ Header Filter
Author: Brian Humphrey, Loku.com
E-mail: bhump@loku.com
Date: August 17, 2011
A Lightweight SMTP server that accepts all messages, removes the Error-To header which offends SES, then relays them to SES
Credit to http://www.doughellmann.com/PyMOTW/smtpd/ for a tutorial on smtpd with asyncore
"""
import smtpd
import asyncore
from boto.ses.connection import SESConnection
from email.parser import Parser
AWS_KEY = '<YOUR AWS KEY>'
AWS_SECRET_KEY = '<YOUR AWS SECRET KEY>'
SERVER_IP_ADDRESS = "<YOUR SERVER IP ADDRESS>"
SERVER_PORT = 1025
class SESRelaySMTPServer(smtpd.SMTPServer):
def process_message(self, peer, mailfrom, rcpttos, data):
#Print the request information
print 'Receiving message from:', peer
print 'Message addressed from:', mailfrom
print 'Message addressed to :', rcpttos
print 'Message length :', len(data)
#Parse the message and remove headers that are offensive to SES
msg = Parser().parsestr(data)
if 'Errors-To' in msg:
del msg['Errors-To']
#Send out via SES
connection = SESConnection(aws_access_key_id = AWS_KEY, aws_secret_access_key = AWS_SECRET_KEY)
#connection.send_raw_email(mailfrom, msg.as_string())
#Looks like this have changed:
#https://github.com/boto/boto/issues/195
connection.send_raw_email(msg.as_string())
#Haven't tested this one
server = SESRelaySMTPServer((SERVER_IP_ADDRESS, SERVER_PORT), None)
asyncore.loop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment