By Manny Fernandez

May 12, 2020

Workflow: Auto Saving Mail Attachments from Specific People

I have a colleague that sends out an inventory report daily at 1:00AM.  I want to save this information because it is useful to me.  The question is how would I do this to process it in a way that makes sense.  Obviously, I do not want to save every day for every year.  Here is my workflow for this article.

  1. Monitor inbox for emails meeting particular criteria (from, subject contains, etc)
  2. I want to move the attachment to a particular folder with Automator.
  3. Have Hazel monitor the folder.
    1. Delete .png and .jpg which came in as attachments
    2. Rename the old file in the folder that has been there longer than 1 day
    3. Rename new file with XXFT_ in the name and was created today​ to inventory.pdf
    4. Delete old messages so I do not keep downloading them into the folder.
  4. This will allow my link in Shortcut Bar point to the latest inventory report.

Here is how I did it.

Mail Rules

First thing I needed to was create some Mail rules that will move some messages around and delete old messages.

2020-05-12_11-15-23.png

In this mail rule, I am monitoring all emails that are recv’d FROM a particular email address @fortinet.com with a subject line, that contain a repeated pattern all emails containing inventory have in the subject line.  It then moves them to a Inv folder (mailbox in Apple speak) underneath my main account.

2020-05-12_11-15-40.png

This rule is very similar to the previous one, however if the date received is less than and in my case I chose 1 day since I receive these emails daily.  In this case, any emails that match that criteria, will be deleted automatically.

Automator

Apple’s macOS X comes with an application named Automator .  You may have seen me use this in the past on other articles.  I find it very useful.

2020-05-12_11-31-49.png

Here we see the Automator screen.  I will break down what I did.

  1. Under the leftmost portion of the screen, you will see Library and under that, you will choose Mail
  2. Next you will select the Get Specified Mail items​ which will allow us to tell Automator what folder or “Mailbox” we want it to monitor.
  3. Next we will drag the Get Attachments from Mail Messages to the right.
  4. Now we click Add and add the folder or mailbox that you want it to look at.
  5. Finally, you set the Save attachment in: location.  I created a folder called Inventory Staging.

Hazel

The next section is the monitoring and taking action portion of the files in a folder.  For this, I use Hazel.  It is truly an amazing piece of code that I use for all sorts of things.

2020-05-12_11-40-09.png

In Hazel, I have Inventory Staging folder defined under the Folders section of Hazel.  On the right side, you will see the different rules for that folder.

2020-05-12_11-40-34.png

The first rule, is to clean up any pictures that were received in the email as attachments, such as .png files and .jpg files (usually from the email signatures), and it is going to move those to the trash.

2020-05-12_11-43-25.png

Next, we are going to get any file with a .pdf that was NOT created today (The time it runs), it will rename it to old- and the name of the file.  This is really to save one version of the file.

2020-05-12_11-45-35.png

Lastly, I will get any file that contains XXFT_ which is the way all the files start, that ALSO were created Today, I am going to rename them to INVENTORY.PDF

Schedule Cron Job

Now the finishing touch is to schedule the cron job and add the shortcut to the Shortcut Bar and we are done.

mannyfernandez:~$crontab -l
00 2 * * 1-5 /Users/mannyfernandez/Data/Backups/Scripts/GetAttachments.app
mannyfernandez:~$

To automate this on macOS and Linux, you can use the crontab -e command.  I like to add the text below with # before each line so you can see it in the cron tab.

* * * * *  command to execute
│ │ │ │ │
│ │ │ │ └─── day of week (0 - 6) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0)
│ │ │ └──────── month (1 - 12)
│ │ └───────────── day of month (1 - 31)
│ └────────────────── hour (0 - 23)
└─────────────────────── min (0 - 59)Looking at my example

In my example, I want to run the script every weekday at 2:00AM.  As you can see in the sample text:

  • We are using the first place holder which represents min to say that at 00 minutes
  • The next section represents hours so I am saying at 2 hours.  When combined, it represents 2:00AM
  • We do not care about the day of month, so we keep the next spot at the default *
  • Same with month, so that too will stay at *.
  • Next we want Monday-Friday, which is represented in numeric value, where 0 represents Sunday and 6 represents Saturday
  • And finally the path and script name.

To check the cron jobs that are scheduled, you can use crontab -l

mannyfernandez:~$crontab -l
00 2 * * 1-5 /Users/mannyfernandez/Data/Backups/Scripts/GetAttachments.app
mannyfernandez:~$

 

Shortcut Folder

I use a little utility that stays active in the menu bar of macOS.  It allows me to pin files, folders, apps, scripts etc. I use it for our price list, my bio preso, product matrix, etc.  These are files I use regularly and do not want to search for the files every-time.

2020-05-12_15-17-30.png

The app costs about $5.00

2020-05-12_15-17-38.png

You can click the + sign and then choose Add File/Folder

2020-05-12_15-17-57.png

Then choose the file location

2020-05-12_15-18-14.png

Now I can easily click on the INVENTORY link.

 

Recent posts