mailx
is an evolution of the original mail
command line utility for sending and receiving mail. It a very useful tool to test mail setup.
How the mail command works
The mail
command invokes the “standard” sendmail
command which in turns connects to the local MTA (mail transfert agent). In most modern distribution the sendmail
binary isn’t used anymore, and it’s just a symbolic link to the MTA.
The local MTA (exim, postfix, etc..) run an SMTP server that accept connection on the port TCP 25.
Install the mail command
On Debian three packages provide the mailx
command. I suggest you to install the heirloom-mailx
package, because this version has more features and options.
Sending mails
You can use mailx
interactively. You can hit enter for new lines, and when done typing the message press Ctrl+D and mailx would display an EOT:
$ mail -s "This is the subject" someone@foobar.com
I'm batman!
EOT
1. Body content from a file
The message body of the email can be taken from a file:
$ mail -s "This is Subject" someone@foobar.com < /path/to/file
The message can also be piped:
$ echo "This is message body" | mail -s "This is Subject" someone@foobar.com
2. Multiple recipients
To send the mail to multiple recipients, specify all the emails separated by a comma:
$ echo "This is message body" | mail -s "This is Subject" someone@foobar.com,someone2@foobar.com
3. CC and BCC
The -c
and -b
options can be used to add CC and BCC addresses respectively:
$ echo "This is message body" | mail -s "This is Subject" -c user@foobar.com someone@foobar.com
4. Specify From name and address
To specify a “FROM” name and address, use the -r
option:
$ echo "This is message body" | mail -s "This is Subject" -r "ThePope<thepope@vactican.com>" someone@foobar.com
5. Specify “Reply-To” address
The reply to address is set with the internal option variable “replyto” using the -S
option:
$ echo "This is message" | mail -s "Testing replyto" -S replyto="batman@foobar.com" someone@foobar.com
6. Attachments
Attachments can be added with the -a
option:
$ echo "This is message body" | mail -s "This is Subject" -r "ThePope<thepope@vactican.com>" -a /path/to/file someone@foobar.com