forked from prathimacode-hub/Awesome_Python_Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request prathimacode-hub#1047 from shubhdholakiya/main
Attachment Downloader prathimacode-hub#961
- Loading branch information
Showing
6 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+308 KB
AutomationScripts/Attachment Downloader/Images/2_before run_mail received.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+264 KB
...ionScripts/Attachment Downloader/Images/3_Program downloaded the attachment.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Attachment Downloader | ||
|
||
|
||
## Aim | ||
|
||
The aim of this project is to create a program that can download the attachments from the fresh received mail. | ||
|
||
## Short description of package/script | ||
|
||
- Basically this program downloads all the attachments that attached with new received mails. | ||
|
||
* Note: The sender's email address must set to allow less secure apps, if you are using Gmail. | ||
|
||
## Workflow of the Project | ||
|
||
- First this program will run and take input of the email address and it's password. | ||
- Then it will download all the attachments at the file location you want. | ||
|
||
|
||
## Setup instructions | ||
|
||
- You just need to install some of the libraries of python and you can run this program. | ||
- All the libraries are mentioned in requirements.txt | ||
|
||
|
||
# Screenshot of Output | ||
<p align="center"><img src="https://github.com/shubhdholakiya/Awesome_Python_Scripts/blob/853f353473bc5625b4170ccc05b724d11f81dc28/AutomationScripts/Attachment%20Downloader/Images/1_Sending%20Mail.png">"</p> | ||
<br> | ||
|
||
<p align="center"><img src="https://github.com/shubhdholakiya/Awesome_Python_Scripts/blob/853f353473bc5625b4170ccc05b724d11f81dc28/AutomationScripts/Attachment%20Downloader/Images/2_before%20run_mail%20received.png">"</p> | ||
<br> | ||
|
||
<p align="center"><img src="https://github.com/shubhdholakiya/Awesome_Python_Scripts/blob/853f353473bc5625b4170ccc05b724d11f81dc28/AutomationScripts/Attachment%20Downloader/Images/3_Program%20downloaded%20the%20attachment.png">"</p> | ||
<br> | ||
|
||
## Author(s) | ||
|
||
Shubh Dholakiya |
53 changes: 53 additions & 0 deletions
53
AutomationScripts/Attachment Downloader/attachment_downloader.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# TBD | ||
# Python 3.10 | ||
|
||
|
||
import imaplib | ||
import base64 | ||
import os | ||
import email | ||
|
||
user_email_add = input('Email: ') | ||
pass_email_add = input('Password: ') | ||
|
||
mail = imaplib.IMAP4_SSL('imap.gmail.com', 993) | ||
|
||
mail.login(user_email_add, pass_email_add) | ||
|
||
mail.select() | ||
|
||
type, data = mail.search(None, 'ALL') | ||
mail_ids = data[0] | ||
id_list = mail_ids.split() | ||
|
||
for num in data[0].split(): | ||
typ, data = mail.fetch(num, '(RFC822)' ) | ||
raw_email = data[0][1] | ||
# converts byte literal to string removing b'' | ||
raw_email_string = raw_email.decode('utf-8') | ||
email_message = email.message_from_string(raw_email_string) | ||
# downloading attachments | ||
for part in email_message.walk(): | ||
# this part comes from the snipped I don't understand yet... | ||
if part.get_content_maintype() == 'multipart': | ||
continue | ||
if part.get('Content-Disposition') is None: | ||
continue | ||
fileName = part.get_filename() | ||
if bool(fileName): | ||
filePath = os.path.join('C:/Users/shubh/Auto Download/', fileName) | ||
if not os.path.isfile(filePath) : | ||
fp = open(filePath, 'wb') | ||
fp.write(part.get_payload(decode=True)) | ||
fp.close() | ||
subject = str(email_message).split("Subject: ", 1)[1].split("\nTo:", 1)[0] | ||
print('Downloaded "file" from email titled "subject" with UID uid.'.format(file=fileName, subject=subject, uid=mail_ids.decode('utf-8'))) | ||
|
||
for response_part in data: | ||
if isinstance(response_part, tuple): | ||
msg = email.message_from_string(response_part[1].decode('utf-8')) | ||
email_subject = msg['subject'] | ||
email_from = msg['from'] | ||
print ('From : ' + email_from + '\n') | ||
print ('Subject : ' + email_subject + '\n') | ||
print(msg.get_payload(decode=True)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Install following Libraries: | ||
pybase64 | ||
pycopy-imaplib |