zoukankan      html  css  js  c++  java
  • python SMTP发送邮件

     这是个人在项目中抽取的代码,自己写的utils的通用模块,使用的框架是tronado,包括了SMTP方式发送邮件,如有特别需要(POP)可以联系我或者自己扩展,刚学python不久,仅供参考,例子如下。

    import smtplib
    
    from email.mime.text import MIMEText
    from email.mime.multipart import MIMEMultipart
    
    def send_mail(auth,to_list,sub,content,file_name):
        print file_name
        mail_user, mail_pass, mail_host, mail_postfix = auth
        me=mail_user+"<"+mail_user+"@"+mail_postfix+">"
        #Create Mail
        msg = MIMEMultipart('alternative')
        msgText = MIMEText(content,'html', 'utf-8')
        msg.attach(msgText)
        msg['Subject'] = sub
        msg['From'] = me
        msg['To'] = ";".join(to_list)
        if file_name != None:
            att = MIMEText(open(file_name, 'rb').read(), 'base64', 'gb2312')
            att["Content-Type"] = 'application/octet-stream'
            att["Content-Disposition"] = 'attachment; filename="%s"'%file_name.split('/')[-1].strip()
            msg.attach(att) #attachment
            #Send it!
        s = smtplib.SMTP()
        s.connect(mail_host)
        try:
            s.login(mail_user,mail_pass)
            print 'login email server'
        except Exception, e:
            pass
        s.sendmail(me,to_list,msg.as_string())
        s.close()
        print 'success!'
  • 相关阅读:
    构建账户系统
    我的vim配置
    document.readyState和xmlhttp.onreadystatechange
    RSA非对称算法实现HTTP密码加密传输
    css3动画学习资料整理
    H5缓存机制学习记录
    [leetcode]3Sum Closest
    [leetcode]Word Ladder II
    [leetcode]Two Sum
    [leetcode]Regular Expression Matching
  • 原文地址:https://www.cnblogs.com/jingtyu/p/7200862.html
Copyright © 2011-2022 走看看