I check the attachments in the letter through the anti-virus system, if the attachment is malicious or suspicious, it must be removed from the letter and add the text that the attachment was deleted. But I ran into some difficulties, if the letter contains an inline attachment (for example, a picture embedded in the letter) then adding information to the end does not lead to anything, this information is simply not displayed by email clients when viewing the letter. I use the following letter (the contents of the image have been deleted):
To: nmikaev <nmikaev@tip.avsw.ru> From: nmikaev <nmikaev@tip.avsw.ru> Subject: test18 Message-ID: <5bf8356f-2cad-bb52-1644-344b25a6a3fe@tip.avsw.ru> Date: Wed, 13 Dec 2017 16:39:47 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.5.0 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="------------9D100498B76DCCA2302346E9" Content-Language: en-US This is a multi-part message in MIME format. --------------9D100498B76DCCA2302346E9 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit test18 test18 --------------9D100498B76DCCA2302346E9 Content-Type: multipart/related; boundary="------------F4FDF24DEF3F140B04A4F455" --------------F4FDF24DEF3F140B04A4F455 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 7bit <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> </head> <body text="#000000" bgcolor="#FFFFFF"> <p>test18</p> <p><img src="cid:part1.FFAD31E9.10EFE63D@tip.avsw.ru" alt=""></p> <p>test18</p> </body> </html> --------------F4FDF24DEF3F140B04A4F455 Content-Type: image/jpeg; name="oplpbkaaobmmkala.jpeg" Content-Transfer-Encoding: base64 Content-ID: <part1.FFAD31E9.10EFE63D@tip.avsw.ru> Content-Disposition: inline; filename="oplpbkaaobmmkala.jpeg" HERE IMAGE CONTENT --------------F4FDF24DEF3F140B04A4F455-- --------------9D100498B76DCCA2302346E9 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Removed oplpbkaaobmmkala.jpeg with hash: 4b002716ede36b1f8da0ec3543cdb6996b50d943d1ba789a8fd39e3d467174e3 --------------9D100498B76DCCA2302346E9-- At the end of the information is added already. I use the following code:
text = '' if self.attachments_pool: for attachment in self.attachments_pool: text+='Removed ' + attachment.getFilename() +' with hash: ' + attachment.getHash() + '\n' text = MIMEText(text, 'plain') self.msg.attach(text) However, if the letter does not contain inline attachments, then the information is correctly displayed. Perhaps the problem is that test18 is written twice in text / plain and in html. And between them there should be an image, it may be necessary to select the main object, which email.Message to add new text to. How to do it ? And I understand that the attach method is not quite suitable for these purposes?
And more, how can Content-Disposition: inline or Content-Disposition: attachment be effectively removed? Is it necessary to collect a new letter (new email.Message object) and add attachments to it already? You can probably somehow change the already existing object email.Message? And if it is impossible, how can you construct a new letter that can be of any complexity (you need to copy all the headers, all parts of the letter, for example, using msg.walk)?
Thank you very much in advance ! I hope to get an answer to my question.