python发邮件

# encoding=utf-8
import os
import time
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
import ConfigParser


class SendEmail(object):
    def __init__(self, file_with_path):
        config = ConfigParser.SafeConfigParser()
        config.read('policy.ini')
        self.file_with_path = file_with_path
        self.file_name = os.path.basename(file_with_path)
        self.sender = '[email protected]'
        self.receiver = [value for _, value in config.items('email')]
        self.username = '[email protected]'
        self.password = 'xxw+1033'

    def send(self):
        msgRoot = MIMEMultipart('related')
        str_today = time.strftime("%Y-%m-%d")
        str_title = u'2345王牌浏览器升级测试报告' + str_today
        msgRoot['Subject'] = str_title
        msg = r'''

大家好:
       2345王牌浏览器升级测试报告已自动生成,详见附件。本邮件为脚本自动发送,有疑问请联系徐小卫。




------------------

徐小卫

高级测试工程师

上海二三四五网络科技有限公司/软件研发中心/测试与质控部

上海浦东亮秀路112号Y2座9层

 
''' msgRoot["From"] = Header('徐小卫', 'utf-8') msgText = MIMEText('%s' % msg, 'html', 'utf-8') msgRoot.attach(msgText) att = MIMEText(open(self.file_with_path, 'rb').read(), 'base64', 'utf-8') att["Content-Type"] = 'application/octet-stream' att["Content-Disposition"] = ('attachment; filename="%s"' % self.file_name).encode("utf-8") msgRoot.attach(att) smtp = smtplib.SMTP() smtp.connect('smtp.exmail.qq.com') smtp.login(self.username, self.password) smtp.sendmail(self.sender, self.receiver, msgRoot.as_string()) smtp.quit()

你可能感兴趣的:(python发邮件)