发送Email给用户-使用163邮箱的smtp

  • A+
所属分类:Django框架

在setting.py文件中添加

EMAIL_HOST = 'smtp.163.com'

EMAIL_PORT = 25

EMAIL_HOST_USER = 'youremail@163.com'

EMAIL_HOST_PASSWORD = 'yourpassword'

EMAIL_USE_TLS = False

EMAIL_FROM = 'youremail@163.com' #要与EMAIL_HOST_USER一致

新建email_send.py文件

from random import Random

from django.core.mail import send_mail

from users.models import EmailVerifyRecord

from MmOnline.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 = '暮梅在线教育注册激活链接'

       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:

          pass

def random_str(random_length=10): #生成随机字符串

    str = ''

    chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789'

    length = len(chars) - 1

    random = Random()

    for i in range(random_length):

       str += chars[random.randint(0, length)]

    return str

  • 微信
  • 等你哟
  • weinxin
  • 微信公众号
  • mac软件资源等你来拿
  • weinxin
小羊同学

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: