NOTE: This is mac only.
- Go to Finder.
- Press CMD+SHIFT+G.
- Type
~/Library/Application Scripts/com.apple.mail
. - Open
saveByRule.scpt
and changetheFolder
to where you'd like emails to be saved. - Copy and Paste
saveByRule.scpt
into~/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-parser
andbrew install Caskroom/cask/wkhtmltopdf
. - Open
toPDF.sh
and 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 script
action. - 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
msgSubject
value, 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
msgSubject
value that it not a lower case letter or UPPER CASE LETTER or digit to space, or perhaps convert anything in themsgSubject
value that it not a lower case letter or UPPER CASE LETTER or digit to space into the letter x?UPDATE 0
Alternatively, setting
newFile
could be done without the use of the message subject string, as in:If you do this, you might want to modify
msgDate
to include the hour and minute and to include a counter when formingnewFIle
as 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.sh
shell script. Consider an Email with a subject that contains some text such as:It might depend on what
mailparser
andwkhtmltopdf
do 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.sh
such as changing:Into