Django project practice (mall): 10. Supplement: Django sends emails

Insert picture description here

(According to the content of teacher's live broadcast)
  • Django provides the class django.core.mail for sending mail

One, Django sending mail process analysis

Insert picture description here

  • Configure related parameters, send mail information to smtp server by django’s send_mail() method,
  • Email sent by smtp server to user mailbox

1. Introduction to send_mail() method

  • The django.core.mail module provides send_mail() to send mail.
  • send_mail(subject, message, from_email, recipient_list, html_message=None)
    • subject: the subject of the message
    • message: normal mail body, normal string
    • from_email: sender
    • recipient_list: recipient list
    • html_message: The body of the multimedia message, which can be an html string

Two, send mail configuration parameters

# 发送邮件的配置参数
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'   # 指定邮件后端 django 
EMAIL_HOST = 'smtp.126.com'  # 发邮件主机,邮箱的smtp服务器地址
EMAIL_PORT = 25     # 发邮件端口
EMAIL_HOST_USER = '[email protected]'  # 授权的邮箱:发件箱
EMAIL_HOST_PASSWORD = ''  # 邮箱授权时获得的密码,非注册登录密码
EMAIL_FROM = '老萝卜<[email protected]>'  # 发件人抬头

Ready to send mail server

  • 1. Enter the mailbox, click to enter the "Settings" interface
    Insert picture description here

    1. POP3/SMTP/IMAP, open service
      Insert picture description here
    • According to the prompt, obtain the authorization code (EMAIL_HOST_PASSWORD) through the mobile phone
  • 3. Set the authorization code, remember the set password, login in Django is this password

3. Example of sending email:

Insert picture description here

		subject = "商城邮箱验证"
        html_message = '<p>尊敬的用户您好!</p>' \
                       '<p>感谢您使用商城。</p>' \
                       '<p>您的邮箱为:%s 。请点击此链接激活您的邮箱:</p>' \
                       '<p><a href="%s">%s<a></p>' % (email, 'www.baidu.com', 'www.baidu.com')
        send_mail(subject, '', from_email=settings.EMAIL_FROM, recipient_list=[email], html_message=html_message)

Guess you like

Origin blog.csdn.net/laoluobo76/article/details/113893593