Deleting attachment from email using Python [duplicate] - python

This question already has answers here:
Python email lib - How to remove attachment from existing message?
(4 answers)
Closed 1 year ago.
I want to be able to delete just the attachment in an email and keep the text content. Can i do this using IMAPClient package?
I found this article which tells me there is some feature like that for mozilla thunderbird which is IMAP client. the link is :
http://www.howtogeek.com/112734/how-to-free-up-space-in-gmail-5-ways-to-reclaim-space/
how do i do this using imaplib or imapclient package in python 2.7?

I think you are getting this wrong. IMAPClient is an IMAP client, so it can handle Imap, thunderbird is also an email (including Imap) client, so It's redudant, you don't need this to use that. Unfortunately IMAPclient doesn't seem to have an attachment handling method (atleast what I could tell from the docs). but a quick google search pointed me to python's native imaplib as a possible candidate to do what you want together with python's email lib. the first to access you gmail acount, and the second to get the message. a possible (also not exactly your use case) scenerio is outlined in this [receipe]ץ(http://code.activestate.com/recipes/302086-strip-attachments-from-an-email-message/)
also a very similiar stackoverflow question.
the trick is that there isn't a detach attachment method in **email* lib. you have to reset the message payload to simulate this effect.

Related

Is it possible to scrape mails within hotmail using Python?

Can this actually be done? Like checking if my browser is logged in and if so, scraw some of my emails? Or does hotmail prevent you from scraping your mails?
Yes actually very easily.
All you have to do is connect to a POP or IMAP(gmail,hotmail) and then manipulate data however you want.import smtplib
Here's a link documentation --- Stackoverflow question
--- POP -email --- Gmail
You can use regular expressions if you want to find something specific
Python RE-Regular Expressions
I don't know how would you check if you're already logged in, there isn't an obvious way to tell.
Of course it could be done but in some weird manner.

Parsing emails with bad mime types in python

I am trying to use python's imaplib and email.feedparser to grab an attachment out of a gmail inbox. The email is generated by an external party and sent to us, so I have no control over it.
The trouble is that the message I am trying to parse has msg.get_content_maintype() return 'text' instead of 'multitype'. As a result the uuencoded attachment gets concatenated with the rest of the message and I don't see an ease way to pull it out of email.message.Message.
Any ideas how I can extract the attachment out of such an email?
If it is any help, the email has 'Produced By Microsoft MimeOLE V6.00.3790.4862' in it. Thunderbird also had trouble rendering this email and wasn't able to figure out that it had an attachment. Otherwise, the message looks ok in Outlook and Gmail web client.
As email.parser.FeedParser documentation says you can use _factory parameter to provide your own Message class. You could put your own class derived from email.message.Message that will replace message contenttype with the correct one if this message was composed in "Produced By Microsoft MimeOLE ...blablabla".
I think it is safe to expect that the messagesin this particular case will always be multipart.

Python lib for publishing email to web

I want to read email and publish it to the web. Is there a good python library available to read email, understand headers, data, attachments etc. and which can easily convert this data to web publishable format?
Try python email module
This tutorial will also be helpful.

How to access GMail (IMAP Email) from my Shell/Python script to download a zip file attached to an email and process it?

I have to process a file everyday. This file is sent to my Email once everyday. If I can get to this email once every day and download the attachment, that had be awesome. Is it even remotely possible to do such a thing?
Thanks!
Please see How can I download all emails with attachments from Gmail? for a practical example.
This is certainly possible. Check out imaplib in Python's standard library; with it doing what you want should be quite straightforward. Also, you can process zip files directly in Python using the zipfile library.
Your best bet is to create an IMAP Folder for your daily emails to be sent to and then create a filter in GMail to send those files there. Your Python script can then check ONLY that folder on some interval and assume that whatever ends up in there is the file you want.
A quick search yielded sooo many results for IMAP fetching examples in Python, I'll leave that part up to you, but I will say that libgmail looks pretty neat.

Best Python Library for Downloading and Extracting Addresses

I've just been given a project which involves the following steps
Grab an email from a POP3 address
Open an attachment from the email
Extract the To: email address
Add this to a global suppression list
I'd like to try and do this in Python even though I could it in PHP in half the time (this is because I dont know anywhere near as much Python as PHP)
My question would be.
Can anyone recommend a Python library for interacting with email in the way described above?
Many thanks in advance
Two bits from the standard library: poplib to grab the email via POP3, email to slice and dice it as you wish.

Categories