python3.x 群发qq邮件

 """邮件发送方法
    source = {
            'usrname':xxxx,
            'passwd':xxxx,
            'server':'smtp.qq.com',
            'port':465

    }
    destination = [usr1,usr2,....]
    data = {
        'text':文本数据,
        'attfile':[附件2的路径,附件1的路径,....],
        'html':html数据
    }
    """
    def notice(self, sendata):

        #数据组装
        source = {
            'usrname': self.usrname,
            'passwd': self.passwd,
            'server': self.smtpserver,
            'port':self.port
        }

        data = {
            'text': sendata,
            'attfile': [r"filepath1",r"filepath2"],
            'html': r"

你好!

"
} self.send(source,self.receviers,data) def send(self,source,destination,data): #选择发送邮件的方式 if 'attfile' not in data or data['attfile'] is None: __send_no_att(source,destination,data) else: __send_att(source,destination,data) """ 不带附件的邮件 """ @staticmethod def __send_no_att(source, destination, data): mimetext = None isHtml = False if 'html' in data and data['html'] is not None: mimetext = data['html'] isHtml = True else: mimetext = data['text'] # 设置邮件主体内容 msgRoot = MIMEText( mimetext, _subtype='html' if isHtml else 'plain', _charset='utf-8' ) # 设置邮件的主题 msgRoot['From'] = source['usrname'] msgRoot['Subject'] = Header("测试报告", 'utf-8') msgRoot['TO'] = ",".join(destination) # 发送 stmp = smtplib.SMTP_SSL(source['server'], source['port']) stmp.connect(source['server']) stmp.login(source['usrname'], source['passwd']) stmp.sendmail(source['usrname'], destination, msgRoot.as_string()) stmp.quit() """ 带附件的邮件 """ @staticmethod def __send_att(source, destination, data): mimetext = None isHtml = False # 判断正文数据是否为 html 格式 if 'html' in data and data['html'] is not None: mimetext = data['html'] isHtml = True else: mimetext = data['text'] #构造邮件属性 msgRoot = MIMEMultipart() # 设置邮件的主题 msgRoot['From'] = source['usrname'] msgRoot['Subject'] = Header("测试报告", 'utf-8') msgRoot['TO'] = ",".join(destination) # 构造邮件正文 msgRoot.attach(MIMEText(mimetext, 'html', 'utf-8')) # 构造附件 attfiles = data['attfile'] for file in attfiles: att = MIMEText(open(file, 'rb').read(), 'base64', 'utf-8') att["Content-Type"] = r'application/octet-stream' att["Content-Disposition"] = "attachment; filename="+file.split("\\")[-1] msgRoot.attach(att) # 清空 att del att # 发送 stmp = smtplib.SMTP_SSL(source['server'], source['port']) stmp.connect(source['server']) stmp.login(source['usrname'], source['passwd']) stmp.sendmail(source['usrname'], destination, msgRoot.as_string()) stmp.quit()

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