使用python

时间:2018-10-11 09:37:56

标签: python python-2.7 email smtp smtplib

我有一个脚本,该脚本执行硒检查,然后使用ms Outlook发送带有屏幕截图的电子邮件。脚本工作正常。问题是,有时即使测试成功(尝试阻止),它也会发送失败电子邮件(阻止除外)并且是间歇性的。

有时候我得到smtplib.SMTPServerDisconnected: Server not connectedsmtplib.SMTPServerDisconnected: Connection unexpectedly closed

我添加了以下代码以再次打开连接,以防连接被关闭但没有运气。

        try:
            conn = server.noop()[0]
            print("---CONNECTION CODE---", conn)
            if conn != 250:
                server = smtplib.SMTP('mail.xxx.com', 587)

        except:
            pass

我的脚本:

class MyTesting:

def AutomationTesting(self):

    path = 'C:\\xxxx'

    try:
        ## Selemium test code

        msg['From'] = sender
        msg['To'] = ", ".join(receiver)
        msg['Subject'] = 'Automation Testing Success'

        body = 'Hello, \n\nAttached is the screenshot for the reference. \n \nRegards'
        msg.attach(MIMEText(body, 'plain'))
        attachment = open('C:\\{}'.format(filename), 'rb')

        part = MIMEBase('application', 'octet-stream')
        part.set_payload((attachment).read())
        # part.set_payload((attachment).read())
        encoders.encode_base64(part)
        part.add_header('Content-Disposition', "attachment; filename= " + filename)

        msg.attach(part)

        server = smtplib.SMTP('mail.xxx.com', 587)

        conn = server.noop()[0]

        try:
            conn = server.noop()[0]
            print("---CONNECTION CODE---", conn)
            if conn != 250:
                server = smtplib.SMTP('mail.xxx.com', 587)

        except:
            pass

        server.starttls()
        server.login(sender, "Heavensdoor11")
        text = msg.as_string()
        server.sendmail(sender, receiver, text)
        # server.quit()
        time.sleep(4)
        print("Email sent.")
    except:
        print("Unable to test site.")

        file_write = open(log_file, "a+")
        file_write.write("automation failure {}\n".format(timestamp))
        file_write.close()

        filename_2 = 'Fidelity-Failure' + timestamp + '.png'
        driver.get_screenshot_as_file("C:\\{}".format(filename_2))
        sender = 'sender '
        # receiver = receiver '

        msg = MIMEMultipart()
        print("Sending an email to {} ...".format(receiver))

        msg['From'] = sender
        msg['To'] = ", ".join(receiver)
        msg['Subject'] = 'Automation Testing Failure'

        body = 'Hi Team, \n\nAutomation testing for customer has been failed.'
        msg.attach(MIMEText(body, 'plain'))
        attachment = open('C:\\{}'.format(filename_2), 'rb')

        part = MIMEBase('application', 'octet-stream')
        part.set_payload((attachment).read())
        # part.set_payload((attachment).read())
        encoders.encode_base64(part)
        part.add_header('Content-Disposition', "attachment; filename= " + filename_2)

        msg.attach(part)

        server = smtplib.SMTP('mail.xxx.com', 587)

        server.starttls()
        server.login(sender, "mypassword")
        text = msg.as_string()
        server.sendmail(sender, receiver, text)
        # server.quit()
        time.sleep(4)
        print("Email sent.")


    finally:
        driver.close()

我们非常感谢您的帮助。

0 个答案:

没有答案