4

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 . . .
MasterOdin
  • 7,117
  • 1
  • 20
  • 35
Rafi
  • 135
  • 1
  • 9

1 Answers1

6

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.

Mathias Rav
  • 2,808
  • 14
  • 24
  • Thanks for the enlighting answer. 2.to make sure i understand, all this time i actually try to send mail to my localhost although the To: field was to a different mail? 3.with smtplib alone, i can send mail to bob@hate.com even though the Form: field is not from the hate.com domain but an custome address of my own? my goal is to send mails for my buisness with my domain without 3rd party account such as gmail,yahoo – Rafi May 22 '17 at 17:42
  • It sounds like you have understood my answer correctly. To send email from your own domain you can in principle just use smtplib (along with a DNS resolver to figure out what server to contact to reach your recipient), but if you're not careful you will be blocked by major email providers (Gmail, Microsoft, ...) for sending spam. You should search the internet for "outbound email service" and look up how to set up DKIM for your domain -- that is unfortunately beyond the scope of StackOverflow. – Mathias Rav May 22 '17 at 17:52
  • I will look ot up. Thanks again! – Rafi May 22 '17 at 21:46