History and use of the mail utility

Magical Mail

There Must Be an Easier Way

Now for a deep breath. The modern heirloom mailx (and other mail clients for that matter) lets you skip that painful process with an unfathomably easy -a attachment parameter, as follows:

# mail -x "Not just anybody" -a 1.jpg chris@binnie.tld

It really is that simple. With what you've learned, however, if you're ever caught out by MIME types or base64 encoding headaches on older operating systems, then you'll be suitably armed to circumvent the issues in the future.

How does the modern mailx manage this? It automatically alters its content type by looking into the file for MIME types or by file name extension (e.g., .jpg). You might expect a JPG image to have a MIME, such as jpeg image/jpeg or similar. If the mail utility can't fathom what type of file the content belongs to, then it will use text/plain or application/octet-stream as a fallback.

Tangential Tinkering

Before getting distracted with another facet, I might mention some of the other basics for sending email from the command line using the (modern) mail utility. You'll be glad to know that they're very simple to enable.

Formatted as comma-separated lists, you can use the -b parameter for blind carbon copying (BCCing) and -c for carbon copying (CCing). You can throw in -d for debugging, which conveniently doesn't send the email message but steps through all the motions as if it were going to. With debugging enabled (having pressed the "dot" key and Enter to trigger EOT), I see this:

user = chrisbinnie, homedir = /home/chrisbinnie
.
Sendmail arguments: "send-mail" "-i" "chris@binnie.tld" "chris@linux.tld"

The linux.tld address is my CC address, and the primary To: address is the binnie.tld address.

According to the man pages, the debugging option "is intended for mailx development only." Fear not, however; Listing 2 shows how powerful the verbose or -v option is. I think it's absolutely fantastic.

Listing 2

Verbose Mode

[<-] 220 mx.google.com ESMTP t2fjm4427fd213ply.2523 - gsmtp
[->] EHLO smtp.box.tld
[<-] 250 SMTPUTF8
[->] STARTTLS
[<-] 220 2.0.0 Ready to start TLS
[->] EHLO smtp.box.tld
[<-] 250 SMTPUTF8
[->] AUTH LOGIN
[<-] 334 VXNlcm5hbWU6
[->] Y2hyaXNiaW5uaWUzQGdtYWlsLmNvbQ==
[<-] 334 UGFzc3dvcmQ6
[<-] 235 2.7.0 Accepted
[->] MAIL FROM:<chris@binnie.tld>
[<-] 250 2.1.0 OK t2fjm4427fd213ply.2523 - gsmtp
[->] RCPT TO:<chris@binnie.tld>
[<-] 250 2.1.5 OK t2fjm4427fd213ply.2523 - gsmtp
[->] RCPT TO:<chris@linux.tld>
[<-] 250 2.1.5 OK t2fjm4427fd213ply.2523 - gsmtp
[->] DATA
[<-] 354  Go ahead t2fjm4427fd213ply.2523 - gsmtp
[->] Received: by smtp.box.tld (sSMTP sendmail emulation); \
     Sat, 12 Mar 2018 17:47:29 +0000
[->] From: "Chris Binnie" <chris@binnie.tld>
[->] Date: Sat, 15 Nov 2014 17:47:29 +0000
[->] To: chris@binnie.tld
[->] Cc: chris@linux.tld
[->] Subject: Picture yourself, on a boat, on a river
[->] User-Agent: Heirloom mailx 12.5 6/20/10
[->] MIME-Version: 1.0
[->] Content-Type: multipart/mixed;
 boundary="=_546791b1.cwvb6n4KoGN8/hpZ+3JYeQN7+d1wNr6+lHmtxFOfJlKkkY3o"
[snip ...]

As you can see, an entire email transaction is taking place before being released into the wild on the Internet. I have snipped the Content-Type: image/jpeg section with the MIME information and the gobbledygook that is presented in place of the binary JPG file.

The information in Listing 2 allows you to trace each stage through the transaction; note that I'm using authentication to connect to Gmail, which is accepted successfully. I'm also using a Sendmail clone to package up my SMTP requests rather than installing a full-fledged SMTP server. I'm utilizing the services of the super-lightweight and highly versatile sSMTP software to speak to Gmail directly via the mail utility.

Finally, there's an address CC'd, and I'm emailing the same address (displayed as the To: line) that I'm sending the email from. The process tails off with the aforementioned MIME information and shows the mail client version with:

[->] User-Agent: Heirloom mailx 12.5 6/20/10

A handy -E switch, also known as the skipemptybody option, is ideal for scripting. Here, the mail utility will quietly ignore email with empty bodies, as is its prerogative. You might want to use this for one-line entries in the crontab, where you want email with content but also just want to ignore them if there's nothing to read after various system events.

For such a scenario – wherein you use cron to report dutifully to you if there's something to report – you might want certain preconfigured variables set up in advance. You can do this with a config file called /etc/nail.rc.

Dare I repeat the statement about the utter chaos caused by so many hats being thrown into the ring when it came to developing the mail utility? Even the excellent heirloom mailx still uses that file name, referencing nail, for its config. At least the headers of the nail.rc file provide a kindly reminder of which application this config belongs to.

Within that file, you'll find a mixture of interactive shell commands and a sprinkling of options for command-line email. Spend a moment rummaging inside if you want to learn more. Not listed on my Ubuntu version are the following options:

set smtp=smtp.box.tld
set from=sysadmin@linux.tld

You might want to add these lines to adjust where the mail comes from and which SMTP relay to use. For integration with Gmail (and be warned that mileage will almost certainly vary because of version subtleties), your config file might look as follows:

set smtp=smtps://smtp.gmail.com:465
set from="Chris Binnie"
set smtp-auth-user=chris_binnie_linux
set smtp-auth-password=my_password
set ssl-verify=ignore

Rather than using the config file, you can also pass these parameters sensibly to the command line, as I've been doing. I've kept it simple and left out a lot of the parameters, but I'm sure you get the idea.

Config files exist for a reason: to avoid having to rewrite the Magna Carta as a massive, single command line:

# mail -s "Made the bus in seconds flat" chris@binnie.tld \
  -S ssl-verify=ignore -S smtp-auth-user=chris_binnie_linux < bodytxt

Before modern versions of mail clients existed, consistently porting hand-crafted scripts from the command line between systems (and versions of the mail utility) was problematic. Remember, for example, that the -s for changing the email subject line was not supported universally, and I'm sure that you'll agree that's a relatively fundamental component of sending email. More difficulty resulted from the fact that altering the From: line, or who the email was sent from, wasn't always easily possible.

A scenario might have been, for example, that you had Bash installed on a Solaris box and also on a Linux box and knew your scripts were solid. However, because of the packages bundled with the operating systems, you always had to be mindful of altering your scripts or, more than likely, overwriting versions of the mail utility by compiling them from source to get them to send your system reports reliably.

Another Subtitle

I remember having all sorts of issues with a Qmail MTA (Mail Transfer Agent) trying to get it to accept a change in the From: line. I can do this on my current setup (remember that I'm using sSMTP as a conduit to speak to Gmail's SMTP servers) with the following settings. On the command line, I can use:

# echo "Four thousand holes in Blackburn, Lancashire" | \
  mail -r chris@linux.tld -v -s "Dragged a comb across my head" chris@binnie.tld

This replaces the From: line with chris@linux.tld. Be warned however, if you're delivering it to some email providers, they will ignore the From: line and replace the account the email has been relayed through to protect against spam.

If you're interested, you can install sSMTP as follows:

# apt-get install ssmtp

The configuration that is working for me is shown in Listing 3 and lives in the config file /etc/ssmtp/ssmtp.conf. I've abbreviated the less salient entries and other comments for the sake of brevity.

Listing 3

sSMTP Configuration

# The place where the mail goes. The actual machine name is required, no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=smtp.gmail.com:587
# The full hostname
hostname=linux.chrisbinnie.tld
# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=YES
AuthUser=chris_binnie_linux@gmail.com
AuthPass=my_plaintext_password
UseTLS=YES
UseSTARTTLS=YES
AuthMethod=LOGIN

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy ADMIN Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus
Subscribe to our ADMIN Newsletters
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs



Support Our Work

ADMIN content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.

Learn More”>
	</a>

<hr>		    
			</div>
		    		</div>

		<div class=