pythonsmtpsmtplibaiosmtpd

aiosmtpd - python smtp server


I'm trying to run my own stmp server on my computer with python and the aiosmtpd library.
I run the example, everything looks fine but I never receive the email on the other side.
I don't know if there are logs I can see.
I'm using visual studio 2015, python 3.5, and windows 8.1

I saw a similar post but it didn't help.

important note:
at the Client code, I also tried without the Date header

server.py:

 import asyncio import logging

 from aiosmtpd.controller import Controller
 from aiosmtpd.handlers import Sink
 from smtplib import SMTP

 async def amain(loop):
     cont = Controller(Sink(), hostname='::0', port=8025)
     cont.start()


 if __name__ == '__main__':
     logging.basicConfig(level=logging.DEBUG)
     loop = asyncio.get_event_loop()
     loop.create_task(amain(loop=loop))
     try:
         loop.run_forever()
     except KeyboardInterrupt:
         pass

Client.py:

 from smtplib import SMTP import smtplib

 s = SMTP('localhost', 8025) try:
     s.set_debuglevel(True)
     s.sendmail('andy@love.com', ['bob@hate.com'], """\
     Date:17/05/2017,2:18
     From: andy@love.com
     To: bob@hate.com
     Subject: A test
     testing
     """)
     s.quit() except smtplib.SMTPException:
     print("Error: unable to send email")
     import traceback
     traceback.print_exc()

Update
I set s.set_debuglevel(True) at Client.py and received this output:

send: 'ehlo [192.168.56.1]\r\n'
reply: b'250-mycomputername\r\n'
reply: b'250-SIZE 33554432\r\n'
reply: b'250-8BITMIME\r\n'
reply: b'250-SMTPUTF8\r\n'
reply: b'250 HELP\r\n'
reply: retcode (250); Msg: mycomputername\nSIZE 
33554432\n8BITMIME\nSMTPUTF8\nHELP'
send: 'mail FROM:<andy@love.com> size=122\r\n'
reply: b'250 OK\r\n'
reply: retcode (250); Msg: b'OK'
send: 'rcpt TO:<myreal@email.com>\r\n'
reply: b'250 OK\r\n'
reply: retcode (250); Msg: b'OK'
send: 'data\r\n'
reply: b'354 End data with <CR><LF>.<CR><LF>\r\n'
reply: retcode (354); Msg: b'End data with <CR><LF>.<CR><LF>'
data: (354, b'End data with <CR><LF>.<CR><LF>')
send: b'     Date:17/05/2017,2:18\r\n     From: andy@love.com\r\n     To: 
myreal@email.com\r\n     Subject: A test\r\n     testing\r\n     \r\n.\r\n'
reply: b'250 OK\r\n'
reply: retcode (250); Msg: b'OK'
data: (250, b'OK')
send: 'quit\r\n'
reply: b'221 Bye\r\n'
reply: retcode (221); Msg: b'Bye'
Press any key to continue . . .

Solution

  • In your code, Controller(Sink(), hostname='::0', port=8025) starts an SMTP server that receives messages on port 8025 and throws them away (the Sink() part). That's why the messages don't show up in your inbox -- when you send an email to localhost:8025, it is never actually sent to your inbox.

    aiosmtpd is an SMTP server, meaning it will receive messages sent via SMTP and process them somehow. In your code, the Sink() handler does not process incoming email in any way -- it simply throws incoming messages away.

    If you want to send email over the Internet to bob@hate.com, then you should contact the SMTP server responsible for the hate.com domain instead of the SMTP server you're running using aiosmtpd. For this task you will not need to run an SMTP server, since there's supposedly already an SMTP server on the internet running for hate.com; instead, you will need an SMTP client component, which is provided in the smtplib module in the Python standard library. Sending email with SMTP is not something that aiosmtpd is involved with in any way, and you should look for how to use smtplib instead.

    Further reading: Email § Operation on Wikipedia.