首先创建一个发送邮件的工具类sendmailM.py
import smtplib
import email.mime.multipart
import email.mime.text
from email.mime.application import MIMEApplication
def send(file_name, show_name):
msg = email.mime.multipart.MIMEMultipart()
# 设置邮件信息
msg['from'] = '寄件人邮箱'
msg['to'] = '发送邮箱1,发送邮箱2'
msg['cc'] = '抄送邮箱'
# 对于发送所需信息的整理,需要整理成list
msgtos = msg['to'].split(',')
msgtos.append(msg['cc'])
#邮件标题
msg['subject'] = '每天商户数据统计'
#邮件主题内容
content = '''
hi,
附件为本次商户统计结果,请查阅!
谢谢!
'''
# 邮件正文必须的设置
txt = email.mime.text.MIMEText(content)
msg.attach(txt)
# 引入附件
part = MIMEApplication(open(file_name, 'rb').read())
part.add_header('Content-Disposition', 'attachment', filename=show_name)
msg.attach(part)
# 发送邮件服务器,登录信息,发送信息设置
smtp = smtplib.SMTP()
smtp.connect('c2.icoremail.net', '25') # 连接smtp邮件服务器,端口默认是25
smtp.login('寄件人邮箱', '密码') # 登陆服务器
smtp.sendmail('寄件人邮箱', msgtos, str(msg)) # 发送邮件
smtp.quit()
调用发送邮件方法
sendmailM.send(zip_file_name, '数据.zip')
发送成功之后