download attachment using shell script

How to Download Attachment from Gmail Using Shell Script

Gmail is a widely used email service used by individuals and organizations. Typically, we download attachment from Gmail via web browser. But sometimes you may need to download attachment from Gmail via terminal. For example, you may need to download a CSV file from Gmail and run some commands or scripts on it automatically. You can easily do this using scripting languages such as Python, Perl or even shell script. In this article, we will learn how to download attachment from Gmail using shell script in Linux. It is useful for email attachment processing.

How to Download Attachment from Gmail Using Shell Script

We will basically be downloading email attachments hosted on a secure POP3 web server such as Gmail and save them to a local folder using fetchmail, procmail and munpack utility tools. Fetchmail is used to fetch email from remote servers, procmail utility is used to convert them into maildir format, as required by munpack and munpack is used to unpack the attachments.

1. Install Required Packages

Open terminal and run the following commands to install required packages.

$ sudo apt-get install fetchmail procmail mpack 

2. Configure Fetchmail

Run the following command to create configuration file for fetchmail.

$ vi ~/.fetchmailrc

Add the following lines to provide information about your Gmail account. Replace the parts in bold with your Gmail address and password.

protocol pop3
timeout 300
port 995
username "<>" password "<gmail password here>"
sslproto TLS1
mda "/usr/bin/procmail -m '/home/ubuntu/.procmailrc'"

Save and close the file. Please note, the last line above specifies the folder location where procmail will store downloaded emails. We will need this in the next step.

Since the above file contains Gmail username and password, we will make it secure by allowing only root user to be able to access it, using the following command.

$ chmod 700 ~/.fetchmailrc

3. Configure Procmail

Next, create configuration file for procmail with the following command.

$ vi ~/.procmailrc

Add the following lines to it, to link it with fetchmail.



The above configuration will set /home/ubuntu as the folder where emails are to be downloaded & saved. It needs to be the same directory mentioned in the last configuration line of fetchmail file in previous step. Only then fetchmail and procmail will be able to work together properly.

Next, run the following commands to create a bunch of directories required to download and save emails and attachments.

mkdir ~/Maildir/process
mkdir ~/Maildir/process/landing
mkdir ~/Maildir/process/extract
mkdir ~/Maildir/process/store
mkdir ~/Maildir/process/archive

Here is what each of the directories are required for:

  • landing is where we first move new mails from procmail’s Maildir before extracting the attachments
  • extract is where attachment extraction is performed
  • store is the final destination of attachments
  • archive is where the mail files are stored after the process is done. If you want to reprocess certain files, just move it back to landing

4. Create Shell Script to Download Gmail Attachment

Next, create an empty shell script to download Gmail attachment.

$ vi ~/scripts/

Add the following lines to it.

date +%r-%-d/%-m/%-y >> $LOG
mv $DIR/new/* $DIR/process/landing/
cd $DIR/process/landing/
shopt -s nullglob
for i in *
echo "processing $i" >> $LOG
mkdir $DIR/process/extract/$i
cp $i $DIR/process/extract/$i/
echo "saving backup $i to archive"  >> $LOG
mv $i $DIR/process/archive
echo "unpacking $i" >> $LOG
munpack -C $DIR/process/extract/$i -q $DIR/process/extract/$i/$i

shopt -u nullglob
echo "finishing.." >> $LOG
mv $DIR/process/extract/* /$DIR/process/store/ 
echo "done!" >> $LOG

Save and close the file. The above script basically calls fetchmail to download emails, and moves their contents to various sub directories in /home/ubuntu/Maildir. From there, we use munpack to unpack the various attachments and even save backup to archive (optional).

Each set of attachments is kept on separate folder, with date & time of processing. Make the file executable with the following command.

$ sudo chmod +x ~/scripts/

You can run the script with the following command.

$ ~/scripts/

We will schedule a cronjob to automatically run the above script on a periodic basis. For this, open crontab with the following command.

$ crontab -e

Add the following line to it to run the script every day at 10.a.m.

0 10 * * * /home/ubuntu/scripts/ >/dev/null 2>&1

Save and close the file to install the new cronjob.

In this article, we have learnt how to download Gmail attachments using shell script in Linux. You can customize it as per your requirement.

Also read:

How to Do Google Search From Terminal
How to Backup WordPress to Dropbox
How to Download Attachment Using Python
How to Read File Line by Line into Python List
How to Backup WordPress to GitHub

Leave a Reply

Your email address will not be published.