python 发送邮件

来源:互联网 发布:海口seo 编辑:程序博客网 时间:2024/04/30 23:01

 @staticmethod 

    def send_mail(subject, content, mail_list,filename =None):  

        MAIL_FROM = APP_CFG.MAIL_USER_NAME + "<"+APP_CFG.MAIL_USER+">" 

        message = MIMEMultipart()  

        message.attach(MIMEText(content,'plain','utf-8'))  

        message["Subject"] = Header(subject, 'utf-8')

        message["From"] = Header(MAIL_FROM, 'utf-8')

        message["To"] = ";".join(mail_list)  

        if filename != None and os.path.exists(filename):  

            basename = os.path.basename(filename)

            att = MIMEText(open(filename, 'rb').read(),'base64', 'gb2312')  

            att["Content-Type"] ='application/octet-stream'

            att["Content-Disposition"] ='attachment; filename=%s' % basename.encode('gb2312')  

            message.attach(att)  

        smtp = smtplib.SMTP()  

        smtp.connect(APP_CFG.MAIL_HOST)  

        smtp.login(APP_CFG.MAIL_USER, APP_CFG.MAIL_PASS)  

        smtp.sendmail(MAIL_FROM, mail_list, message.as_string())  

        smtp.quit()