pythonemailsmtpspfaiosmtpd

Can receive but not send emails with aiosmtpd SMTP server


I have setup a local SMTP server with aiosmtpd where I received emails after setting the proper DNS records:

mail.mydomain.com   A   1 minute    1.2.3.4
mydomain.com    MX  1 minute    10 mail.mydomain.com.

Just running aiosmtpd in the command line is enough to receive emails:

python.exe -m smtpd -c DebuggingServer -n 0.0.0.0:25

The problem is now sending emails which for whatever reason seems not to work. I have set the SPF record this way:

mydomain.com    TXT 1 minute    "v=spf1 a mx include: mydomain.com ~all"

I have tried with this too:

mydomain.com    TXT 1 minute    "v=spf1 ip4:1.2.3.4 ~all"

I'm using this code to send the email while the SMTP server is running:

import smtplib

sender = 'from@mydomain.com'
receivers = ['to@mydomain.com']

message = """From: From Person <from@mydomain.com>
To: To Person <to@mydomain.com>
Subject: SMTP e-mail test

This is a test e-mail message.
"""

try:
   smtpObj = smtplib.SMTP('127.0.0.1')
   smtpObj.sendmail(sender, receivers, message)
   print ("Successfully sent email")
except smtplib.SMTPException:
   print ("Error: unable to send email")

I have tried to add as destination email my @gmail.com email address too but I don't get anything.

I can see that the email is sent to the SMTP server but I don't get the email back:

---------- MESSAGE FOLLOWS ----------
b'From: From Person <from@mydomain.com>'
b'To: To Person <to@mydomain.com>'
b'Subject: SMTP e-mail test'
b'X-Peer: 127.0.0.1'
b''
b'This is a test e-mail message.'
------------ END MESSAGE ------------

Any idea on what am I doing wrong?


Solution

  • Generally, email is sent on the internet from email clients connecting to mail transfer agents (MTAs) that relay emails to mail delivery agents (MDAs) that store the email for the recipient to read. That is, GMail operates both as an MTA for its users, relaying GMail users' emails to remote mail servers, and as an MDA that receives emails slated for delivery to its users' mailboxes. Emails are transferred between servers using the SMTP protocol, and aiosmtpd is an implementation of the server-end of the SMTP protocol, whereas the built-in smtplib module is an implementation of the client-end.

    When you run aiosmtpd in its default configuration, it is simply a dumb SMTP server that receives emails and spits them out on the terminal without relaying them or storing them. That is to say, aiosmtpd by itself is not an MTA or an MDA, unless you write code that implements storage of email (e.g. using the 'mailbox' built-in module), or you write code that implements relaying of email.

    When you use the SMTP client smtplib.SMTP() to connect to 127.0.0.1, you are simply connecting to aiosmtpd, which won't do anything interesting with the email that you give it (unless you add code that does something interesting).


    If you want to send email to remote email providers, e.g. GMail, then you need to either submit the email to a fixed MTA, or you need to implement the relaying mechanism yourself using DNS lookups.