部分配置是直接写死的,请注意自己修改
email 变量是收件人
msg 变量是html中一小部分自定义文字
智能邮件 直接匹配这个文字,这个是发件人名字
user 是发件人账号
password 是发件人密码
废话不多说,直接上代码
import logging
import smtplib
import time
from email.mime.text import MIMEText
from email.utils import formataddr
email = "接收人邮箱,可写多个,使用逗号隔离"
msg = "你好世界"
user = "发件人邮箱"
password = "发件人密码"
class QQMailMan:
def __init__(self, user, password):
# 发件人邮箱账号
self.user = user
# user登录邮箱的用户名,password登录邮箱的密码(授权码,即客户端密码,非网页版登录密码),但用腾讯邮箱的登录密码也能登录成功
self.password = password
def connect(self):
try:
self.smtp_host = "smtp.exmail.qq.com"
self.smtp_port = 465
self.mailman = smtplib.SMTP_SSL(self.smtp_host, self.smtp_port)
self.mailman.set_debuglevel(1)
self.mailman.login(self.user, self.password)
except Exception:
raise (Exception)
def close(self):
self.mailman.quit()
def mail_to(self, to_addrs, subject, content):
"""
发送邮件到指定地址(也可以是地址列表或用,拼接的字符串)
"""
msg = MIMEText(content, 'html', 'utf-8')
if type(to_addrs) == str:
msg['To'] = to_addrs
to_addrs = to_addrs.split(',')
elif type(to_addrs) == list:
msg['To'] = ','.join(to_addrs)
else:
logging.error("邮件地址类型错误,必须为str(可以用,拼接)或list")
return
time.sleep(1)
msg['From'] = formataddr(["智能邮件", self.user])
# 邮件的主题
msg['Subject'] = subject
try:
ret = self.mailman.sendmail(self.user, to_addrs, msg.as_string())
logging.info("成功发送邮件至:{} {}".format(to_addrs, ret))
except Exception:
logging.error("发送邮件失败: {} {} {}".format(to_addrs, ret, Exception))
def mailsend():
content_template = ("""\
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>智能邮件机器人</title>
<style>
body,html,div,ul,li,button,p,img,h1,h2,h3,h4,h5,h6 {
margin: 0;
padding: 0;
}
body,html {
background: #fff;
line-height: 1.8;
}
h1,h2,h3,h4,h5,h6 {
line-height: 1.8;
}
.email_warp {
height: 100vh;
min-height: 500px;
font-size: 14px;
color: #212121;
display: flex;
/* align-items: center; */
justify-content: center;
}
.logo {
margin: 3em auto;
width: 200px;
height: 60px;
}
h1.email-title {
font-size: 26px;
font-weight: 500;
margin-bottom: 15px;
color: #252525;
}
a.links_btn {
border: 0;
background: #4C84FF;
color: #fff;
width: 100%%;
height: 50px;
line-height: 50px;
font-size: 16px;
margin: 40px auto;
box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.15);
border-radius: 4px;
outline: none;
cursor: pointer;
transition: all 0.3s;
text-align: center;
display: block;
text-decoration: none;
}
.warm_tips {
color: #757575;
background: #f7f7f7;
padding: 20px;
}
.warm_tips .desc {
margin-bottom: 20px;
}
.qr_warp {
max-width: 140px;
margin: 20px auto;
}
.qr_warp img {
max-width: 100%%;
max-height: 100%%;
}
.email-footer {
margin-top: 2em;
}
#reset-password-email {
max-width: 500px;
}
#reset-password-email .accout_email {
color: #4C84FF;
display: block;
margin-bottom: 20px;
}
</style>
</head>
<body>
<section class="email_warp">
<div id="reset-password-email">
<h1 class="email-title">
这是一封系统发出的邮件:
</h1>
<p>这是我定制的一封邮件:<span style="color:blue">%s</span></p>
<p>请注意,当收到邮件通知时,项目已更新完成,请忽略并关闭此邮件。</p>
<a class="links_btn" href="https://www.baidu.com/">查看部署信息</a>
<div class="warm_tips">
<div class="desc">
为安全起见,以上按钮为一次性链接,且仅在24小时内有效,请您尽快完成操作。
</div>
<p>如有任何疑问,请通过如下方式与我们联系:</p>
<p>邮箱:xxxx@qq.com</p>
<p>本邮件由系统自动发送,请勿回复。</p>
</div>
</div>
</section>
</body>
</html>
""") % (msg)
if email is None:
print("没有配置发送邮箱,本次执行不发送邮件")
exit(1)
qqmail = QQMailMan(user, password)
qqmail.connect()
to_addrs = email
content = content_template
biaoti = "我是一封邮件标题"
qqmail.mail_to(to_addrs, biaoti, content)
qqmail.close()
if __name__ == "__main__":
mailsend()