2

I'm here to ask you some help.

So the thing is, the subject of my email is alright but when I open the email, it's empty and I really can't understand why...

Here is my code :

from urllib2 import urlopen
import smtplib
s=smtplib.SMTP('smtp.gmail.com',587)
s.starttls()
s.ehlo()

# now login as my gmail user
username='myEmail@gmail.com'
password='*****************'
s.login(username,password)

# the email objects and body
replyto='myEmail@gmail.com'
sendto=['myEmail@gmail.com']
subject='Mail automatisation'
content="This is a test"
mailtext='Subject:'+subject+'\n'+content

# send the email
s.sendmail(replyto, sendto, mailtext)

rslt=s.quit()

Thanks for you help.

  • 4
    Possible duplicate of [Python: "subject" not shown when sending email using smtplib module](https://stackoverflow.com/questions/7232088/python-subject-not-shown-when-sending-email-using-smtplib-module) – Alexandre Beaudet May 07 '18 at 13:08
  • I didn't know why I can see it, but I didn't forget to say "Hello everyone". Sorry about that –  May 07 '18 at 13:09
  • Don't worry about using "Hello Everyone" "Thank you" "Please", we actually try to avoid using those sentences, as it adds useless text to the question. (If you do use them, it's fine, but don't feel like you have to). Check the link in the first comment, you should find an answer to your problem there :) ! Welcome to Stack Overflow – Alexandre Beaudet May 07 '18 at 13:10
  • 1
    Thanks to your link, I could solve my problem. But honnestly, I have some struggle to understand why my code wasn't working. Anyway, it's working know, thanks ! –  May 07 '18 at 13:22
  • I'm not a pro with mail creation, but I guess the way it's handled is with a body (your content for example) and a header (the recipient, the sender, the subject). Since you used the "body" part to store your subject, and not the header, it couldn't find it ! – Alexandre Beaudet May 07 '18 at 13:28
  • Feel free to add a comment on Roman's answer (on the other link) and ask for more info if you want, he might be able to explain it :) – Alexandre Beaudet May 07 '18 at 13:30
  • Ok, that's make sense ! –  May 07 '18 at 13:31

1 Answers1

6

The definition of the Internet Message Format (RFC5322 ex. RFC822) requires an empty line between the headers part and the body. As you have one correct header (subject), all what follows before an empty line can be interpreted as a possibly incorrect header. That means that a mail reader can chose to ignore it

You just need:

mailtext='Subject:'+subject+'\n\n'+content

Community
  • 1
  • 1
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252