Bootstrap FreeKB - mailx - Send and retrieve email using the mailx command
mailx - Send and retrieve email using the mailx command

Updated:   |  mailx articles

The which command can be used to determine if the mailx command is installed.

~]$ which mailx
/usr/bin/which: no mailx in (/home/john.doe/.local/bin:/home/john.doe/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin)

 

If the which command is not installed, apt-get (Debian, Mint, Ubuntu) or dnf or yum (CentOS, Fedora, Red Hat) can be used to install mailx.

yum install mailx

 

Under the hood, the mail and mailx commands are simply wrapper commands that invoke the sendmail command, so if you are debugging some issue, you may want to use the sendmail command.

 


The mailx command without any options can be used to list the emails in your current users inbox.

~]# mailx
Heirloom mail version 12.5.7/5/10. Type ? for help.
"/home/john.doe/Maildir/": 2 new messages 2 unread
>U jane.doe              Mon Jan 01 09:00 17/565  "Hello John"
 U jack.doe              Mon Jan 01 10:00 17/532  "Hello dad"
&

 

The -u option can be used to list the emails in some other users inbox.

mailx -u jane.doe

 

In this example, mailx is used to send an email to john.doe@example.com. This assumes there is a user account for john.doe on the example.com email server. After typing "Hello John", press Ctrl + D to send the email.

~]# mailx john.doe@example.com
Subject: Example subject
Hello John
EOT

 

echo can be used to add a message body or content and the -s option can be used to include the subject.

echo "example message" | mailx -s "example subject" john.doe@example.com

 

The -r option can be used to set the from (sender) email.

mailx -r jane.doe@example.com john.doe@example.com

 

The -c option can be used to carbon copy.

mailx -c jane.doe@example.com john.doe@example.com

 

The -b option can be used to blind carbon copy.

mailx -b jane.doe@example.com john.doe@example.com

 


The -S option can be used to set additional settings.

mailx -S from="no-reply@example.com" john.doe@example.com

 

However, since the additional settings are almost always the same, and its common to have a lot of additional settings, it is much more common to set the additional settings in the /etc/mail.rc file. In this example, the name of the additional settings is "default" but this can be some other string.

account default {
  set smtp=smtp://mail.example.com:587
  set from="no-reply@example.com"

  #Authentication
  set smtp-auth-user=john.doe
  set smtp-auth-password=itsasecret

  #Encryption (ssl/tls)
  set smtp-use-starttls
  set ssl-verify=ignore
  set nss-config-dir=/etc/pki/nssdb
}

 

Then the -A option can be used to use the "default" additional settings.

mailx -A default john.doe@example.com

 

This article has more details on each additional setting -> http://heirloom.sourceforge.net/mailx/mailx.1.html.

 


The -v option can be used for verbose output.

~]# mailx -v -A default user1@example.com
Subject: Example subject
Hello John
Resolving host mail.example.com . . . done.
Connecting to 192.168.0.10:587 . . . connected.
220 mail.example.com ESMTP Postfix
>>> EHLO client.example.com
250-mail.example.com
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-8BITTIME
250 DSN
>>> STARTTLS
220 2.0.0 Ready to start TLS
Comparing common name: "mail.example.com"
host certificate does not match "mail.example.com"
SSL parameters: cipher=missing, keysize=256, secretkeysize=256,
issuer=E=admin@example.com,CN=mail.example.com,OU=example,O=example,L=City,ST=State,C=US
subject=E=admin@example.com,CN=mail.example.com,OU=example,O=example,L=City,ST=State,C=US
>>>EHLO client.example.com
250-mail.example.com
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ESTN
250-AUTH PLAIN LOGIN
250-AUTH=PLAIN LOGIN
250-ENHANCEDSTATUSCODES
250-8BITTIME
250 DSN
235 2.7.0 Authentication successful
>>> MAIL FROM:<user1@example.com>
250 2.1.5 Ok
>>> DATA
354 End data with <CR><LF>.<CR><LF>
>>> .
250 2.0.0 Ok: queued as A96586012F
>>> QUIT
221 2.0.0 Bye

 


/etc/syslog.conf or /etc/rsyslog.conf can be used to confirm email events are appended to the /var/log/maillog file.

 ~]$ grep mail /etc/rsyslog.conf
# Log anything (except mail) of level info or higher.
*.info;mail.none;authpriv.none;cron.none                /var/log/messages
# Log all the mail messages in one place.
mail.*                                                  -/var/log/maillog

 

/var/log/maillog or journalctl should contain events for each email that was sent using mailx.

~]# tail /var/log/maillog
Mar 27 21:23:16 mail1 postfix/local[2079]: 94D795845: to=<user1@localhost>, orig_to=<user1@localhost>, relay=local, delay=0.05, delays=0.03/0.01/0/0, dsn=2.0.0, status=sent (delivered to Maildir)

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter f1486c in the box below so that we can be sure you are a human.