Riaz Ladhani Riaz Ladhani - 3 months ago 7
Python Question

python send email global name 'msg' is not defined

I have a code snippet to send an email in Python. I am getting the error NameError: global name 'msg' is not defined:

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


My code is:

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')


How do I solve this error?
I tried to put msg = MIMEText(text, "html") above the msg['Subject'] It does not like that.

Thanks, Riaz

Answer

You forgot to declare msg variable inside send_report_summary_from_htmltestrunner_selenium_report function. You can do it e.g. like this:

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