Accessing email from command line + download attachment

Hello my fellow Linux people,

Everyday I have a report emailed to me as an attached pdf. I want to automate the download of this attachment.

Just for additional information -- I run a BASH script against the pdf (attachment) and the results from this script I need to email to another person. So to fully automate this I'll need an application which can access my email, download a specific attachment, and then email the results of a script I run to another person.

So far I have looked into fetchmail. This seems to allow me access to my email. However, it downloads my entire inbox. It would be nice if the program could be programmed to search for a specific email subject line. Also, I don't think fetchmail allows me to send mail.

Please help me find a Linux application that can access my email and download a specific attachment.

Thanks in advance!

Writing something in Python/Perl ?

Maybe I can do something like this:

  • Cron Job runs calling email utility which downloads my inbox <--- Need a ulitlity recommendation plz
  • Awk Script runs against downloaded emails to match "subject" to regex
  • If subject doesn't match regex then delete email, else (if it matches) then call email utliity which can download attachment and save it into a new directory <--- need email utliity which can download attachments
  • Delete entire local "inbox" directory
  • Run script against pdf
  • Move results to outbox directory
  • Script runs to email results to person
  • ???
  • Profit

Does python have libraries which allow you to access your email???

I'm looking into it as we speak

This may be an approach. Use fetchmail to access emails (need to consider imap vs pop3). Run regex against subject line. If regex matches use a mime comparable application to download the PDF attachment. Run script -> create new file and move it to outbox dir. Use some kind of mxa to send the processed file to email address.

Thought???????

1 Like

That's a good start, I'm sorry I didn't get back to you, Been working all day. I did see something like a web crawler using Perl/python but I cant think of it right now, it's late here but i will add to this tomorrow

does your email server support pop3?

This is a PERFECT use for python. It's a swis-army knife and is very handy at doing these sort of things.

https://docs.python.org/3.5/library/poplib.html
http://www.codemiles.com/python-tutorials/reading-email-in-python-t10271.html

1 Like

I'm considering using fetchmail / procmail for this. I'll update again later.

I was able to get fetchmail to download my inbox. Now I just need to figure out how to use procmail to parse my inbox for a specific subject line.

Not fully tested:

Running on Rasbian

Fetchmail - Procmail - Munpack

Currently able to reach out to Gmail download a pdf from a specific sender. Next step - incorporate it with already working bash script which process the PDF. Final step is to use some other application to email the processed report.

Seems you could filter for subject at the inbound level into a special folder, and then work on just pulling that one folder from the mail server.

For sending through gmail I did manage to find this, not sure if it will help you much.

I used mail to send through gmail. I believe you can specify a file to attach but I use it like the below. I use it to email me my android build results. I don't want the whole log, just the last little bit.

cat /etc/ssmtp/ssmtp.conf
AuthUser=***
AuthPass=***
FromLineOverride=YES
mailhub=smtp.gmail.com:587
UseSTARTTLS=YES

echo -e "Some stuff" | mail -s "A subject" [email protected]
1 Like

That's a good idea! I already filter that subject into its own folder. Save a step right.

In the end I won't be using Gmail, but for learning the technology .

I'll have to check out mail. That'll be the next step; first I have to fetch my mail from my job's server. Testing on Gmail went ok, well see how this goes.

Thanks for tip on mail!

1 Like

Is anyone familiar with a metamail or mpack equivalent for Centos 7????

how is this project going?

Things are still moving forward.

I haven't deployed the pi yet. Last week wasn't a good week to do it. However, hopefully mid-week I'll get the static IP and then set up fetchmail to the company's mail server.

On the homefront, I've used a few new technologies like mutt and base64 to figure a work around on CentOS7 systems.

I'm optimistic about the pi; it's running raspbian and has munpack which should make this real easy once I get the pi talking to the mail server.

Thanks for asking about this @Miguel_Sensacion.

1 Like