How to process email attachments automatically on Linux
02/04/2014
Automating Email Attachment Processing with Linux Bash Scripting
I will share my experience of processing an attachment from my personal email using Linux bash scripting.
To start with, I purchased a daily proxy feed from Hidemy***, which I wanted to integrate into our proxy software, the E-Bouncer project. The downside is that Hidemy*** only sends the list as an attachment to your email address, which in my case, is my personal email. Extracting the list from a zip file and uploading it to the server on a daily basis was tedious.
So, I thought, why not automate the entire process? The plan was to create a filter on my Gmail account to forward any email with an attachment from Hidemy*** to a custom email address accessed by the E-Bouncer server, which is “hproxy@oman0.net.” When the email is received in my inbox and the condition is met, it will be forwarded to the custom email address.
I then wrote a small shell script that processes the “hproxy@oman0.net” email, extracts the attachment, and copies the new proxy list to the E-Bouncer directory. This ensures that users always retrieve the most recent proxy list automatically.
This approach streamlined the process and eliminated the need for manual intervention, making it efficient and reliable. If you encounter a similar situation, this method can help you automate and manage email attachments effectively.
#!/bin/sh# Shell script written by W. Al Maawali # (c) 2014 Founder of Eagle Eye Digital Solutions# https://www.digi77.com# http://www.om77.net# script starts here:# Go to the custom mail directorycd/home/oman0/mail/.hproxy@oman0_net/new# Delete files older than 1 dayls-t | sed-e'1,1d' |xargsrm# Extract attachment files from any available mail on the current directoryuudeview-c-i*# Decompress it as you only receive it as zip fileunzip*.zip# Move to sub directorycdfull_list# Rename the proxy list file to a custom name cp_full_list.txtelite.ppFILE='/home/oman0/mail/.hproxy@oman0_net/new/full_list/elite.pp'# We only proceed if the proxy list is found by checking file existenceif [ -f $FILE ];then# remove the old proxy listrm-f/home/digi77/www/software/xxx/data/proxy-feed/elite.pp# Copy the new proxy listcpelite.pp/home/digi77/www/software/xxx/data/proxy-feed/elite.pp# Clean mail box and wait for next day updatecd/home/oman0/mail/.hproxy@oman0_net/newrm-f-r/home/oman0/mail/.hproxy@oman0_net/new/*rm-f-r/home/oman0/mail/.hproxy@oman0_net/new/*.*rm-f-r/home/oman0/mail/.hproxy@oman0_net/cur/*rm-f-r/home/oman0/mail/.hproxy@oman0_net/cur/*.*# Count number of proxiescat/home/digi77/www/software/xxx/data/proxy-feed/elite.pp|wc-l > /home/digi77/www/software/xxx/data/servercount.pp# Print the datedate+'%Y-%m-%d' > /home/digi77/www/software/xxx/data/proxyvalidatedate.ppelseecho"File $FILE does not exists"fi