求助:写python脚本发 带有附件的邮件, 收到邮件后,发现附件直接显示在屏幕上了,而不是以附件形式
不知道为什么会这样,以下是我从网上参考的pythoncode:fromAdd='odps.testmail@alibaba-inc.com'title='RepPrjSt...
不知道为什么会这样,以下是我从网上参考的python code:
fromAdd = 'odps.testmail@alibaba-inc.com'
title = 'RepPrjStat'
ccList = ''
toList = 'wenfeng.jiang@aliyun-inc.com'
try:
#smtp.ops.aliyun-inc.com
emailSender = libmail.SMTP_SSL('smtp.ops.aliyun-inc.com')
msgRoot = MIMEMultipart('alternative')
msgRoot['Subject'] = 'Project Replicate Stat'
content = MIMEText(message, 'html')
msgRoot.attach(content)
file_name="./%s"%self.jobStatDetailFilePath
contype = 'application/octet-stream'
maintype, subtype = contype.split('/', 1)
data = open(file_name, 'rb')
file_msg = MIMEBase(maintype, subtype)
file_msg.set_payload(data.read())
data.close()
basename = os.path.basename(file_name)
file_msg.add_header('Content-Disposition',
'attachment', filename = basename)
msgRoot.attach(file_msg)
msgRoot['From'] = fromAdd
msgRoot['To'] = toList
emailSender.SendHTML('', '', fromAdd, toList, ccList, title, msgRoot.as_string())
except Exception, e:
print traceback.format_exc() 展开
fromAdd = 'odps.testmail@alibaba-inc.com'
title = 'RepPrjStat'
ccList = ''
toList = 'wenfeng.jiang@aliyun-inc.com'
try:
#smtp.ops.aliyun-inc.com
emailSender = libmail.SMTP_SSL('smtp.ops.aliyun-inc.com')
msgRoot = MIMEMultipart('alternative')
msgRoot['Subject'] = 'Project Replicate Stat'
content = MIMEText(message, 'html')
msgRoot.attach(content)
file_name="./%s"%self.jobStatDetailFilePath
contype = 'application/octet-stream'
maintype, subtype = contype.split('/', 1)
data = open(file_name, 'rb')
file_msg = MIMEBase(maintype, subtype)
file_msg.set_payload(data.read())
data.close()
basename = os.path.basename(file_name)
file_msg.add_header('Content-Disposition',
'attachment', filename = basename)
msgRoot.attach(file_msg)
msgRoot['From'] = fromAdd
msgRoot['To'] = toList
emailSender.SendHTML('', '', fromAdd, toList, ccList, title, msgRoot.as_string())
except Exception, e:
print traceback.format_exc() 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询