使用 tornadomail组件发送邮件
@property
def mail_connection(self):
#mail_smtp 邮件服务器的域名 smtp.ym.163.com
#mail_smtp_port 邮件服务器的端口 25
#mail_address 发送邮箱的用户名
#mail_password 发送邮箱的密码
return EmailBackend(
options.mail_smtp, options.mail_smtp_port,
options.mail_address, options.mail_password,
True,
template_loader=template.Loader(os.path.join(os.path.dirname(__file__), "tpl"))
)
---------------------------
@property
def mail_connection(self):
return self.application.mail_connection
---------------------------
class testEmailHandler(BaseHandler):
def post(self, *args, **kwargs):
#邮件发送完毕后的回调方式
def _finish(num):
print 'sended %d message(s)' % num
templatepath = os.path.join(os.path.dirname(__file__), 'mail', 'mail_reset_password.html')
message = EmailFromTemplate(
options.mail_forgot_title,
templatepath,
#templatepath 中需要的参数参数
params={'host':options.host,'name': "shi",'token':"shi",'ua':"shi"},
#from_email 和 mail_address 值一样就ok
from_email=options.mail_from,
to=["shi.changcheng@puyuntech.com"],
connection=self.mail_connection
)
#添加附件 只需要加入附近的路径就行
message.attach_file("./README.md")
message.send(callback=_finish)
self.write("success")
存在的问题:
在window环境下,无法发送,总是提示报错,但是在liunx(阿里云) 就可以正常发送,具体原因我还没找到,如果大家知道原因,请告知我,谢谢
tornadomail git网址 https://github.com/equeny/tornadomail