2

I'm writing a python script to send email. Right now it is sending an excel file as an attachment, but instead I want to send the email with the body having the contents of the excel file. Like copying the contents of the excel file with color, formatting etc. and the pasting it in the email body. How do I do that?

My python script is below:

#!/usr/local/bin/python2.7
import smtplib,email,email.encoders,email.mime.text,email.mime.base
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email import encoders
from email.message import Message
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import schedule
import time

msg = MIMEMultipart()
# me == my email address
# you == recipient's email address
me = "abc@something.com"
you = "abc@something.com"


# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('mixed')
msg['Subject'] = "Automation Testing"
msg['From'] = me
msg['To'] = you

# Create the body of the message (a plain-text and an HTML version).
text = "Hi\n\nPlease find attached the weekly report."


part1 = MIMEText(text, 'plain')


#attach an excel file:
fp = open('/Users/excel.xlsm', 'rb')
file1=email.mime.base.MIMEBase('application','vnd.ms-excel')
file1.set_payload(fp.read())
fp.close()
email.encoders.encode_base64(file1)
file1.add_header('Content-Disposition','attachment;filename=excelsheet.xlsx')

# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.

msg.attach(part1)
msg.attach(file1)

composed = msg.as_string()

fp = open('msgtest.txt', 'w')
fp.write(composed)

# Credentials (if needed)  
# The actual mail send  
server = smtplib.SMTP('smtp.something.com', 259)  
server.starttls()  
server.sendmail(me, you, composed)  
server.quit()  
fp.close()
Reshma
  • 21
  • 1
  • 4
  • 1
    possible duplicate of [Send table as an email body (not attachment ) in Python](http://stackoverflow.com/questions/38275467/send-table-as-an-email-body-not-attachment-in-python) – chickity china chinese chicken Mar 28 '17 at 19:09
  • I don't think it's a duplicate because I need to send the contents of the excel file as is like it appears when I manually do Ctrl+A, Ctrl+C on the excel file and Ctrl+V on the email body; it appears more like a screenshot of the entire file of sorts. With color everything. – Reshma Mar 28 '17 at 19:23
  • Ok, so you want python to insert an image (screenshot) of the excel document into the body of your email? Use [pyscreenshot](http://pyscreenshot.readthedocs.io/en/latest/) and then `embedded in html`: http://stackoverflow.com/a/20474635/1248974 – chickity china chinese chicken Mar 28 '17 at 19:26
  • That could work only if I don't need to open the file manually for the screenshot to be taken. I basically want to know how to copy all contents of the excel sheet along with all its formatting, color etc. into the email body using python. – Reshma Mar 28 '17 at 19:30
  • 1
    That sounds like [Emails via Python - Paste contents of excel/csv as a formatted table onto the mail body](http://stackoverflow.com/questions/16145616/emails-via-python-paste-contents-of-excel-csv-as-a-formatted-table-onto-the-ma) – chickity china chinese chicken Mar 28 '17 at 19:32
  • Yes, but it doesn't have many answers to clarify my doubt. Can anyone help me on this? – Reshma Mar 28 '17 at 19:36

0 Answers0