If you spend a considerable amount of time in your terminal, you might find the ability to fire off short emails from within it–without context-switching to your mail client or browser or whichever means you use–a convenient shortcut. Not to mention the utility it affords any number of use cases you might later choose to implement; a simple shell script, for example, can deliver notifications via email. It's a convenient feature of Unixen that requires very little setup.
First, edit /etc/mail/secrets
, as a privileged user, to store your
credentials in the following format:
relayid relayuser:secretpassword
Where relayuser
is the username used to login to your relay, which, in many
cases, is your full email address (e.g., darth@vader.com). And relayid
is
just a label to identify the username:password pair that will be referenced by
smtpd.conf
whenever sending mail.
Given the sensitive contents, secure the file mode and ownership:
# chown root:_smtpd secrets # chmod 640 secrets
Next, open /etc/mail/smtpd.conf
, again with privileges, and ensure the
following is either uncommented or present—substitute with your relayid
label
from secrets
:
table aliases file:/etc/mail/aliases table secrets file:/etc/mail/secrets listen on lo0 action "local" mbox alias <aliases> action "relay" relay host smtp+tls://relayid@smtp.mailserver.com:587 auth <secrets> match for local action "local" match for any action "relay"
Now, simply restart smtpd
and test your console driven mail app:
# rcctl restart smtpd smtpd(ok)
Give the mail(1) man page a quick read to get the complete rundown,
but the fundamentals of sending an email are quite simple: hit the -s
switch;
specify the subject and receiver as arguments; enter your message; and
terminate with a lone period ('.') on a line when finished like so:
$ mail -s "Scanning all frequencies" zorb@broz.net Is there anybody out there? . EOT
That's it. Now, rather than interrupt your flow when working in the terminal,
you can fire off emails without even leaving vi
!