Send mail via SMTP servers
The default mail command on the Linux terminal, uses the local smtp server (mta) on port 25 to send emails. However at times you need to specify an external smtp server to use for sending mails.
For example you have just setup an smtp server, like Postfix or Exim, then you would want to test it out to check if it is receiving and relaying emails properly or not.
Being able to send mails from command line using this external smtp server is quick rather than having to setup a mail client like Thunderbird on your local machine.
There are 2 command line utilites called mailx and swaks that can be used to send mails using external smtp server. These are quite useful when you need to send emails from a bash script in an automated manner.
1. mailx command
The mailx command is available from many different packages like mailutils, heirloom-mailx etc. First you need to use the aptitude command to search the mailx package available for your system. Here is an example
$ aptitude search mailx p bsd-mailx - simple mail user agent p bsd-mailx:i386 - simple mail user agent v mailx - v mailx:i386
To find out which mailx command your system is using, run the readlink command. Here is a sample output.
$ readlink -f /usr/bin/mailx /usr/bin/bsd-mailx $ readlink -f /usr/bin/mailx /usr/bin/mail.mailutils
Not all mailx variants can use external smtp servers to send mail. Only the one that comes from the s-nail package (pulled by heirloom-mailx) can do it.
We shall be using heirloom-mailx since it allows to specify smtp connection details in a single command and issue and email quickly.
$ sudo apt-get install heirloom-mailx
Now send an email with an external smtp server like this -
echo "This is the message body and contains the message" | mailx -v -r "[email protected]" -s "This is the subject" -S smtp="mail.example.com:587" -S smtp-use-starttls -S smtp-auth=login -S smtp-auth-user="[email protected]" -S smtp-auth-password="abc123" -S ssl-verify=ignore [email protected]mail.com
Here is a step by step version of the same command -
$ echo "This is the message body and contains the message" | mailx -v \ > -r "[email protected]" \ > -s "This is the subject" \ > -S smtp="mail.example.com:587" \ > -S smtp-use-starttls \ > -S smtp-auth=login \ > -S smtp-auth-user="[email protected]" \ > -S smtp-auth-password="abc123" \ > -S ssl-verify=ignore \ > [email protected]
Make sure to use the correct settings, like port number, authentication mechanism etc. The command would produce verbose output giving full details of the smtp communication that goes on behind, making it very easy to test and debug.
Note: The package heirloom-mailx was removed from Ubuntu version 18 onwards. Now you have to install the s-nail package.
$ sudo apt-get install s-nail
The s-nail command is the same as heirloom mailx command. Just the name is different. Run the same command above by replacing mailx with s-nail and it should work.
If you are using the latest version of s-nail (14.9.x) the syntax for specifying the smtp server details might be slightly different. Check the latest manual herehttp://manpages.ubuntu.com/manpages/bionic/en/man1/s-nail.1.html
Check out the previous post on mailx command here -
9 mail/mailx command examples to send emails from command line on Linux
2. Swaks command
Swaks (Swiss army knife for SMTP) is a simple command line tool that can be used to test smtp servers to check if they are doing they job properly. It supports TLS as well.
Install swaks on Ubuntu/Debian with the following command
$ sudo apt-get install swaks
Now send the email
$ echo "This is the message body" | swaks --to [email protected] --from "[email protected]" --server mail.example.com --auth LOGIN --auth-user "[email protected]" --auth-password "abc123" -tls
All the options are pretty self explanatory. The "--server" option specifies the external SMTP server to use, "--auth" specifies the type of authentication. The "-tls" option tells swaks to use STARTTLS.
Check the man page for more options.