[使用python通过Outlook发送excel附件

问题描述 投票:0回答:1

以下是我使用python 2.7版本发送Excel作为Outlook电子邮件附件的代码。如果我在代码中命令附件行,我可以发送文本电子邮件,但不能发送附件电子邮件,而且我也不会收到任何错误。有人可以帮我确定代码中的问题。

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 = "[email protected]"
RECIPIENTS = "[email protected]"
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'] = "[email protected]"
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()
python python-2.7
1个回答
0
投票
path='/xxx/yyyy/zzz/file.xlsx'
fp=open(path, 'rb')
stg = MIMEBase('application','vnd.openxmlformatsofficedocument.spreadsheetml.sheet')
stg.set_payload(fp.read())
fp.close()
© www.soinside.com 2019 - 2024. All rights reserved.