I have built a code to read from excel and send email through python. I can't figure out how to put line breaks inside the text. For example, My code sends email as "Hello Team The handler for this week of date1 to date2 is Name1. The handler for next week of date2 to date3 is Name2.".
I want the email to read as:
"Hello Team
The handler for this week of date1 to date2 is Name1.
The The handler for next week of date3 to date4 is Name2."
Dates and names are extracted from an excel spreadsheet.
import email.message
import smtplib
from email.mime.multipart import MIMEMultipart
from openpyxl import load_workbook
gmail_user = "someone@gmail.com"
gmail_appPassword = "password"
sent_from = ['someone@gmail.com']
to = ['anyone@gmail.com']
import datetime,xlrd
book =
xlrd.open_workbook("C:/Users/shabib/Desktop/simple_excelmail.xlsx")
sh = book.sheet_by_index(0)
a1=sh.cell_value(rowx=0, colx=0)
b1=sh.cell_value(rowx=0, colx=1)
a1_as_date = datetime.datetime(*xlrd.xldate_as_tuple(a1, book.datemode))
b1_as_date = datetime.datetime(*xlrd.xldate_as_tuple(b1, book.datemode))
from datetime import date
x1=date.today()
for rx in range(sh.nrows):
text1= "Hello Team The handler for the week of: "'%s'%
a1_as_date.date(),"to " '%s' % b1_as_date.date(),"is "'%s'%
sh.cell_value(rowx=0, colx=2),"."
text1a=str(text1).strip('()').replace("'",'').replace(",",'')
print (text1a)
SUBJECT = "Weekly Rotation"
message = 'Subject: {}\n\n{}'.format(SUBJECT, msg)
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.login(gmail_user, gmail_appPassword)
server.sendmail(sent_from, to, message)
server.quit()