mail_server.py
import smtpd
import asyncore
class CustomSMTPServer(smtpd.SMTPServer):
def process_message(self, peer, mailfrom, rcpttos, data):
print 'Receiving message from:', peer
print 'Message addressed from:', mailfrom
print 'Message addressed to :', rcpttos
print 'Message length :', len(data)
return
#server = CustomSMTPServer(('127.0.0.1', 1025), None)
server = CustomSMTPServer(('192.168.0.100', 1025), None)
asyncore.loop()
send_mail.py
import smtplib
import email.utils
from email.mime.text import MIMEText
import socket
def send_mail(body, to, subject):
msg = MIMEText(body)
msg['To'] = email.utils.formataddr(('Recipient', to))
msg['From'] = email.utils.formataddr(('Author', 'author@example.com'))
msg['Subject'] = subject
try:
server = smtplib.SMTP(to)
except socket.gaierror:
print 'fail to send to', to
return
except socket.error:
print 'fail to send to', to
return
except smtplib.SMTPServerDisconnected:
print 'fail to send to', to
return
server.set_debuglevel(True) # show communication with the server
try:
server.sendmail('hello', [to], msg.as_string())
finally:
server.quit()
if __name__ == '__main__':
send_mail('word', '192.168.0.100:1025', '[Urgent]') # IOError
#send_mail('word', '10.8.130.180:8888', '[Urgent]')
#send_mail('word', '10.8.130.180:8', '[Urgent]') # IOError
#send_mail('word', '10130.180:8', '[Urgent]') # IOError
#send_mail('word', '10.8.130.180:8899', '[Urgent]') # IOError
python send_mail.py
# start server 1025 port
python mail_server.py
Receiving message from: ('192.168.0.100', 50627)
Message addressed from: hello
Message addressed to : ['1025']
Message length : 187
inbox_server.py
from inbox import Inbox
inbox = Inbox()
@inbox.collate
def handle(to, sender, subject, body):
print to
print sender
print subject
print body
if __name__ == '__main__':
inbox.dispatch()
python send_mail.py
python inbox_server.py 192.168.0.100 1025
['1025']
hello
[Urgent]
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
To: Recipient <192.168.0.100:1025>
From: Author <author@example.com>
Subject: [Urgent]
word