Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 = '[email protected]' recipient = '[email protected]' 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) 
like image 245
f.ashouri Avatar asked Jan 03 '13 12:01

f.ashouri


People also ask

How do you insert a line break in plain text email?

You can insert a break into plaintext emails using {! BR()} .

How do I use Smtplib in Python 3?

Python provides smtplib module, which defines an SMTP client session object that can be used to send mails to any Internet machine with an SMTP or ESMTP listener daemon. host − This is the host running your SMTP server. You can specifiy IP address of the host or a domain name like tutorialspoint.com.

What is Smtplib in Python How can you use Python built-in mail server explain?

The smtplib module The smtplib is a Python library for sending emails using the Simple Mail Transfer Protocol (SMTP). The smtplib is a built-in module; we do not need to install it. It abstracts away all the complexities of SMTP.


2 Answers

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" 
like image 138
Inbar Rose Avatar answered Sep 25 '22 01:09

Inbar Rose


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 = ["[email protected]"] sender = "[email protected]" 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() 
like image 44
moooeeeep Avatar answered Sep 22 '22 01:09

moooeeeep