Python Django发送邮件的配置方法


Python #django #发送邮件2012-11-16 23:17
settings.py文件中添加配置:

#django email config
EMAIL_HOST='smtp.gmail.com'
EMAIL_HOST_USER='yige@gmail.com'
EMAIL_HOST_PASSWORD='yige.org'
EMAIL_USE_TLS = True  #如果是Gmail,这里一定要是True

发送普通邮件:

from django.core.mail import send_mail
send_mail('subject',                  #主题
          'body',                     #邮件内容
          'yige@gmail.com',       #发件人,可以是英文名
          ['webadmin@yige.org'],  #收件人,可以是多个
          fail_silently=True          #发送异常时不提示
          )

发送html超文本邮件:

# -*- coding: utf-8 -*-
# http://yige.org/python/
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_content = loader.render_to_string(
               template_path,            #需要渲染的html模板
               {'paramters':paramters}   #需要传给模板的参数
               )
 
send_html_mail(u'测试邮件主题',
               html_content,
               ['yige@gmail.com','webadmin@yige.org']
               )


相关文章

粤ICP备11097351号-1