+++ images = ['images/blog/encrypt-compress-emails-on-server.jpg'] tagsspot = ['Security','Server','Privacy', 'Dovecot'] categoriesspot= ['Solutions'] date = '2022-07-08' lastmod = '2022-07-08' title = 'Encrypt and compress emails server side' slug = 'encrypt-compress-email-server' +++ To improve the security and privacy of users and save disk space, the mail server [Dovecot](https://www.dovecot.org/) allows encrypting the files containing the e-mail messages. Compression is done through the ```zlib``` plugin while encryption is done through the ```mail_crypt``` plugin. ```bash mail_plugins = $mail_plugins zlib mail_crypt ``` The plugins can be configured with several options ```bash plugin { mail_crypt_global_private_key = "/tmp/$(basename "$file")" if [[ -s "/tmp/$(basename "$file")" ]]; then chmod 600 "/tmp/$(basename "$file")" chown vmail:vmail "/tmp/$(basename "$file")" mv "/tmp/$(basename "$file")" "$file" else rm "/tmp/$(basename "$file")" fi fi done ``` To decrypt and decompress : ```bash find /var/vmail/domain/user/Maildir -type f -regextype egrep -regex '.*S=.*W=.*' | while read -r file; do if [[ $(head -c7 "$file") == "CRYPTED" ]]; then echo $file doveadm fs get compress lz4:0:crypt:private_key_path=/etc/dovecot/crypt/master.key:public_key_path=/etc/dovecot/crypt/master.pub:posix:prefix=/ \ "$file" > "/tmp/$(basename "$file")" if [[ -s "/tmp/$(basename "$file")" ]]; then chmod 600 "/tmp/$(basename "$file")" chown vmail:vmail "/tmp/$(basename "$file")" mv "/tmp/$(basename "$file")" "$file" else rm "/tmp/$(basename "$file")" fi fi done ```