Skip to content

Instantly share code, notes, and snippets.

@swvanbuuren
Last active January 10, 2024 19:53
Show Gist options
  • Save swvanbuuren/90ee1dc665d16f22e79c9c4db7e9a5fb to your computer and use it in GitHub Desktop.
Save swvanbuuren/90ee1dc665d16f22e79c9c4db7e9a5fb to your computer and use it in GitHub Desktop.
Fetching emails using fetchmail and relaying them with msmtp

Fetching emails using fetchmail and relaying them with msmtp

This is a short set of instructions how to regularly fetch emails from an IMAP server and relay them to another email address using an SMTP server.

Prerequisites

  • Debian based operating system; on a server or comparable (e.g. a Raspberry Pi) that runs continuously
  • Access to an IMAP server from which the emails are to be fetched
  • Access to an SMTP server to be able to send of the emails to another email address

Setup

Installation

Install fetchmail and msmtp:

sudo apt install fetchmail msmtp

Fetchmail configuration

For fetchmail, create the following configuration in /etc/fetchmailrc

set daemon 60
# set logfile /path/to/existing-log-file.log

poll <imap-server.com> port 993 auth password with protocol IMAP
     user '<username>'
     password '<password>'
     ssl
     keep
     no rewrite
     mda "/usr/bin/msmtp --file /etc/msmtprc -- <relay-email@adress.com>"

A few replacements are necessary:

  • <imap-server.com> should be replaced with the address of your IMAP server
  • <username> and <password> should be replaced with the proper credentials in order to get access to the IMAP server
  • <relay-email@adress.com> should be replaced with the email adress to which you would like to relay the emails
  • There is a slight chance, that your IMAP server uses a different port than 993. In that case, you need to change this accordingly.

This configuration will fetch emails every 60 seconds.

Note that the log file has been commented out. By default, logging is taken care of by systemd service. However, enabling this log file might prove useful for debugging purposes.
To enable custom logging, remove the comment. Also make sure that the log file exists and the user fetchmail has write permissions for this file.

Msmtp configuration

For msmtp, create the following configuration in /etc/msmtprc

defaults
auth           on
tls            on
tls_trust_file /etc/ssl/certs/ca-certificates.crt

account default
host <smtp-server.com>
port 465
tls_starttls off
from <source.email@address.com>
user <username>
password <password>

Again, some replacements are required:

  • <smtp-server.com> should be replaced with the address of your SMTP server
  • <username> and <password> should be replaced with the proper credentials in order to get access to the SMTP server
  • <source.email@address.com> should be replaced with the source email adress
  • There is a slight chance, that your SMTP server uses a different port than 465. In that case, you need to change this accordingly.

Permissions

Since the msmtp configuration file contains credentials, it can only be used by user who owns it. In our case the fetchmail user will try to execute msmtp and therefore this user and its corresponding group need to own the msmtp configuration file. This is accomplished with the following series of commands:

sudo groupadd fetchmail
sudo usermod -a -G fetchmail fetchmail
sudo chown fetchmail:fetchmail /etc/msmtprc
chmod 0600 /etc/msmtprc

Enabling Daemon mode

Finally, the daemon mode of fetchmail needs to be enabled: edit the configuration file of fetchmail /etc/default/fetchmail and change the value of START_DAEMON from no to yes.

Now, restart the systemd service of fetchmail:

sudo systemctl restart fetchmail.service

And automated fetching and relaying should start.

To monitor fetching, you can view the logs with

sudo journalctl -u fetchmail.service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment