How to get line breaks in e-mail sent using Python's smtplib?

前端 未结 5 1707
说谎
说谎 2020-12-01 15:36

I have written a script that writes a message to a text file and also sends it as an email. Everything goes well, except the email finally appears to be all in one line.

5条回答
  •  再見小時候
    2020-12-01 16:16

    Unfortunately for us all, not every type of program or application uses the same standardization that python does.

    Looking at your question i notice your header is: "Content-Type: text/html"

    Which means you need to use HTML style tags for your new-lines, these are called line-breaks.

    Your text should be:

    "Dear Student, 
    Please send your report
    Thank you for your attention"

    If you would rather use character type new-lines, you must change the header to read: "Content-Type: text/plain"

    You would still have to change the new-line character from a single \n to the double \r\n which is used in email.

    Your text would be:

    "Dear Student, \r\n Please send your report\r\n Thank you for your attention"
    

提交回复
热议问题