在Django中发送邮件 (using gmail)

在django中配置使用gmail来发送邮件,只要在setting.py文件中加入
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = '25'
EMAIL_HOST_USER = 'username'  #加[email protected]也可以。
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.', '[email protected]',['[email protected]'])
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()

你可能感兴趣的:(html,django,python,Gmail)