Send mail from command line with external smtp server on Linux

By | July 20, 2020

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 

$ readlink -f /usr/bin/mailx 

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="" -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="" \
> -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 here

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

About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].


Send mail from command line with external smtp server on Linux
  1. Boris

    Telnet method :

    telnet your.smtp.server 25
    HELO your.ip
    RCPT TO:
    Subject: YOUR SUBJECT
    Your message then end with a point
    (then disconnect)

  2. Colinalvin63

    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.

    Many thanks,


  3. jabbson

    –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]
    –body [body-specification]
    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.


Leave a Reply

Your email address will not be published. Required fields are marked *