mail not being received with python SMTP

萝らか妹 提交于 2019-12-12 21:28: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:


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
"""


来源:https://stackoverflow.com/questions/24214097/mail-not-being-received-with-python-smtp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!