Can’t send e-mail from the Ubuntu command line – mailx: invalid option — ‘A’

Quick post everyone! I’ve been trying to set up e-mail notifications from my server using Gmail, but haven’t been successful due to an odd error caused by ‘mailx’.

I’ve configured a .mailrc file in my home directory like below:

cd ~
nano .mailrc

#Add the following lines to the new file you've just created.

account gmail {
set smtp-use-starttls
set smtp=smtp://smtp.gmail.com:587
set smtp-auth=login
set smtp-auth-user=%REPLACE WITH YOUR E-MAIL ACCOUNT%
set smtp-auth-password=%REPLACE WITH YOUR PASSWORD%
set from=%NAME YOUR E-MAIL SOURCE. NO SPACES%
}

Save and close the file once you’ve updated it. I should point out that if you wanted to have different e-mail addresses for different applications, you would just have to copy the block of text shown above, change “gmail” to some other account name, and modify the user/password. Once saved, let’s go ahead and try to send a test e-mail.

echo "Test Email" | mailx -v -A gmail -s "test" whereshouldIsendthisemail@domain.com

The email account configuration is called by the “-A gmail” command shown above. If you receive this error:

mailx: invalid option -- 'A'
usage: mailx [-dEIinv] [-a header] [-b bcc-addr] [-c cc-addr] [-s subject]...
       mailx [-dEIiNnv] -f [name]
       mailx [-dEIiNnv] [-u user]

Then you might not have the correct version of mailx installed. In order to check, run the following command.

dpkg -l | grep mail

This should produce an output similar to the one below. The issue is that you have “bsd-mailx” installed instead of “heirloom-mailx”, which does not support the “–A” (account) trigger.

mail1

Fixing the issue is as simple as:

sudo apt-get update
sudo apt-get remove bsd-mailx
sudo apt-get install heirloom-mailx

Once that’s complete, try your test command again:

echo "Test Email" | mailx -v -A gmail -s "test" whereshouldIsendthisemail@domain.com

This time, you should see a long output from the mailx function. Check your inbox and look for your test e-mail!

mail2

Comments

comments