#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
@author
@mail
@date 2017/03/16
发送邮件
'''
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
import tools.log_tool as log
def send_email(file_path, day):
# 发送邮件
sender = 'xxx@qq.com'
to_reciver = ['xxx@qq.com', ] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
cc_reciver = ['xxx@qq.com', ]
reciver = to_reciver + cc_reciver
# 创建一个带附件的实例
message = MIMEMultipart()
subject = 'xx报表'
message['Subject'] = Header(subject, 'utf-8')
message['From'] = sender
message['To'] = ','.join(to_reciver)
message['Cc'] = ','.join(cc_reciver)
# 邮件正文内容
message.attach(MIMEText('Dear all:
附件为' + day + 'xx项目报表,请查收,谢谢!', 'plain', 'utf-8'))
# 构造附件1,传送当前目录下的 chart_line.xlsx 文件
att1 = MIMEText(open(file_path, 'rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
# 这里的filename可以任意写,写什么名字,邮件中显示什么名字
att1["Content-Disposition"] = 'attachment; filename=%s.xlsx' % (day)
message.attach(att1)
try:
smtpObj = smtplib.SMTP('xxx.com')
smtpObj.login('xxx@qq.com', 'xxxpsd')
smtpObj.sendmail(sender, reciver, message.as_string())
log.warning("邮件发送成功")
except smtplib.SMTPException as e:
log.error(e)
log.error("Error: 无法发送邮件")
if __name__ == '__main__':
send_email(r'xxxx', 'xxx')