I've set up inside of my CPanel to have all emails sent to x@x.com to be piped into a python script of mine. How would I go about having any attachments saved into a specific directory on the server and perhaps see the subject/message of the email itself?
-
1The docs on the formatting of a forwarded email are not exactly, well, existing. Does it give you a mutlipart message, some sort of stringified representation thereof, some path to the file on the server, or something else? Unless @Thomas' answer works for you, it's hard to tell you how to process this data correctly when we can't find out how the data is formatted. – Silas Ray Jan 17 '13 at 23:14
You can use the email package to process MIME-formatted email messages. Use email.parser.FeedParser to parse the message and get back an email.message.Message object:
- Treat it like a dictionary to get header fields like
Subject. - Use
is_multipart()to check whether it is multipart and therefore might have attachments (or it might just be a plain-text + HTML message). - Use the
walk()method to recursively walk over all multipart submessages. Submessages with aContent-Dispositionheader starting withattachmentare attachments, and you can get their contents usingget_payload().
-
I found harrybailey.com/2009/02/send-or-pipe-an-email-to-a-php-script which has a comment that seems to suggest that messages come through as strings, so this should work, I think. – Silas Ray Jan 17 '13 at 23:35