Использую библиотеку email. Необходимо удалять некоторые вложения из письма и добавлять информацию о том что удалил. На вход поступает подобного вида письмо:
From test-sender@example.com
From: test-sender@example.com
To: test-recipient@example.com
Subject: test simple message
It's a test.
-- Best regards.
Следующий код:
text = ''
for attachment in self.attachments_pool:
text+='Removed ' + attachment.getFilename() +' with hash: ' + attachment.getHash() + '\n'
text = MIMEText(text, 'plain')
self.msg.attach(text)
Получаю ошибку:
Traceback (most recent call last):
File "/usr/lib/python3.5/email/message.py", line 209, in attach
self._payload.append(payload)
AttributeError: 'str' object has no attribute 'append'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "msg_parser.py", line 386, in <module>
raise e
File "msg_parser.py", line 376, in <module>
main()
File "msg_parser.py", line 369, in main
msg_handler.handle()
File "msg_parser.py", line 329, in handle
self.msg.attach(text)
File "/usr/lib/python3.5/email/message.py", line 211, in attach
raise TypeError("Attach is not valid on a message with a"
TypeError: Attach is not valid on a message with a non-multipart payload
Я понимаю что письмо которое пытаюсь преобразовать не совсем валидное, но нужно из него сгенерировать multipart по всей видимости чтобы добавить туда что то. Как это сделать? Заранее спасибо