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

廉价感情. 提交于 2019-12-03 18:01:10

问题


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.

I add line breaks by \n and it works for the text file but not for the email. Do you know what could be the possible reason?


Here's my code:

import smtplib, sys
import traceback
def send_error(sender, recipient, headers, body):

    SMTP_SERVER = 'smtp.gmail.com'
    SMTP_PORT = 587
    session = smtplib.SMTP('smtp.gmail.com', 587)
    session.ehlo()
    session.starttls()
    session.ehlo
    session.login(sender, 'my password')
    send_it = session.sendmail(sender, recipient, headers + "\r\n\r\n" +  body)
    session.quit()
    return send_it


SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587
sender = 'sender_id@gmail.com'
recipient = 'recipient_id@yahoo.com'
subject = 'report'
body = "Dear Student, \n Please send your report\n Thank you for your attention"
open('student.txt', 'w').write(body) 

headers = ["From: " + sender,
               "Subject: " + subject,
               "To: " + recipient,
               "MIME-Version: 1.0",
               "Content-Type: text/html"]
headers = "\r\n".join(headers)
send_error(sender, recipient, headers, body)

回答1:


You have your message body declared to have HTML content ("Content-Type: text/html"). The HTML code for line break is <br>. You should either change your content type to text/plain or use the HTML markup for line breaks instead of plain \n as the latter gets ignored when rendering a HTML document.


As a side note, also have a look at the email package. There are some classes that can simplify the definition of E-Mail messages for you (with examples).

For example you could try (untested):

import smtplib
from email.mime.text import MIMEText

# define content
recipients = ["recipient_id@yahoo.com"]
sender = "sender_id@gmail.com"
subject = "report reminder"
body = """
Dear Student,
Please send your report
Thank you for your attention
"""

# make up message
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = ", ".join(recipients)

# sending
session = smtplib.SMTP('smtp.gmail.com', 587)
session.starttls()
session.login(sender, 'my password')
send_it = session.sendmail(sender, recipients, msg.as_string())
session.quit()



回答2:


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. <br>

Your text should be:

"Dear Student, <br> Please send your report<br> 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"



回答3:


In my case '\r\n' didn't work, but '\r\r\n' did. So my code was:

from email.mime.text import MIMEText
body = 'Dear Student,\r\r\nPlease send your report\r\r\nThank you for your attention'
msg.attach(MIMEText(body, 'plain'))

The message is written in multiple lines and is displayed correctly in Outlook.




回答4:


Setting the content-type header to Content-Type: text/plain (with \r\n at the end) allowed me to send multi-line plain-text emails.




回答5:


Outlook will remove line feeds from plain text it believes are extras. https://support.microsoft.com/en-us/kb/287816

You can try below update to make the lines look like bullets. That worked for me.

body = "Dear Student, \n- Please send your report\n- Thank you for your attention"


来源:https://stackoverflow.com/questions/14139165/how-to-get-line-breaks-in-e-mail-sent-using-pythons-smtplib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!