Reputation: 283
Whenever I try to use sendmail(sender,reciever,message)
, it sends the email successfully, and the subject line is fine, but the 'body' of the e-mail is always missing.
Here is my full code:
s = smtplib.SMTP("smtp.gmail.com",587)
s.ehlo()
s.starttls()
s.login(sender,password)
message = """From %s
Subject: %s
This is the body part""" % (sender,subject)
s.sendmail(sender,reciever,message)
s.quit()
Why is the body not being received?
Upvotes: 3
Views: 5591
Reputation: 2913
You're missing an extra blank line between the Subject and the body, as outlined in the official documentation's example (paraphrased):
msg = "From: %s\r\nTo: %s\r\n\r\nBody text"
In any case, I would follow the recommendations in the documentation and use the Message object from the email package instead. That way you don't have to worry about formatting the string correctly, and can just do this:
msg = EmailMessage()
msg['Subject'] = "Subject"
msg['From'] = Address("Name", "mailbox", "domain")
msg['To'] = (Address("Name Too", "test", "example.com"),
Address("And so on", "so", "forth"))
msg.set_content("The body of the e-mail")
with smtplib.SMTP("smtp.gmail.com", 587) as s:
s.send_message(msg)
See the official documentation's examples for more.
Upvotes: 5