Email Commands

useful email commands

Posted by tfran on 2016-07-08 08:54:13

 

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 "someone@example.com" -s "This is the subject" -S smtp="mail.example.com:587" -S smtp-use-starttls -S smtp-auth=login -S smtp-auth-user="someone@example.com" -S smtp-auth-password="abc123" -S ssl-verify=ignore yourfriend@gmail.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 "someone@example.com"

> -s "This is the subject"

> -S smtp="mail.example.com:587"

> -S smtp-use-starttls

> -S smtp-auth=login

> -S smtp-auth-user="someone@example.com"

> -S smtp-auth-password="abc123"

> -S ssl-verify=ignore

> yourfriend@gmail.com

 

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

 

binarytides.com post middle horizontal

Time : 0.00026512145996094

Now send the email

 

$ echo "This is the message body" | swaks --to someone@gmail.com --from "you@example.com" --server mail.example.com --auth LOGIN --auth-user "you@example.com" --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.