Send mail from command line with external smtp server on Linux

Send mail via SMTP servers

The default mail command on the Linux terminal, uses the local smtp server (mta) on port 25 to transmit 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.

1. mailx command

The mailx command is available from many different packages like mailutils, heirloom-mailx etc. 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.

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.

Last Updated On : 10th December 2014

Subscribe to get updates delivered to your inbox

2 Comments + Add Comment

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

    [/QUOTE]

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

Leave a comment