blog · about · links

Use Terminal to Send and Receive Emails

Required Software

The above mentioned software is available in almost all Linux distributions.

$ sudo yum install maildrop fetchmail msmtp mutt

Receiving Emails

To receive emails, two software applications are required. The first is fetchmail, which downloads emails from the server using either IMAP or POP3 protocol and then hands them over to maildrop to be processed. By default, Unix-like operating systems store user emails in /var/spool/mail/username. This is also where maildrop saves emails by default. If there are not many emails and there are no specific email filtering requirements, maildrop can be used with default settings without configuration.

As for fetchmail, it can be configured to download unread emails using IMAP protocol from the INBOX and JUNK directories. With "keep" enabled, emails won't be deleted after retrieval, but are marked as read instead.

poll smtp.example.org proto imap
    username "user@example.org"
    password "xxxxxxxx"
    options ssl keep
    mda "/usr/bin/maildrop";

poll smtp.example.org proto imap
    username "user@example.org"
    password "xxxxxxxx"
    options ssl keep
    folder JUNK
    mda "/usr/bin/maildrop";

Then, fetchmail can be run periodically to check for new emails.

Reading Emails

To read emails, you can use mutt, which by default checks "/var/spool/mail/username". To display mails in reverse order based on time and conversation grouping, you can set mutt's mail sorting method by adding the following lines to ~/.muttrc:

set folder=~/mail
set sort_aux=last-date-received
set sort=threads
set sort_re

To mark unwanted mails for deletion, you can press "d". If you want to move multiple mails to another mailbox, you can press "t"to select and tag the emails and then press ";", and finally press "s" to move the mails.

For emails containing HTML content or other attachments, you can use pipe to pass them on to relevant applications for display.

For example, for HTML emails, you can press "v" to enter the attachment menu, select the "text/html" attachment, press "|", and use a custom script to show the email in Firefox:

#!/bin/bash

cat > /tmp/hmail.html
firefox /tmp/hmail.html

As for images, you can let "Eye of Gnome", or other image views to display:

#!/bin/bash

cat > /tmp/img
eog /tmp/img

Or save directly:

cat > [filename]

Sending Mails

In Unix-like systems, the default method for sending emails is through the sendmail command. Msmtp provides a sendmail-compatible approach as well. To begin, configure the ~/.msmtprc file:

account mymail
tls on
auth on
host smtp.exmaple.org
port 587
user user@example.org
from user@example.org
password xxxxxxxx
logfile /home/user/.msmtp.log

account default : mymail

Subsequently, configure the sender's name by appending the following line of code to the "~/.muttrc" file:

set from="Your Fullname Here <user@example.org>"

Now mutt is all set. You can run this command to start sending email:

mutt target@example.org

Then you can start editing the email and finally press "y" to send it.

Git Email Workflow

If you want to contribute patches to an open-source project that uses a mailing list, you'll need to use "git send-email". Git uses "sendmail" by default to send emails, so you can just use it directly, provided that msmtp is configured.

To send the contents of the last two commits to a mailing list, for example, simply run:

git send-email --to target@example.org HEAD~2..HEAD

Address Book

Linux distributions often provide a command-line address book application called "abook". Abook can interact with mutt, but personally, I don't find it useful and prefer to use a plain text file, then use grep to search. Therefore, I won't go into further detail on this.

Why Bothering with Terminal

Finally, let me explain why you would want to use terminal for email.

First, email is not a complex thing and is often just plain text. Using a heavy and slow GUI client for such a simple task can be overkill.

Second, the tools used to send and receive emails in terminal are mostly traditional Unix tools or their successors, which are easy to hack and customize to your needs, such as making your own email filtering rules or scripting to send emails. If you're a heavy user of email, this workflow can be very convenient.


© Licensed under CC BY-NC-SA 4.0 if not specified otherwise.
Email: dzshy [at] outlook [dot] com