Skip to content

Instantly share code, notes, and snippets.

@tarnacious
Created November 3, 2011 07:00
Show Gist options
  • Select an option

  • Save tarnacious/1335952 to your computer and use it in GitHub Desktop.

Select an option

Save tarnacious/1335952 to your computer and use it in GitHub Desktop.
MailRedirectServer
import smtpd
import asyncore
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.parser import Parser
import quopri
class MailRedirectServer(smtpd.SMTPServer):
def __init__(self, smtp_listen,smtp_forward,redirect_address):
smtpd.SMTPServer.__init__(self,smtp_listen,None)
self.smtp_forward = smtp_forward
self.redirect_address = redirect_address
self.parser = Parser()
def process_message(self, peer, mailfrom, rcpttos, data):
self.forward_message(data)
def forward_message(self,data):
email = self.parser.parsestr(data)
# Build new email
msg = MIMEMultipart('alternative')
msg['To'] = self.redirect_address
msg['Subject'] = email["Subject"]
msg['from'] = email["From"]
message = quopri.decodestring(email.get_payload())
msg.attach(MIMEText(message,'text'));
msg.attach(MIMEText(message,'html'));
# Attach original email
original = MIMEText(data)
original.add_header('Content-Disposition', 'attachment', filename='original.txt')
msg.attach(original)
# Send to a real SMTP server
server = smtplib.SMTP(self.smtp_forward[0],self.smtp_forward[1])
server.sendmail(email["From"], self.redirect_address, msg.as_string())
server = MailRedirectServer(('127.0.0.1', 1025),
('192.168.0.15', 25),
"[email protected]; [email protected]")
asyncore.loop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment