通过zmail包发邮件,如下以qq邮箱为例
#coding: utf-8
#author: dyl
#date: 2020/8/27 2:35 下午
#IDE: PyCharm
import zmail
import smtplib #加载smtplib模块
from email.mime.text import MIMEText
from email.utils import formataddr
from email.mime.multipart import MIMEMultipart
from email.header import Header
from email.mime.application import MIMEApplication
import datetime
import os
#注意: 此处如果邮箱是qq,密码需要填写授权码,自行百度教程,按照教程发短信就可以获得
# 如果不是qq,可能填写登录密码即可,如还有特殊规定,视情况而定
user = 'xxxx@qq.com'
passwd = 'imvheozlx'
alert_sender = zmail.server(
user,
passwd,
smtp_host='smtp.qq.com',
smtp_port=465,
smtp_ssl=True,
# pop_host='pop.exmail.qq.com',
# pop_port=995
)
def send_alert_email(subject='',
content_text='',
recipients=[],
cc=[],
file_name=None):
# 签名,自行填写
mail_sign = '\n\n' \
+ '------------------\n' \
+ 'dyl\n'
mail = {
'subject': subject, # Anything you want.
'content_text': content_text+mail_sign, # Anything you want.
}
if os.path.exists(file_name)!=None:
mail["attachments"] = [file_name]
return alert_sender.send_mail(recipients=recipients, mail=mail, cc=cc)
def send_mail_interface(file_name):
# 主题
subject_content = "测试"
# 信正文
mail_content ="各位老师好,\n\n辛苦各位!"
# 收信人,可以是多个
recipients = [
"tao@xxx.com",
]
# 抄送人,可以是多个
cc = [
"xxx@xxx.com",
]
send_alert_email(subject=subject_content,
content_text=mail_content,
recipients=recipients,
cc=cc,
file_name=file_name)
if __name__ == "__main__":
#附件名
file_name = './test.xlsx'
send_mail_interface(file_name)