我编写了一个程序,该程序使用python smtp库发送包含结果的电子邮件:
def send_mail(sender_mail, receiver_mail):
msg = MIMEMultipart()
msg['From'] = sender_mail
msg['To'] = receiver_mail
msg['Subject'] = "Test results"
file = open(RESULTS_FILE, "r")
# body = file.read()
##attachment = MIMEText(file.read())
attachment = MIMEBase('application', "octet-stream")
attachment.set_payload(file.read())
file.close()
attachment.add_header('Content-Disposition', 'attachment',
filename=RESULTS_FILE)
msg.attach(attachment)
body = "Attached the test results"
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP("172.16.100.9", 25)
#server.starttls()
#server.login(sender_mail, password)
text = msg.as_string()
server.sendmail(sender_mail, receiver_mail, text)
server.quit()
但是当我尝试运行它时,似乎该行:text = msg.as_string()
导致错误:
Traceback (most recent call last):
File "\\filer2\incam\test_scripts\run_only_ci.py", line 268, in <module>
testresults.analyze_tests(TESTS_FOLDER, RESULTS_FOLDER, sender_mail, receiver_mail)
File "\\filer2\incam\test_scripts\testresults.py", line 224, in analyze_tests
send_mail(sender_mail, receiver_mail)
File "\\filer2\incam\test_scripts\testresults.py", line 119, in send_mail
text = msg.as_string()
File "C:\Users\fl_ncc\AppData\Local\Programs\Python\Python36\lib\email\message.py", line 158, in as_string
g.flatten(self, unixfrom=unixfrom)
File "C:\Users\fl_ncc\AppData\Local\Programs\Python\Python36\lib\email\generator.py", line 116, in flatten
self._write(msg)
File "C:\Users\fl_ncc\AppData\Local\Programs\Python\Python36\lib\email\generator.py", line 195, in _write
self._write_headers(msg)
File "C:\Users\fl_ncc\AppData\Local\Programs\Python\Python36\lib\email\generator.py", line 222, in _write_headers
self.write(self.policy.fold(h, v))
File "C:\Users\fl_ncc\AppData\Local\Programs\Python\Python36\lib\email\_policybase.py", line 326, in fold
return self._fold(name, value, sanitize=True)
File "C:\Users\fl_ncc\AppData\Local\Programs\Python\Python36\lib\email\_policybase.py", line 369, in _fold
parts.append(h.encode(linesep=self.linesep, maxlinelen=maxlinelen))
AttributeError: 'tuple' object has no attribute 'encode'
为什么会发生?它之前工作,所以我不明白为什么会发生。谢谢您的帮助。