繁体   English   中英

使用 python 通过 outlook 发送 excel 附件

[英]send excel attachment through outlook using python

下面是我使用 excel 作为 outlook email 版本的附件发送 excel 的代码。 如果我命令代码中的附件行,我可以发送文本 email 但不发送附加的 email。我也没有收到任何错误。 有人可以帮我找出代码中的问题吗?

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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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