NOTE: This is mac only.
- Go to Finder.
- Press CMD+SHIFT+G.
- Type
~/Library/Application Scripts/com.apple.mail. - Open
saveByRule.scptand changetheFolderto where you'd like emails to be saved. - Copy and Paste
saveByRule.scptinto~/Library/Application Scripts/com.apple.mail. - Go to Mail>Preferences>Rules>Add Rule.
- Enter some filter for your rule and choose Run Applescript as action to perform. Use saveByRule as option.
- Click Ok.
For converting to PDF and HTML.
- In the terminal, run
sudo pip install mail-parserandbrew install Caskroom/cask/wkhtmltopdf. - Open
toPDF.shand alter location to match the directory your saved emails are located. (Keep *.eml) - In the terminal, run
bash toPDF.sh.
For Automating conversion to HTML/PDF.
- Create new>Folder Action in Automator.
- Set folder to watch to folder where emails are saved.
- Add
run shell scriptaction. - Copy and paste contents of toPDF.sh into textarea of run shell script.
- Save.
NOTE: saveByRule.scpt is almost entirely copied from StackOverflow.
Is the use of the message subject potentially dangerous, @dustinknopoff ?
What if an Email subject line were to contain some text such as “/../../../../../../usr/local/bin/“, where an Email subject line causes the Email to be written to some place you don’t want / some place dangerous?
Perhaps after setting the
msgSubjectvalue, that value should have potently dangerous characters (such as / or * or ? or ; or ` or \ or $ or & or < or > or “ or ‘ or : or # or ( or ) or [ or ] or { or } or | etc.) removed from that variable or perhaps potentially dangerous characters could be changed into something harmless?Perhaps remove anything from the
msgSubjectvalue that it not a lower case letter or UPPER CASE LETTER or digit to space, or perhaps convert anything in themsgSubjectvalue that it not a lower case letter or UPPER CASE LETTER or digit to space into the letter x?UPDATE 0
Alternatively, setting
newFilecould be done without the use of the message subject string, as in:If you do this, you might want to modify
msgDateto include the hour and minute and to include a counter when formingnewFIleas suggested in GH-gistcomment-5311296 below.UPDATE 1a
The use of the Email subject in forming the filename could become especially dangerous when used in conjunction with the
toPDF.shshell script. Consider an Email with a subject that contains some text such as:It might depend on what
mailparserandwkhtmltopdfdo with their arguments, and which version of the shell you are using as to if such an Email subject line could be used to cause problems. Nevertheless, why not try to avoid such problems by stripping out dangerous characters or converting them into something harmless?UPDATE 2
You might want to add some additional protections to
toPDF.shsuch as changing:Into