How to handle attachments passed via Postfix

Chris Angelico rosuav at gmail.com
Mon Oct 12 04:58:37 EDT 2015


On Mon, Oct 12, 2015 at 6:17 PM, Anthony Papillion
<anthony at cajuntechie.org> wrote:
> I'm writing a script that will have email with attachments passed to it via Postfix.  Postfix is properly passing the email to the script but I'm not quite sure how to get at the attachment.  What I need to do is save the attachment out to the filesystem.
>
> Can anyone point me in the right direction?

If I understand the situation correctly, you're getting a
MIME-formatted email, and want to decode it into a usable attachment -
right?

Python includes a package for teasing apart RFC[2]822 format emails:

https://docs.python.org/3/library/email.html

Check out the email.parser module, or the convenience function
email.message_from_string - you should be able to get at the different
parts (including attachments) from there.

ChrisA



More information about the Python-list mailing list