在django中配置使用gmail来发送邮件,只要在setting.py文件中加入
ps: 改过setting中的参数需要exit()退出shell,再次python manage.py shell进入。
发送html邮件
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = '25'
EMAIL_HOST_USER = 'username' #加username@gmail.com也可以。
EMAIL_HOST_PASSWORD = 'password'
EMAIL_USE_TLS = True 测试:python manage.py shell
In [1]: from django.core.mail import send_mail
In [2]: send_mail('Subject', 'Body of the message.', 'from_username@gmail.com',['to_username@gmail.com'])
Out[2]: 1#输出一表示成功ps: 改过setting中的参数需要exit()退出shell,再次python manage.py shell进入。
发送html邮件
from django.core.mail import EmailMessage
from django.template import loader
from settings import EMAIL_HOST_USER
def send_html_mail(subject, html_content, recipient_list):
msg = EmailMessage(subject, html_content, EMAIL_HOST_USER, recipient_list)
msg.content_subtype = "html" # Main content is now text/html
msg.send()
本文介绍如何在 Django 项目中配置 Gmail 作为邮件发送服务。通过设置特定参数,如SMTP主机、端口及身份验证信息,可以实现从 Django 应用发送纯文本与 HTML 格式的邮件。
1403

被折叠的 条评论
为什么被折叠?



