This is a work in progress, I will be... Full Story
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.
- Monitor inbox for emails meeting particular criteria (from, subject contains, etc)
- I want to move the attachment to a particular folder with Automator.
- Have Hazel monitor the folder.
- Delete
.png
and.jpg
which came in asattachments
- Rename the old file in the folder that has been there longer than 1 day
- Rename new file with
XXFT_
in the name and was createdtoday
toinventory.pdf
- Delete old messages so I do not keep downloading them into the folder.
- Delete
- 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.
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.
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.
Here we see the Automator
screen. I will break down what I did.
- Under the leftmost portion of the screen, you will see
Library
and under that, you will chooseMail
- Next you will select the
Get Specified Mail items
which will allow us to tellAutomator
what folder or “Mailbox” we want it to monitor. - Next we will drag the
Get Attachments from Mail Messages
to the right. - Now we click
Add
and add the folder or mailbox that you want it to look at. - Finally, you set the
Save attachment in:
location. I created a folder calledInventory 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.
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.
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.
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.
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 at00 minutes
- The next section represents
hours
so I am saying at2 hours
. When combined, it represents2: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
representsSunday
and6
representsSaturday
- 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.
The app costs about $5.00
You can click the +
sign and then choose Add File/Folder
Then choose the file location
Now I can easily click on the INVENTORY
link.
Recent posts
-
-
I have been playing with the free version of... Full Story
-
In my day job, I am on a lot... Full Story