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]
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.
Telnet method :
telnet your.smtp.server 25
Subject: YOUR SUBJECT
Your message then end with a point
I use Thunderbird as a mail client and have been experimenting with sending mail through Thunderbird via the bash command line. I want to be able to send an email within a bash script.
I can successfully make Thunderbird open a compose window to send a mail, but I really want it to happen in the background with NO human interaction. Is there an option I can give Thunderbird on the command line to send the message straight away ?
This is what I’ve got so far :-
thunderbird -compose “preselectid=id3,[email protected],subject=’Dropbox Sync Log `date +%D`’,body=’`cat ~/Dropbox/sync_log.txt`’,format=2,attachment=’~/Dropbox/sync_log.txt'”
I fully realise I don’t need to attach a file as well as catting it into the message. I’ve just left it in to show that attachments are possible from the command line, that’s all.
If someone knows how to make Thunderbird actually send the email from the command line instead of opening a window in Thunderbird client, I’d be delighted to hear from you.
Thanks a lot. very useful.
–body is required for swaks in order to read message body from standard input; otherwise the default body of “This is a test mailing” will be used.
[QUOTE from man]
Specify the body of the email. The default is “This is a test
mailing”. If no argument to –body is given, prompt to supply one
interactively. If ‘-‘ is supplied, the body will be read from
standard input. If any other text is provided and the text
represents an open-able file, the content of that file is used as
the body. If it does not represent an open-able file, the text
itself is used as the body.
If the message is forced to MIME format (see –attach) the argument
to this option will be included unencoded as the first MIME part.
Its content-type will always be text/plain.
Hi, great examples to connect to a smtp server from command line. I also like this: https://github.com/gasparfm/gscripts/blob/master/gemail.sh It uses sendmail (you can have nullmailer configured to connect to a smtp server). The best of it is that you can send attachments.
I use it to send by emails files generated directly on a server.