Django中内置了邮件发送功能,被定义在django.core.mail模块中。发送邮件需要使用SMTP服务器,常用的免费服务器有:163、126、QQ,下面以qq邮箱为例。

  1. 注册qq邮箱,然后登录设置
    如何在django中使用SMTP服务发送邮件
  2. 找到设置里面POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务
    如何在django中使用SMTP服务发送邮件
    3.需要发送验证码生成授权码
    如何在django中使用SMTP服务发送邮件
    如何在django中使用SMTP服务发送邮件
    4.找到settings.py文件,中点击下图配置
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.qq.com'
EMAIL_PORT = 25
#发送邮件的邮箱
EMAIL_HOST_USER = '[email protected]'
#在邮箱中设置的客户端授权密码
EMAIL_HOST_PASSWORD = ''cgnyfwpvcharbbed"
#收件人看到的发件人
EMAIL_FROM = 'python<[email protected]>'

5在views.py文件中新建视图send。

from django.conf import settings
from django.core.mail import send_mail

...
def send(request):
		subject = '主题'	#主题
        message = ''"		#内容
        sender = settings.EMAIL_FROM		#发送邮箱,已经在settings.py设置,直接导入
        receiver = [email]		#目标邮箱
        html_message = '<h1>%s</h1>'%content		#发送html格式
        send_mail(subject,message,sender,receiver,html_message=html_message)

send()		#使用函数

相关文章:

  • 2022-12-23
  • 2021-10-24
  • 2022-01-18
  • 2021-12-19
  • 2021-07-04
  • 2022-12-23
  • 2021-06-28
  • 2021-12-01
猜你喜欢
  • 2021-05-16
  • 2021-11-20
  • 2022-01-04
  • 2022-12-23
  • 2021-12-17
  • 2021-07-27
相关资源
相似解决方案