setting里面
# ########## email ##########
# SMTP_SERVER = 'smtp.qq.com'
# SMTP_PORT = 465
# SMTP_OVER_SSL = True
# SMTP_CONNECTION_TIMEOUT = 10
# EMAIL_PASSWORD = 'XXX' # 填写生成的授权码
# EMAIL_SENDER = 'XXX@qq.com'
# EMAIL_RECIPIENTS = [EMAIL_SENDER]
# EMAIL_SUBJECT = 'Email from #scrapydweb'
# ########## email ##########
# 用于发送邮件的模块
import smtplib
from email.mime.text import MIMEText
from app.settings import SMTP_SERVER, SMTP_PORT, EMAIL_SENDER, EMAIL_RECIPIENTS, EMAIL_PASSWORD
# QQ邮件
# 1> 配置邮箱SMTP服务器的主机地址,将来使用这个服务器收发邮件。
HOST = SMTP_SERVER
# # 2> 配置服务的端口,默认的邮件端口是25.
PORT = SMTP_PORT
# # 3> 指定发件人和收件人。
FROM = EMAIL_SENDER
TO = EMAIL_RECIPIENTS
PASSWORD = EMAIL_PASSWORD
# # 4> 邮件标题
SUBJECT = '这是一封测试邮件'
# # 5> 邮件内容
CONTENT = '这是xxxxx发送过来的邮件。请注意查收!'
CONTENT2 = """
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html";charset="utf-8">
<title>Dcp's python email</title>
</head>
<body>
<p>Dcp's babys:</p>
<p><h1>Happy New Year!</h1></p>
</body>
</html>
"""
# # 创建邮件发送对象
# # 普通的邮件发送形式
# smtp_obj = smtplib.SMTP()
# # 数据在传输过程中会被加密的邮件发送形式。
smtp_obj = smtplib.SMTP_SSL(HOST)
#
# # 需要进行发件人的认证,授权。
# # smtp_obj就是一个第三方客户端对象
smtp_obj.connect(host=HOST, port=PORT)
# # 如果使用第三方客户端登录,要求使用授权码,不能使用真实密码,防止密码泄露。
res = smtp_obj.login(user=FROM, password=PASSWORD)
print('登录结果:', res)
#
# # 发送邮件
# msg = '\n'.join(['From: {}'.format(FROM), 'To: {}'.format(TO), 'Subject: {}'.format(SUBJECT), '', CONTENT2])
# print(msg)
# print(type(msg))
# smtp_obj.sendmail(from_addr=FROM, to_addrs=TO, msg=msg.encode('utf-8'))
msg = MIMEText(CONTENT2, 'html', 'utf-8') # 邮件内容,三个参数:第一个为文本内容,第二个 html 设置文本格式,第三个 utf-8 设置编码
msg["Subject"] = "Your Red Bag is Coming.."
msg["From"] = FROM
msg['To'] = ";".join(TO) # 这里注意如果是一个接收人列表要这么写!
# print(msg)
# print(msg.as_string())
# print(type(msg))
smtp_obj.sendmail(from_addr=FROM, to_addrs=TO, msg=msg.as_string())