Django—邮件发送+激活账户

  • 配置


    # smtp服务的邮箱服务器
    EMAIL_HOST = 'smtp.126.com'
    # smtp服务固定的端口是25
    EMAIL_PORT = 25
    
    #发送邮件的邮箱
    EMAIL_HOST_USER = '[email protected]'
    #在邮箱中设置的客户端授权密码
    EMAIL_HOST_PASSWORD = 'xxxxxxx'
    #收件人看到的发件人 <此处要和发送邮件的邮箱相同>
    EMAIL_FROM = 'python<[email protected]>'
  • 发送


    def mail_send(request):
        # 发送一封邮件
        # res = send_mail('邮件内容',EMAIL_FROM,
        #                 ['[email protected]','[email protected]'])
    
        # 发送多封邮件
        # message1 = ('邮件内容', '<b>邮件内容</b>', EMAIL_FROM, ['写信人@126.com','收信人@163.com'])
        # message2 = ('邮件内容', '<b>邮件内容</b>', EMAIL_FROM, ['写信人@126.com','收信人@163.com'])
        # send_mass_mail((message1, message2), fail_silently=False)
    
        # html格式邮件
        html_content = loader.get_template('active.html').render({'username': '小花猫'})
        msg = EmailMultiAlternatives('内容', from_email=EMAIL_FROM, to=['[email protected]'])
        msg.attach_alternative(html_content, "text/html")
        msg.send()
        return HttpResponse("邮件发送")
    
  • 激活


    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <h2>{{ username }}的账号未激活,请点击下面的连接激活账号</h2>
    
    </body>
    </html>
发布了199 篇原创文章 · 获赞 6 · 访问量 2455

猜你喜欢

转载自blog.csdn.net/piduocheng0577/article/details/105031102
今日推荐