Trying to send email to Google through in Python,

2019-09-09 19:25发布

问题:

I'm getting a strange error when I send email through Python using SMTP.

I get an email from Mail Delivery System in my GoDaddy inbox with the error:

[Return Code 550] sid: Xtpe1o00e3l0Tyx01 :: 5.7.1 more information. ye4si18523263pbb.103 - gsmtp

For reference, the script looks like:

import smtplib
server = smtplib.SMTP_SSL('smtpout.secureserver.net', 465)

print("Logging in: ")
server.login("username", "password")

subject = "This is a test email!"
msg = "Sample email message"
text = 'Subject: %s\n\n%s' % (subject, msg)
fromaddr = "fromemail"
toaddr = "toemail"
print("Sending email...")
server.sendmail(fromaddr, toaddr, text)
server.quit()

But I believe my error has nothing to do with the code, but with Google blocking the email.

回答1:

Warning: Wild guess from incomplete data

Your email is being rejected as spam. Ensure your email is RFC2822-compliant. Quoting the standard:

The only required header fields are the origination date field and the originator address field(s).

In your case, add at least From: and To: to your outgoing email:

text = 'From: %s\nTo: %s\nSubject: %s\n\n%s' % (fromaddr, toaddr, subject, msg)

and confirm that smtplib automatically adds the Date: field.