Collecting and sorting email attachments using Fetchmail + Procmail + uudeview
Background
At work, I received a task from a colleague about the need to resolve the issue of saving attachments to letters (arriving at a specific email address) and saving them on a file server. The subject of the letter contains an abbreviation that must be specified in the name of the directory where attachments will be saved.
I found several articles on the Internet using these tools, but for the most part they did not allow me to do what was intended and I had to add a configuration file that allowed us to save files on a network share.
What and why
Fetchmail – we need it to collect letters from the mail server; setting up this utility as a whole does not have any complicated points. More details here.
Procmail is an email delivery agent that allows you to process a letter. More details at the link.
We also need a utility uudeview – This utility works with binary files and is needed to process attachments.
Installation and configuration of utilities
I chose server ubuntu 22.04, we install:
sudo apt update
sudo apt upgrade
apt install fetchmail procmail uudeview cifs-utils
and create a directory where everything will live
mkdir mail
Create a fetchmail.log file in the created mail directory
touch mail/fetchmail.log
grant access to it:
chmod 700 mail/fetchmail.log
chown user:user mail/fetchmail.log
connect a network folder (in my case the file server runs on Windows Server, so the connection is via the samba protocol via cifs)
Open the file /etc/fstab
mcedit /etc/fstab
And add a line (first the path to the file server, and then the mount point)
//*путь к серверу* /mnt cifs defaults,uid=1000,gid=1000,rw,credentials=/home/user/.smbclient 0 0
Create a file with credentials
mount -a
open the file
mcedit .smbclient
and add lines with data (indicate your account information):
username=user
password=password
domain=example.com
We mount
mount -a
Next, configure fetchmail:
mcedit ~/.fetchmailrc
In case of IMAP connection
poll mail.example.com
port 993
proto IMAP
user "user"
password "password"
ssl
mda "/usr/bin/procmail -d %T"
set syslog set logfile "mail/fetchmail.log"
where mail.example.com is the mail server to which you will receive mail
The rest, I think, is clear and understandable)
in case of POP3 connection
poll mail.example.com
port 995
proto POP3
user "user"
password "password"
ssl
mda "/usr/bin/procmail -d %T"
set syslog set logfile "mail/fetchmail.log"
We register access to the file:
chmod 700 .fetchmailrc
chown user:user .fetchmailrc
Now let's move on to setup Procmail
mcedit ~/.procmailrc
and add the following:
SHELL=/usr/bin/sh
subject =`cat | egrep "^Subject:" | awk '{ print $2 }'`
:0
* ^Subject:[ ].+[a-z0-9]
* ^Content-Type:
| mkdir /mnt/${subject} ;\
uudeview -i +a +o -p /mnt/$subject -
We register access to the file:
chmod 700 .procmailrc
chown user:user .procmailrc
I explain:
this config file checks letters on the mail server and creates a folder with the name of the subject and saves attachments from the letter into it
Launch
Now, after all the settings, we send a test email with an attachment and run the command:
fetchmail -kv
If necessary, add command execution to the task scheduler
open the scheduler:
crontab -e
and add an entry that will execute the command every minute:
*/1 * * * * /usr/bin/fetchmail -kv
We check for the presence of a new directory on the network share and attachments in them.
The final
In my case, there was a problem with the encoding; topics written in Cyrillic are saved in a crooked form, so we use the Latin alphabet; so far, a solution to this problem has not been found.
If you are not saving files on a network share, but everything is fine on the local disk, check access to the network folder.