Bootstrap FreeKB - Linux Commands - Send and retrieve email using the mail command
Linux Commands - Send and retrieve email using the mail command

Updated:   |  Linux Commands articles

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

~]$ which mail
/usr/bin/which: no mail 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.

dnf 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 mail command without any options can be used to list the emails in your current users inbox.

~]# mail
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.

mail -u jane.doe

 


In this example, mail 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.

~]# mail 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" | mail -s "example subject" john.doe@example.com

 

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

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

 

The -c option can be used to carbon copy.

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

 

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

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

 

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

mail -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.

mail -A default john.doe@example.com

 

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

 


Verbose

The -v (verbose) option can be used to diplay more information.

[root@server1 ~]# mail -v john.doe@example.com
250-mail.example.com
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
Hello user1
EOT

 


Mail log

View the mailog to ensure the email was delivered.

[root@server1 ~]# 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 efe584 in the box below so that we can be sure you are a human.