user3739190
user3739190

Reputation: 121

mail not being received with python SMTP

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 = "[email protected]"
receivers=["[email protected]"]
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.

Upvotes: 5

Views: 5239

Answers (1)

kindall
kindall

Reputation: 184081

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    = "[email protected]"
receivers = ["[email protected]"]

headers = f"""From: {sender}
To: {", ".join(receivers)}
Subject: Hello
"""

message = headers + "\n" + """
Hello
"""

Upvotes: 4

Related Questions