Mail Filer connects to an IMAP e-mail server and downloads any attachments. Which mailboxes are read and which attachments are downloaded are configurable.
Using mailfiler
To get started with Miln Mail Filer, download and expand the executable file onto your computer.
Mail Filer is a single executable file. It does not need to be installed, can be run from any directory, and can be removed by deleting the file.
Let’s walk through an example use of mailfiler
and then consider other options. Consider this command line call:
./mailfiler -server mail.example.com
The flags for this call to mailfiler
are:
-server mail.example.com
- The IMAP4 server to connect to.
What Happens
So what happens when this command is issued?
mailfiler
does the following:
- Looks for credentials to the server, in the default netrc file
~/.netrc
- Connects to the server;
- Reads a list of all the available mailboxes;
- Reads the e-mail from each mailboxes:
- If an e-mail has an attached file, the file is written to the current working directory.
Mail Filer will overwrite any existing file with the same name as the attachment.
netrc
Mail Filer uses a netrc
file to read credentials. This a text file, with should have limited access rights, stored on your computer. For the example call of mailfiler
above, the netrc file should contain an entry for the machine mail.example.com
:
machine mail.example.com
login main@example.com
password thisisthemainpassphrasefortheaccount
The location of the netrc file can be specified with the -netrc
flag. The credentials used from the file can be specified using the -netrc-machine
flag.
Filters
Mail Filer can filter mailboxes (-mailbox
) and attachments by their name (-name
). Only those that match the filter are read and acted upon.
The matching is implemented with Go’s path.Match function:
pattern:
{ term }
term:
'*' matches any sequence of non-/ characters
'?' matches any single non-/ character
'[' [ '^' ] { character-range } ']'
character class (must be non-empty)
c matches character c (c != '*', '?', '\\', '[')
'\\' c matches character c
character-range:
c matches character c (c != '\\', '-', ']')
'\\' c matches character c
lo '-' hi matches character c for lo <= c <= hi
Using filters, you can control which attachments are downloaded from which mailboxes.
Intended Use
Mail Filer is designed to be simple and robust. I designed the tool to be part of a larger workflow and needed consistency and reliability.
As part of business process, I need a specific file type of attachment to be downloaded. This task was previously performed manually and occasionally e-mails were missed.
To run Mail Filer regularly and automatically, I added an entry to DiskStation Manager’s Task Scheduler:
- Control Panel > Task Scheduler > User-defined script
I could have also used a tool like cron
, launchd
, or systemd
.
Examples
Below are examples of common uses of Mail Filer.
Download Attachments from the Inbox
Download attachments from e-mails in the inbox and no other mailboxes.
./mailfiler -server mail.example.com -mailbox INBOX
Download Compressed Archives to a Specific Directory
Save attachments with the file extension .zip
to the directory ~/zipped/
.
./mailfiler -server mail.example.com -name '*.zip' -destination ~/zipped/
Use Alternative Credentials
Use alternative credentials, rather than the default one associated with the server.
./mailfiler -server mail.example.com -netrc-machine mail-alt
Alternative netrc Example
For this example, the ~/.netrc
file must contain an entry for the machine mail-alt
:
machine mail-alt
login user@example.com
password thisisthealternativepassphrasefortheaccount
Flags
mailfiler
accepts the following command line flags:
Usage of ./mailfiler:
-config string
File path to configuration.
-destination string
Path to directory to write attachments.
-h Show this help message and exit. (shorthand)
-help
Show this help message and exit.
-l string
Directory path to licence certificate files (PEM encoded) (shorthand) (default "~/.miln/")
-legal
Show legal notices and exit.
-licence string
Directory path to licence certificate files (PEM encoded) (default "~/.miln/")
-name string
Attachment file name filter. (default "*.storereceipt")
-netrc string
Path to netrc file for CalDAV authentication. (default "~/netrc")
-port int
TCP/IP port of the IMAP server (default 993)
-quiet
Quieten non-critical output.
-server string
Host of the IMAP server (default "mail.your-server.de")
-show-licence
Show licence details and exit.
-skip-existing
Do not overwrite existing files with attachments. (default true)
-v Show version details and exit. (shorthand)
-version
Show version details and exit.