美文网首页
PYTHON发送邮件

PYTHON发送邮件

作者: 9016 | 来源:发表于2021-07-23 14:30 被阅读0次
    from email.mime.text import MIMEText
    from email.header import Header
    from email.mime.multipart import MIMEMultipart
    import smtplib
    
    
    def sendEmail(smtpserver, username, password, sender, receiver, subject, content, attLocationName, attFileName):
        #发送带有附件的邮件
        # smtpserver/邮件服务器地址: str  'smtp.eee-inc.cn'
        # username/登录账号: str  'autotest@eee-inc.cn'
        # password/登录密码: str  '12345678'
        # sender/邮件发送者账号: str  'autotest@eee-inc.cn'
        # receiver/邮件接收者账号: str  ['guojie@eee-inc.cn']
        # subject/邮件标题: str  '司机端自动化测试报告'
        # content/邮件正文: str  '司机端自动化报告以附件形式展示,如附件'
        # attLocationName/待发送附件文件名: str  '司机端自动化测试报告.html'
        # attFileName/邮件中附件名: str  '司机端自动化测试报告.html'
    
        # 构造邮件体
        msg = MIMEMultipart()
        msg['From'] = '{}'.format(sender)
        msg['To'] = ','.join(receiver)
        msg['Subject'] = Header(subject, 'utf-8')
        contentMsg = MIMEText(content, "plain", "utf-8")
        msg.attach(contentMsg)
    
        # 构造附件
        att = MIMEText(open(attLocationName, 'rb').read(), 'base64', 'utf-8')
        att["Content-Type"] = 'application/octet-stream'
        att.add_header('Content-Disposition', 'attachment', filename=attFileName)
        msg.attach(att)
    
        # 发送邮件
        try:
            serverEmail = smtplib.SMTP()
            serverEmail.connect(smtpserver)
            serverEmail.set_debuglevel(0)
    
            serverEmail.login(username, password)
            serverEmail.sendmail(sender, receiver, msg.as_string())
    
            serverEmail.quit()
        except Exception as err:
            print('发送邮件错误: %s' % err)
    

    相关文章

      网友评论

          本文标题:PYTHON发送邮件

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