email

    [发送邮件]

发送邮件内容

        #!/usr/bin/python
        #encoding:utf8
        # 导入 smtplib 和 MIMEText
        import smtplib
        from email.mime.text import MIMEText

定义发送列表

        mailto_list=["272121935@qq.com","272121935@163.com"]

        # 设置服务器名称、用户名、密码以及邮件后缀
        mail_host = "smtp.163.com"
        mail_user = "mailuser"
        mail_pass = "password"
        mail_postfix="163.com"

发送邮件函数

        def send_mail(to_list, sub):
            me = mail_user + "<"+mail_user+"@"+mail_postfix+">"
            fp = open('context.txt')
            msg = MIMEText(fp.read(),_charset="utf-8")
            fp.close()
            msg['Subject'] = sub
            msg['From'] = me
            msg['To'] = ";".join(to_list)
            try:
                send_smtp = smtplib.SMTP()
                send_smtp.connect(mail_host)
                send_smtp.login(mail_user, mail_pass)
                send_smtp.sendmail(me, to_list, msg.as_string())
                send_smtp.close()
                return True
            except Exception, e:
                print str(e)
                return False

        if send_mail(mailto_list,"标题"):
            print "测试成功"
        else:
            print "测试失败"

发送附件

        #!/usr/bin/python
        #encoding:utf8
        import smtplib
        from email.mime.multipart import MIMEMultipart
        from email.mime.base import MIMEBase
        from email import encoders

        def send_mail(to_list, sub, filename):
            me = mail_user + "<"+mail_user+"@"+mail_postfix+">"
            msg = MIMEMultipart()
            msg['Subject'] = sub
            msg['From'] = me
            msg['To'] = ";".join(to_list)
            submsg = MIMEBase('application', 'x-xz')
            submsg.set_payload(open(filename,'rb').read())
            encoders.encode_base64(submsg)
            submsg.add_header('Content-Disposition', 'attachment', filename=filename)
            msg.attach(submsg)
            try:
                send_smtp = smtplib.SMTP()
                send_smtp.connect(mail_host)
                send_smtp.login(mail_user, mail_pass)
                send_smtp.sendmail(me, to_list, msg.as_string())
                send_smtp.close()
                return True
            except Exception, e:
                print str(e)[1]
                return False

设置服务器名称、用户名、密码以及邮件后缀

        mail_host = "smtp.163.com"
        mail_user = "xuesong"
        mail_pass = "mailpasswd"
        mail_postfix = "163.com"
        mailto_list = ["272121935@qq.com","quanzhou722@163.com"]
        title = 'check'
        filename = 'file_check.html'
        if send_mail(mailto_list,title,filename):
            print "发送成功"
        else:
            print "发送失败"
文档更新时间: 2018-11-21 17:25   作者:RuM