我不是那種經驗豐富的python,而是爲小型工作做一些python編碼。目前我有一項工作可以打開一個日誌文件並提取任何被認爲是錯誤的記錄。然後將這個錯誤列表添加爲電子郵件通知的一部分。我想要做的是包含列表或通知列表爲空。我已經能夠在控制檯中執行此操作,但不知道如何在電子郵件中將其添加爲參數。Python在輸出中包含If/else的內容不僅僅是打印
if errorlist:
print "\n".join(errorlist)
else:
print "No Errors Found"
# Send Email
SMTP_SERVER = {SMTP SERVER}
SMTP_PORT = {SMTP PORT}
sender = {Sender}
password = {Password}
recipient = {Recipient}
subject = "This is the subject line"
errorlist = "<br>" "\n".join(errorlist)
body = "" + errorlist + ""
headers = ["From: " + sender,
"Subject: " + subject,
"To: " + ", " .join(recipient),
"MIME-Version: 1.0",
"Content-Type: text/html"]
headers = "\r\n".join(headers)
session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
session.ehlo()
session.starttls()
session.ehlo
session.login(sender, password)
session.sendmail(sender, recipient, headers + "\r\n\r\n" + body)
session.quit()
你嘗試使用一個變量? –