美文网首页
python 发送带附件的邮件

python 发送带附件的邮件

作者: 曾柏超 | 来源:发表于2018-01-04 23:29 被阅读0次
    
    #/usr/bin/env python
    #coding=utf-8
    #
    #from_addr = 'xx@163.com'
    #password = 'xxx'
    #to_addr = 'xxx@qq.com'
    #smtp_server = 'smtp.163.com'
    
    import smtplib
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    from email.mime.image import MIMEImage
    from email.mime.application import MIMEApplication
    
    HOST = "smtp.163.com"
    SUBJECT = u"官网业务服务质量周报"
    TO = "xxx@qq.com"
    FROM = "xxx@163.com"
    
    def addimg(src,imgid):
        fp = open(src, 'rb')
        msgImage = MIMEImage(fp.read())
        fp.close()
        msgImage.add_header('Content-ID', imgid)
        return msgImage
    
    msg = MIMEMultipart('related')
    msgtext = MIMEText("<font color=red>官网业务周平均延时图表:<br><img src=\"cid:weekly\" border=\"1\"><br>详细内容见附件。</font>","html","utf-8")
    msg.attach(msgtext)
    msg.attach(addimg("weekly.png","weekly"))
    
    attach = MIMEApplication(open('曾柏超的简历.doc','rb').read())
    attach.add_header('Content-Disposition', 'attachment', filename="曾柏超的简历.doc")
    msg.attach(attach)
    
    
    
    
    msg['Subject'] = SUBJECT
    msg['From']=FROM
    msg['To']=TO
    try:
        server = smtplib.SMTP()
        server.connect(HOST,"25")
        server.starttls()
        server.login("xxxx@163.com","xxxx")
        server.sendmail(FROM, TO, msg.as_string())
        server.quit()
        print "邮件发送成功!"
    except Exception, e:
        print "失败:"+str(e)
    
    
    

    相关文章

      网友评论

          本文标题:python 发送带附件的邮件

          本文链接:https://www.haomeiwen.com/subject/pyadnxtx.html