-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathsimple_mail_server.py
More file actions
29 lines (24 loc) · 820 Bytes
/
simple_mail_server.py
File metadata and controls
29 lines (24 loc) · 820 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import smtpd
import smtplib
import asyncore
import dns.resolver # pip install dnspython
PORT = 8025
HOST = ""
# code from dnspython examples
def get_mx_record(domain):
records = dns.resolver.query(domain, 'MX')
return str(records[0].exchange)
# code from http://www.doughellmann.com/PyMOTW/smtpd/
class SimpleMailServer(smtpd.SMTPServer):
def process_message(self, peer, mailfrom, rcpttos, data):
for rcptto in rcpttos:
domain = rcptto.split('@')[1]
mx = get_mx_record(domain)
server = smtplib.SMTP(mx, 25)
try:
server.sendmail(mailfrom, rcptto, data)
finally:
server.quit()
server = SimpleMailServer((HOST, PORT), None)
print 'Server listening on port {0}'.format(PORT)
asyncore.loop()