python 发送邮件

使用 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

转载于:https://my.oschina.net/shichangcheng/blog/671010

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值