zoukankan      html  css  js  c++  java
  • django发送邮箱

    要用django发送邮箱之前需要在setting中配置一下

    EMAIL_HOST = 'smtp.qq.com'
    EMAIL_PORT = 25
    EMAIL_HOST_USER = 'xxx@qq.com'
    EMAIL_HOST_PASSWORD = 'xxx'
    EMAIL_USE_TLS = True
    EMAIL_FROM = 'xxx@qq.com'

    下面以发送激活码为例子

    from random import Random
    import string
    
    from django.core.mail import send_mail
    
    from users.models import EmailVerifyRecord
    from imooc.settings import EMAIL_FROM
    
    
    def send_register_email(email, send_type='register'):
        email_record = EmailVerifyRecord()
        code = random_str(16)
        email_record.code = code
        email_record.email = email
        email_record.send_type = send_type
        email_record.save()
    
        if send_type == 'register':
            email_title = 'xx注册激活链接’
            email_body = '请点击下面的链接激活你的账号:http://127.0.0.1:8000/active/{0}'.format(code)
            send_status = send_mail(email_title, email_body, EMAIL_FROM, [email])
            if send_status:
                # TODO
                print('发送成功')
    
        elif send_type == 'forget':
            email_title = 'xx密码重置链接'
            email_body = '请点击下面的链接重置你的密码:http://127.0.0.1:8000/reset/{0}'.format(code)
    
            send_status = send_mail(email_title, email_body, EMAIL_FROM, [email])
            if send_status:
                # TODO 提示发送成功
                pass
    
    
    def random_str(random_length=16):
        code = ''
        # 26个大小写字母加数字
        chars = string.ascii_letters + str(string.digits)
        length = len(chars) - 1
    
        for i in range(random_length):
            code += chars[Random().randint(0, length)]
        return code

    只要调用一个简单的send_mail函数,传入标题,内容,发送方,以及一个包含接收方的列表

  • 相关阅读:
    微服务Kong(三)——添加一个API
    微服务Kong(二)——快速入门
    Oracle 12c 创建用户
    汉字转拼音工具
    GITHUB一个新的项目发布
    Log4J 配置文件模板及代码说明
    Log4J2 配置文件模板及代码说明
    java 写入数据到Excel文件中_Demo
    关于数据库NULL值的几个问题思考
    详解Spring中的Profile
  • 原文地址:https://www.cnblogs.com/lgh344902118/p/7052652.html
Copyright © 2011-2022 走看看