用django 发送邮件例子
By:Roy.LiuLast updated:2013-02-28
很久前,直接用python发过邮件,还写过一篇文章记录下来,http://www.yihaomen.com/article/python/188.htm,最近用django开发web应用,也需要给用户发邮件,虽然仍然可以采用python 发邮件的方式,但django本身其实是有发邮件这个工具类的,可以直接使用:
有兴趣的,可以打开 from django.core.mail import EmailMessage 查看源代码,这是django 本身的代码。
#encoding:utf-8 import threading from django.core.mail import EmailMessage import time class EmailThread(threading.Thread): """ 发送账号激活邮件线程 """ def __init__(self,subject='', body='', from_email=None, to=None): self.subject = subject self.body = body self.from_email = from_email self.to = to self.fail_silently = True threading.Thread.__init__(self) def run(self): msg_email = EmailMessage(self.subject,self.body,self.from_email,self.to) msg_email.content_subtype = 'html' try: msg_email.send(self.fail_silently) except Exception,e: # 记录错误日志 log = open('email_error.log','a') log.write('%s %s\n' %(time.strftime('%Y-%m-%d %H:%M:%S'),e) ) log.close() def send_email(subject='', body='', from_email=None, to=[]): """ 发送邮件方法 """ email = EmailThread(subject, body, from_email, to) email.start() email.join()
有兴趣的,可以打开 from django.core.mail import EmailMessage 查看源代码,这是django 本身的代码。
From:一号门
Previous:python解析xml的简单例子
Next:django 重设(修改)用户密码
COMMENTS