python发送电子邮件全球名称' msg'没有定义

时间:2016-08-15 08:40:00

标签: python python-2.7

我有一个用Python发送电子邮件的代码段。我收到错误NameError:全局名称' msg'未定义:

     Traceback (most recent call last):
  File "E:/test_runners 2 edit project in progress add more tests/selenium_regression_test_5_1_1/Email/email_selenium_report.py", line 32, in <module>
    report.send_report_summary_from_htmltestrunner_selenium_report()
  File "E:\test_runners 2 edit project in progress add more tests\selenium_regression_test_5_1_1\Email\report.py", line 516, in send_report_summary_from_htmltestrunner_selenium_report
    msg['Subject'] = "ClearCore 5_1_1 Automated GUI Test"
NameError: global name 'msg' is not defined

我的代码是:

def send_report_summary_from_htmltestrunner_selenium_report():
    print extract_only_header_from_summary_from_report_htmltestrunner()
    print extract_header_count__from_summary_from_report_htmltestrunner()
    all_testcases = list(extract_testcases_from_report_htmltestrunner())
    # print all_data
    pprint.pprint(all_testcases)


    msg['Subject'] = "ClearCore 5_1_1 Automated GUI Test"
    msg['body'] = "\n ClearCore 5_1_1 Automated GUI Test_IE11_Selenium_VM   \n " + extract_only_header_from_summary_from_report_htmltestrunner() + "\n extract_header_count__from_summary_from_report_htmltestrunner() \n" + list(
        extract_testcases_from_report_htmltestrunner()) + "\n Report location = : \\storage-1\Testing\Selenium_Test_Report_Results\ClearCore_5_1_1\Selenium VM\IE11 \n"

    msg['to'] = "cc4_server_dev@company.onmicrosoft.com", "riazladhani@company.com"
    msg['From'] = "system@company.com"

    s = smtplib.SMTP()
    s.connect(host=SMTP_SERVER)
    s.sendmail(msg['From'], msg['To'], msg['body'], msg.as_string())
    s.close()


def extract_only_header_from_summary_from_report_htmltestrunner():
    filename = (r"E:\test_runners 2 edit project\selenium_regression_test_5_1_1\TestReport\ClearCore501_Automated_GUI_TestReport.html")
    html_report_part = open(filename,'r')
    soup = BeautifulSoup(html_report_part, "html.parser")
    table = soup.select_one("#result_table")

    #Create list here...
    results = []

    headers = [td.text for td in table.select_one("#header_row").find_all("td")[1:-1]]
#    print(" ".join(headers))

    #Don't forget to append header (if you want)
    results.append(headers)
    return results

def extract_header_count__from_summary_from_report_htmltestrunner():
    filename = (r"E:\test_runners 2 edit project\selenium_regression_test_5_1_1\TestReport\ClearCore501_Automated_GUI_TestReport.html")
    html_report_part = open(filename,'r')
    soup = BeautifulSoup(html_report_part, "html.parser")
    table = soup.select_one("#result_table")

    #Create list here...
    results = []
    for row in table.select("tr.passClass"):
        #Store row string in variable and append before printing
        row_str = " ".join([td.text for td in row.find_all("td")[1:-1]])
        results.append(row_str)
#        print(row_str)

    return results


def extract_testcases_from_report_htmltestrunner():
    filename = (r"E:\test_runners 2 edit project\selenium_regression_test_5_1_1\TestReport\ClearCore501_Automated_GUI_TestReport.html")
    html_report_part = open(filename,'r')
    soup = BeautifulSoup(html_report_part, "html.parser")
    for div in soup.select("#result_table tr div.testcase"):
          yield div.text.strip().encode('utf-8'), div.find_next("a").text.strip().encode('utf-8')

如何解决此错误? 我试图将msg = MIMEText(文字,&#34; html&#34;)放在信息上方[&#39;主题&#39;]它不喜欢。

谢谢Riaz

3 个答案:

答案 0 :(得分:1)

您忘记在msg函数中声明send_report_summary_from_htmltestrunner_selenium_report变量。 你可以这样做,例如像这样:

from email.mime.text import MIMEText
msg = MIMEText('body of your message')

答案 1 :(得分:0)

包含此代码可能会纠正您的问题:

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

msg=MIMEMultipart()

答案 2 :(得分:-1)

您似乎还没有正确声明变量msg。 我假设你在我们在这里看到的代码中声明它。 但是请确保为它分配一些值,即使是None,在这种情况下,例如它应该是MIMEText(content, "html")。 正如本answer中所述。

如果这没有帮助,请发布您在定义msg

之后即将获得的新例外情况