mail not being received with python SMTP

2019-08-01 06:23发布

Hello I am trying to make python 3 send a simple email from Ubuntu.

I started a simple smpt server with: python -m smtpd -n -c DebuggingServer localhost:1025

The following is the code for my email server:

import smtplib

message = """
Hello
"""
sender = "dancbtalk@yahoo.com"
receivers=["dancbtalk@yahoo.com"]
try:
   smtpObj = smtplib.SMTP('localhost', 1025)
   smtpObj.sendmail(sender, receivers, message)         
   print "Successfully sent email"
except smtplib.SMTPException:
   print "Error: unable to send email"

My output says that the email is send successfully but when I actually check that email account, it has received nothing. I have tried this with several email accounts.

1条回答
淡お忘
2楼-- · 2019-08-01 07:05

Your message does not have any headers. Or more precisely, your message contains only headers, none of which will be recognized as valid. At the very least you probably want to add Subject, From, and To headers. E.g.

sender    = "dancbtalk@yahoo.com"
receivers = ["dancbtalk@yahoo.com"]

headers = """From: %s
To: %s
Subject: Hello
""" % (sender, ", ".join(receivers)

message = headers + "\n" + """
Hello
"""
查看更多
登录 后发表回答