使用文本文件搜索字符串,然後通過電子郵件發送nex幾行。搜索文本文件和電子郵件結果
我有搜索工作,並打印正確的線 電子郵件發送成功,但它僅包含輸出
任何思考的最後一行?
FILE.TXT
first
second
thrid
------------------------------------------------------------------------------
after first
after second
after thrid
after forth
after fifth
after sixth
我的代碼
import smtplib
from email.mime.text import MIMEText
from_address = "[email protected]"
to_address = "[email protected]"
with open("file.txt", "r") as f:
searchlines = f.readlines()
for i, line in enumerate(searchlines):
if "------------------------------------------------------------------------------" in line:
for l in searchlines[i:i+6]: print l,
output = l
msg = MIMEText(output)
msg['Subject'] = "Test email"
msg['From'] = from_address
msg['To'] = to_address
body = output
# Send the message via local SMTP server.
s = smtplib.SMTP('smtp.domain.com', '8025')
s.sendmail(from_address, to_address, msg.as_string())
s.quit()
通過打印輸出:
------------------------------------------------------------------------------
after first
after second
after thrid
after forth
after fifth
電子郵件只包含在身體
after fifth
啊我確定我看到了,所以收集結果中的輸出。原始代碼('ascii') AttributeError:'list'對象沒有屬性'encode' – Adam
我錯過了創建MIMEText正文部分的地方 - 更新了答案。 – tripleee
完美,謝謝 – Adam