I'm trying to send a email to a gmail account from a non-gmail email address, but I get the following error:
Mail delivery failed: returning message to sender
Our system has detected that this message is
550-5.7.1 not RFC 5322 compliant:
550-5.7.1 'From' header is missing.
Here is my code:
import smtplib
import ssl
import getpass
port = 465 # For SSL
smtp_server = "website.net"
sender_email = "foo@website.net"
receiver_email = "foo@gmail.com"
password = getpass.getpass(prompt='Type your password and press enter: ')
message = "This message is sent from Python."
context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message)
I'm able to send emails to myself, but sending them to a gmail address gives me that 'From' header is missing
error. How and were do I add that?
Add the headers to the message
variable.
message = f"""From: {sender_email}
To: {receiver_email}
This message is sent from Python."""
You should also add a To:
header. Otherwise the recipients will be treated as BCC.