Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python email lib - How to remove attachment from existing message?

Tags:

python

email

mime

I have an email that I'm reading with the Python email lib that I need to modify the attachments of. The email Message class has the "attach" method, but does not have anything like "detach". How can I remove an attachment from a multipart message? If possible, I want to do this without recreating the message from scratch.

Essentially I want to:

  1. Load the email
  2. Remove the mime attachments
  3. Add a new attachment
like image 540
john a macdonald Avatar asked Oct 26 '09 18:10

john a macdonald


People also ask

Can you remove an attachment from an already sent email?

Delete an attachment from a sent or received email messageYou cannot remove attachments from multiple messages at once. Open the email message in the reading pane or double-click the message to open it in a new window. Select the drop-down arrow next to the attachment name and then select Remove attachment.

How do I remove an attachment from an email?

Highlight the file and press the Del . Right-click the attachment and select delete from the right-click menu.

What is EmailMessage () in Python?

It is the base class for the email object model. EmailMessage provides the core functionality for setting and querying header fields, for accessing message bodies, and for creating or modifying structured messages. An email message consists of headers and a payload (which is also referred to as the content).

What is Set_payload in Python?

In Python, set_payload is a method of the class email.message.Message()


1 Answers

The way I've figured out to do it is:

  1. Set the payload to an empty list with set_payload
  2. Create the payload, and attach to the message.
like image 101
john a macdonald Avatar answered Sep 22 '22 06:09

john a macdonald