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