Reading gmail email on macOS using fetchmail/postfix/sendmail/alpine
This post describes the process for setting up macOS to read and store email locally –without deleting it from the remote server. The downloaded email can be read with a local application like Alpine.
The process goes like this:
- fetchmail pulls emails from gmail
- fetchmail hands the emails to postfix for local delivery
- use alpine to read our locally delivered email
- use alpine to write an email, which uses postfix to relay it via gmail
- we can use sendmail for any other application that requires us to send emails
Configuring the hostname
- From the Apple menu, open System Settings, and look for
hostname
. It should be under the Sharing settings. - Update the hostname to something that is easy to remember/type/etc. Mine, for example, is set to
macos.local
- Save the changes, open a terminal, and check the new name shows up with the following command:
1hostname
Allowing app access to gmail
Installing Alpine
alpine
can be replaced with other mail clients, like mutt
, or good old mail
.
1brew install alpine
Run alpine
, and send an email to your local account. Typing your own username in the To field should be enough. It should work, but don’t worry if it does not, keep reading.
Configuring postfix
postfix is the application in charge of delivering email locally. When running, it listens for requests on port 25.
It’s expected to have a working copy of postfix
on every mac. We should get some output from the following command to give us a clue whether it’s there or not.
1postconf -d | head -n 1
Type the following commands to read its current configuration, and status:
1postconf -n
2sudo postfix status
We may get something like the following, but it’s ok. Keep reading.
postfix/postfix-script: the Postfix mail system is not running
Start postfix, and check it’s listening on localhost:
1sudo postfix start
2sudo lsof -i :25
We should get output like the following:
master 12345 root ... TCP localhost:smtp (LISTEN)
if not, re-read its config:
1sudo postfix reload
Installing fetchmail
fetchmail is the application we’ll use to download the email. This program automatically uses the local process listening on port 25 –postfix
in our case– to deliver email locally. Both pieces, fetchmail
and postfix
, coordinate to deliver the email to our local file.
Install fetchmail
:
1brew install fetchmail
Create a ~/.fetchmailrc
configuration using the following contents as a guide:
1# Replace "local_user" with your local username
2set logfile "/Users/local_user/.fetchmail/fetchmail.log"
3poll imap.gmail.com proto IMAP
4# Use gmail app password, not regular email password
5user "gmail-user@gmail.com"
6pass "gmail-app-password"
7# Local user to deliver mail to. Replace "local_user" with your local username
8is "local_user"
9# Force secure connection.
10ssl
11# Keep a copy of the mail on the server.
12keep
13# Gmail folder to fetch from.
14folder "INBOX"
15# Fetch limit
16fetchlimit 10
Restrict permissions to others:
1chmod 600 ~/.fetchmailrc
Try it:
1fetchmail -v --fetchlimit 1
Verify with Alpine that the email is there. Alternatively, we can check our mail file in /var/email
, for example:
1# Replace "local_user" with your local username
2less /var/mail/local_user
Visit the fetchmail manual for additional configurations.
Starting postfix in daemon mode
Simply invoking
1fetchmail -d 900
will poll every 15 minutes all the hosts described in our ~/.fetchmailrc
file. As an alternative, we can update the configuration file to set a polling value:
1# Poll every 10 minutes
2set daemon 600
If we do this, fetchmail
will always start in daemon mode unless overriden with the command-line option --daemon 0
or -d0
. Only one daemon process is permitted per user.
Read the daemon mode section in the fetchmail manual for additional options.
Sending email
Warning: Work in progress. There are additional steps needed to prevent postfix from relaying email sent to the local computer. Don’t try it until complete, since it may affect other applications logging process, like
cron
’s.
Edit /etc/postfix/main.cf
:
1relayhost = [smtp.gmail.com]:587
2
3smtp_use_tls = yes
4smtp_sasl_auth_enable = yes
5smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
6smtp_sasl_security_options = noanonymous
7smtp_tls_CAfile = /etc/ssl/cert.pem
Create /etc/postfix/sasl_passwd
:
1[smtp.gmail.com]:587 gmail-user@gmail.com:gmail-app-password
Secure the file and compile it:
1sudo chmod 600 /etc/postfix/sasl_passwd
2sudo postmap /etc/postfix/sasl_passwd
Reload the configuration:
1sudo postfix reload
And try it by sending a message to an external account with alpine
.