import random
import string
from smtplib import SMTP_SSL
from email.mime.text import MIMEText
from email.header import Header
# 发送邮件,参数为收件邮箱地址和验证码
def send_email(receiver, code):
sender = "测试 <[email protected]>" # 邮箱账号,签名
# 邮件内容,支持HTML、CSS
content = f"<br/>欢迎注册年少有为博客账号,您的邮箱验证码为:<span style='color:red; font-size:20px'>{code}</span>,请复制到注册窗口中完成注册,感谢你的支持。</br>"
# 实例化邮件对象,并指定邮件的关键信息
message = MIMEText(content, "html", 'utf-8')
# 指定邮件的标题,utf-8 编码
message["Subject"] = Header("注册验证码", 'utf-8')
message["From"] = sender # 指定发件信息
message["To"] = receiver # 收件人邮箱地址
smtpObj = SMTP_SSL("smtp.yeah.net") # 与网易邮箱服务器连接
# 通过你的邮箱账号和获取到的授权登录网易邮箱
smtpObj.login(user="[email protected]", password="你的邮箱密码")
# 指定发件人,收件人和邮件内容
smtpObj.sendmail(sender, receiver, str(message))
smtpObj.quit()
# 生成6位随机字符串作为邮箱验证码
def gen_email_code():
str = random.sample(string.ascii_letters + string.digits, 6)
return "".join(str)
if __name__ == '__main__':
send_email("[email protected]", gen_email_code())
标签:__,Python,课时,import,邮箱,message,email,邮件
From: https://blog.csdn.net/xzs51job/article/details/142449585