Skip to content

Commit

Permalink
Merge pull request prathimacode-hub#1047 from shubhdholakiya/main
Browse files Browse the repository at this point in the history
Attachment Downloader prathimacode-hub#961
  • Loading branch information
prathimacode-hub authored Oct 17, 2021
2 parents 24d4776 + 5e6b110 commit d9cfbaf
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 0 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions AutomationScripts/Attachment Downloader/README.md
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 AutomationScripts/Attachment Downloader/attachment_downloader.py
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))
4 changes: 4 additions & 0 deletions AutomationScripts/Attachment Downloader/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Install following Libraries:
email
pybase64
pycopy-imaplib

0 comments on commit d9cfbaf

Please sign in to comment.