简体   繁体   中英

send excel attachment through outlook using python

Below is the code i'm using to send excel as a attachment to outlook email using python 2.7 version. If i command the attachment lines in the code, i could send the text email but not the attached email.Also i'm not receiving any error. Can somebody please help me identify the issue in the code.

import psycopg2
import pandas as pd
import numpy as np
import xlsxwriter
from datetime import date
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import smtplib
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

server = smtplib.SMTP(host='xxxxx.net', port=25)
fromaddr = "aaaaaaaaabb@outlook.com"
RECIPIENTS = "aaaaaaaaabb@outlook.com"
msg = MIMEMultipart()
body = """
   Hi Team, <br>
   <br>
   Please find the attached data sync validation betweeen Reltio and BDS .<br>
   <br>
   Please reach out to Team for any questions.<br>
   <br>
   Regards,<br>
   xxxx <br>
   Support Team<br>"""

subject = 'Reltio and BDS Data Sync for EU Org'
msg = MIMEMultipart("alternative", None, [MIMEText(body, 'html','utf-8')])
msg['To'] = RECIPIENTS
msg['From'] = "axxxxxyyy@.net"
msg['Subject'] = subject
stg = MIMEBase('application', 'vnd.ms-excel')
stg.set_payload(open("EU_Consent.xlsx", "rb").read())
encoders.encode_base64(stg)
stg.add_header('Content-Disposition', 'attachment', filename="EU_Consent.xlsx")
msg.attach(stg)
server.ehlo()
server.starttls()
server.ehlo()
text = msg.as_string()
server.sendmail(fromaddr, RECIPIENTS, text)
server.quit()
path='/xxx/yyyy/zzz/file.xlsx'
fp=open(path, 'rb')
stg = MIMEBase('application','vnd.openxmlformatsofficedocument.spreadsheetml.sheet')
stg.set_payload(fp.read())
fp.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM