monochromatic

monochromatic blog: http://blog.z3bra.org
git clone git://z3bra.org/monochromatic
Log | Files | Refs

plain-old-mails.txt (6378B)


      1 # [Plain old mails](#)
      2 ## — 04 November, 2013
      3 
      4 On my way to meet the default UNIX tools, I ran into a simple one: `mail`, that
      5 was sitting in the corner of my system playing with.. Nothing in fact.  
      6 `mail` is one of that small utilities that have been forgotten and replaced by
      7 more "moderns" tools like mutt, alpine or even thunderbird. But it is worth
      8 knowing about !
      9 
     10 `mail` can manipulate a mail box in either mbox or Maildir format, and is
     11 intelligent enough to know the difference between the two of them.  
     12 It can also handle IMAP mail boxes, but for this post, I'll assume you use a
     13 local mail directory under `$HOME/var/mail/INBOX/`
     14 
     15 Because we all need that bearded touch, we will use `mail` as our **main mail
     16 user agent**.
     17 
     18 ### The environment
     19 
     20 As any of the standard UNIX tool, `mail` integrates well in a UNIX environment,
     21 and is able to interact with external tools to perform specific action (assume
     22 it, you love that huh?).
     23 
     24 Here is the set of variable `mail` is going to use:
     25 
     26 * `MAIL`: The default mail box
     27 * `EDITOR`: The default editor to use
     28 * `VISUAL`: The default visual editor to use
     29 
     30 And that all ! We will not need more to get a running set up (For
     31 more infos, you can check the mail(1) manpage).
     32 
     33 So here we go. Make sure those two variables are exported:
     34 
     35     $ export MAIL=$HOME/var/mail/INBOX
     36     $ export EDITOR=ed
     37     $ export VISUAL=vim
     38 
     39 Now, we will create the most basic directory tree needed by the
     40 setup (We will improve it later)
     41 
     42     $ tree $HOME/var/mail
     43     /home/z3bra/var/mail/
     44     └── INBOX
     45         ├── cur
     46         ├── new
     47         └── tmp
     48 
     49     4 directories, 0 files
     50 
     51 Ok, now the mail environment is set up. You can try the `mail` command at this
     52 point, but an empty mail tree will only result in the following message:
     53 
     54     No mail for z3bra
     55 
     56 For future convenience, copy your /etc/mail.rc to
     57 ~/.mailrc, so we will be able to edit it later.
     58 
     59 
     60 Before continuing with mail, we will take a look at two mail related programs,
     61 [fdm](http://fdm.sourceforge.net/) and [msmtp](http://msmtp.sourceforge.net/),
     62 that we will use to fetch and deliver emails.
     63 
     64 ### Fetching mails
     65 
     66 FDM stands for <q>Fetch and Deliver Mails</q>, so it basically get mails from a
     67 server, and place them in your local filesystem based on regex rules.  
     68 If you want a great tutorial for fdm, check out the
     69 [FDM Quick start guide](http://fdm.sourceforge.net). I'll just give you my
     70 own (simplified) config file:
     71 
     72     action "INBOX"           maildir "%h/var/mail/INBOX"
     73 
     74     account "<account-name>"
     75     pop3s
     76     server "<pop3-server>"
     77     new-only
     78     cache "~/var/mail/.cache"
     79     keep # Keeps mails on the server
     80 
     81     match all action "INBOX"
     82 
     83 `FDM` can get infos from your `~/.netrc` file, which looks like this:
     84 
     85     machine <pop3-server>
     86     login <email@domain.tld>
     87     password <password>
     88 
     89 check that mail fetching works with `fdm -kv fetch`.
     90 If it works, you could place `fdm fetch` in your cron entries.
     91 
     92 ### Sending mails
     93 
     94 `MSMTP` is as simple to use as `fdm`. Check its
     95 [documentation](http://msmtp.sourceforge.net/documentation.html).
     96 Here is a simplified config file:
     97 
     98     defaults
     99     auth            on
    100 
    101     account         <account-name>
    102     user            <email@domain.tld>
    103     from            <email@domain.tld>
    104     host            <stmp-server>
    105     port            25
    106 
    107     account default : <account-name>
    108 
    109 
    110 `msmtp` will also read your `~/.netrc` file to get your password.
    111 
    112 by default, `mail` uses `sendmail` (guess what it does...). Add the following
    113 at the end of  your `~/.mailrc`:
    114 
    115     ~/.mailrc
    116     ...
    117     # use msmtp instead of sendmail
    118     set sendmail="/usr/bin/msmtp"
    119 
    120 ### Writing a new mail
    121 
    122 Back to the topic!  
    123 Now that tools we are going to interact with are set up, let's write
    124 and send out first mail.
    125 We will send this mail to ourselves, so let's go like this:
    126 
    127     $ mail email@domain.tld
    128     Subject: Testing a new MUA
    129     Here is the top of the mail.
    130     You are actually typing like in ed's insert mode.
    131 
    132     To stop typing, just type a dot on its own line
    133     .
    134     EOT
    135 
    136 This will send a mail to the given address. Nothing more. Nothing less.  
    137 You can give multiple address to send the mail to multiple contacts.
    138 
    139 If you need more flexibility (e.g. using your own editor, or input
    140 the text dynamically within a script, keep in mind that you can do
    141 the following:
    142 
    143     $ echo "<E-mail body goes here>" | mail -s "<subject>" <email@domain.tld>
    144     $ vim /tmp/body.txt
    145     $ mail -s "<subject>" <email@domain.tld> < /tmp/body.txt
    146 
    147 
    148 As you might guess, the `-s` can be used to specify the subject. There are also
    149 `-c <CC-field>`, `-b <BCC-field>` for copy/carbon copy, and so on. Just
    150 read the manpage for more options.
    151 
    152 ### Reading your mails
    153 
    154 To read your mail, it's quite simple. Just type `mail` to get an output like:
    155 
    156     $ mail
    157     mail version v14.4.4.  Type ? for help.
    158     "/home/z3bra/var/mail/INBOX": 4 messages 1 unread
    159     O 1 contact@domain.tld Thu Jan  1 01:00  140/5273  Blah blah, subject
    160     A 2 me@mail.domain.tld Thu Jan  1 01:00   95/5869  RE: Previous subject
    161     A 3 NEWS GROUPS        Thu Jan  1 01:00  222/15606 TR: Check this out!
    162     >U 4 willy@mailoo.org   Thu Jan  1 01:00  104/4146 >Testing a new MUA
    163     ? 
    164 
    165 The `?` at the end is a prompt. You can input commands like `print <num>` to
    166 display the content of the mail number "num".  
    167 You can use abbreviations for commands: "p" is the same as "print".  "e" means
    168 "edit", "v" means "visual".
    169 
    170 There are A LOT of commands (to delete mails, encrypt/decrypt, copy to folders,
    171 manage aliases, ...)  
    172 
    173 You can even define macros, to make action like, add sender to aliases, mark as
    174 read, copy to another folder and delete the current mail.
    175 
    176 Today, I discovered `mail` which does anything I need to manage my e-mails. I'll
    177 probably make the switch from mutt on all my machines once I'll be used to it.  
    178 
    179 This little discovery reminded me that UNIX was and still is a great operating
    180 system, regardless of all the tools that have been developped since its birth.
    181 
    182 I hope you (re)learnt something with this article. I don't hear about `mail`
    183 that much nowadays, although it's really usable and functionnal. I feel like a
    184 pokemon hunter. Aware that there are many, many tools out there, of different
    185 forms, with different purpose... I'll probably never use them all. But I'll try!
    186 
    187 <!-- vim: set ft=markdown ts=4 et: -->