Работа с текстовым файлом для поиска строки, а затем отправить по электронной почте несколько строк.Поиск текстовых файлов и результатов электронной почты
У меня есть поиск работу и выводит правильные строки электронной посылаемые успешно, однако он содержит только последнюю строку вывода
Любые мысли?
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
ah ok я вижу, поэтому собираю вывод в результате. orig.encode ('ascii') AttributeError: объект 'list' не имеет атрибута 'encode' – Adam
Я пропустил место, где вы создали часть тела MIMEText - обновил ответ. – tripleee
отлично, спасибо – Adam