-
-
Save baali/2633554 to your computer and use it in GitHub Desktop.
# Something in lines of http://stackoverflow.com/questions/348630/how-can-i-download-all-emails-with-attachments-from-gmail | |
# Make sure you have IMAP enabled in your gmail settings. | |
# Right now it won't download same file name twice even if their contents are different. | |
import email | |
import getpass, imaplib | |
import os | |
import sys | |
detach_dir = '.' | |
if 'attachments' not in os.listdir(detach_dir): | |
os.mkdir('attachments') | |
userName = raw_input('Enter your GMail username:') | |
passwd = getpass.getpass('Enter your password: ') | |
try: | |
imapSession = imaplib.IMAP4_SSL('imap.gmail.com') | |
typ, accountDetails = imapSession.login(userName, passwd) | |
if typ != 'OK': | |
print 'Not able to sign in!' | |
raise | |
imapSession.select('[Gmail]/All Mail') | |
typ, data = imapSession.search(None, 'ALL') | |
if typ != 'OK': | |
print 'Error searching Inbox.' | |
raise | |
# Iterating over all emails | |
for msgId in data[0].split(): | |
typ, messageParts = imapSession.fetch(msgId, '(RFC822)') | |
if typ != 'OK': | |
print 'Error fetching mail.' | |
raise | |
emailBody = messageParts[0][1] | |
mail = email.message_from_string(emailBody) | |
for part in mail.walk(): | |
if part.get_content_maintype() == 'multipart': | |
# print part.as_string() | |
continue | |
if part.get('Content-Disposition') is None: | |
# print part.as_string() | |
continue | |
fileName = part.get_filename() | |
if bool(fileName): | |
filePath = os.path.join(detach_dir, 'attachments', fileName) | |
if not os.path.isfile(filePath) : | |
print fileName | |
fp = open(filePath, 'wb') | |
fp.write(part.get_payload(decode=True)) | |
fp.close() | |
imapSession.close() | |
imapSession.logout() | |
except : | |
print 'Not able to download all attachments.' |
This is awesome! Thank you
Hi,
Does anyone find this repo working? I think I am stuck with the login that throws me exception
typ, accountDetails = imapSession.login(userName, passed)
can anyone help?
thank you
Hi @chaohuichen,
Have you tried this? https://gist.github.com/kngeno/5337e543eb72174a6ac95e028b3b6456
Also from @x011on flagging particular emails
https://gist.github.com/x011/d6c346debe22f5fec9e4499643ae1050
Hi,
Does anyone find this repo working? I think I am stuck with the login that throws me exceptiontyp, accountDetails = imapSession.login(userName, passed)
can anyone help?
thank you
Hi @chaohuichen,
Have you tried this? https://gist.github.com/kngeno/5337e543eb72174a6ac95e028b3b6456
Also from @x011on flagging particular emails
https://gist.github.com/x011/d6c346debe22f5fec9e4499643ae1050Hi,
Does anyone find this repo working? I think I am stuck with the login that throws me exception
typ, accountDetails = imapSession.login(userName, passed)
can anyone help?
thank you
Thank you @kngeno. the second gist link works!
Trying this in 2022 and always getting the 'Not able to download all attachments.' error. I read that in May 2022 google does not give the options to allow access to less secure apps, which fixed the problem for other users here. Does anyone know how to make this work in 2022?
Any help is much appreciated. Thanks!
Trying this in 2022 and always getting the 'Not able to download all attachments.' error. I read that in May 2022 google does not give the options to allow access to less secure apps, which fixed the problem for other users here. Does anyone know how to make this work in 2022?
Any help is much appreciated. Thanks!
If mail is something like test@test.com
Less secured app option is still available
For mails like anything@gmail.com you should use "app password"
Thanks for the reply! For App password, would I just use it in place of the regular password in the script above?
I tried that but am still getting the same issue.
Dears
usful code to check old pictures, thanks, however
When I ran the code it printed the email username and did not do anything more.