Plain old mails
04 November, 2013
On my way to meet the default UNIX tools, I ran into a simple one:
It can also handle IMAP mail boxes, but for this post, I'll assume you use a local mail directory under
Because we all need that bearded touch, we will use
As any of the standard UNIX tool,
Here is the set of variable
EDITOR: The default editor to use
VISUAL: The default visual editor to use
And that all ! We will not need more to get a running set up (For more infos, you can check the mail(1) manpage).
So here we go. Make sure those two variables are exported:
$ export MAIL=$HOME/var/mail/INBOX $ export EDITOR=ed $ export VISUAL=vim
Now, we will create the most basic directory tree needed by the setup (We will improve it later)
$ tree $HOME/var/mail /home/z3bra/var/mail/ └── INBOX ├── cur ├── new └── tmp 4 directories, 0 files
Ok, now the mail environment is set up. You can try the
No mail for z3bra
For future convenience, copy your /etc/mail.rc to ~/.mailrc, so we will be able to edit it later.
Before continuing with mail, we will take a look at two mail related programs, fdm and msmtp, that we will use to fetch and deliver emails.
FDM stands for
Fetch and Deliver Mails, so it basically get mails from a
server, and place them in your local filesystem based on regex rules.
If you want a great tutorial for fdm, check out the FDM Quick start guide. I'll just give you my own (simplified) config file:
action "INBOX" maildir "%h/var/mail/INBOX" account "<account-name>" pop3s server "<pop3-server>" new-only cache "~/var/mail/.cache" keep # Keeps mails on the server match all action "INBOX"
FDM can get infos from your
~/.netrc file, which looks like this:
machine <pop3-server> login <firstname.lastname@example.org> password <password>
check that mail fetching works with
fdm -kv fetch.
If it works, you could place
fdm fetch in your cron entries.
MSMTP is as simple to use as
fdm. Check its
Here is a simplified config file:
defaults auth on account <account-name> user <email@example.com> from <firstname.lastname@example.org> host <stmp-server> port 25 account default : <account-name>
msmtp will also read your
~/.netrc file to get your password.
sendmail (guess what it does...). Add the following
at the end of your
~/.mailrc ... # use msmtp instead of sendmail set sendmail="/usr/bin/msmtp"
Writing a new mail
Back to the topic!
Now that tools we are going to interact with are set up, let's write and send out first mail. We will send this mail to ourselves, so let's go like this:
$ mail email@example.com Subject: Testing a new MUA Here is the top of the mail. You are actually typing like in ed's insert mode. To stop typing, just type a dot on its own line . EOT
This will send a mail to the given address. Nothing more. Nothing less.
You can give multiple address to send the mail to multiple contacts.
If you need more flexibility (e.g. using your own editor, or input the text dynamically within a script, keep in mind that you can do the following:
$ echo "<E-mail body goes here>" | mail -s "<subject>" <firstname.lastname@example.org> $ vim /tmp/body.txt $ mail -s "<subject>" <email@example.com> < /tmp/body.txt
As you might guess, the
-s can be used to specify the subject. There are also
-b <BCC-field> for copy/carbon copy, and so on. Just
read the manpage for more options.
Reading your mails
To read your mail, it's quite simple. Just type
$ mail mail version v14.4.4. Type ? for help. "/home/z3bra/var/mail/INBOX": 4 messages 1 unread O 1 firstname.lastname@example.org Thu Jan 1 01:00 140/5273 Blah blah, subject A 2 email@example.com Thu Jan 1 01:00 95/5869 RE: Previous subject A 3 NEWS GROUPS Thu Jan 1 01:00 222/15606 TR: Check this out! >U 4 firstname.lastname@example.org Thu Jan 1 01:00 104/4146 >Testing a new MUA ?
? at the end is a prompt. You can input commands like
print <num> to
display the content of the mail number "num".
You can use abbreviations for commands: "p" is the same as "print". "e" means "edit", "v" means "visual".
There are A LOT of commands (to delete mails, encrypt/decrypt, copy to folders, manage aliases, ...)
You can even define macros, to make action like, add sender to aliases, mark as read, copy to another folder and delete the current mail.
Today, I discovered
This little discovery reminded me that UNIX was and still is a great operating system, regardless of all the tools that have been developped since its birth.
I hope you (re)learnt something with this article. I don't hear about