简体   繁体   中英

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?

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 (eg 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, eg 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.

  • If all you need is a simple MTA to relay email to remote hosts, then you don't need aiosmtpd or any other kind of Python programming - postfix is an excellent choice. Further reading: aiosmtpd is not an MTA

  • If you want to implement the relaying mechanism yourself, then you need to look up the MX record for the recipient's address (eg gmail.com ) and then instruct smtplib.SMTP to connect to the host specified in the MX record. However, I wouldn't recommend trying to do this, as there are many pitfalls and cases to consider. For example - what to do if there are multiple MX records or no MX records at all. Furthermore, the remote SMTP server might not respond, or it may respond with a 4xx or 5xx error code, in which case you need to take appropriate action and retry at a later time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM