December 8, 2014

Send mail from command line with external smtp server on Linux

SMTP servers
If 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.
If you do not want to setup a mail client like Thunderbird on your local machine and want to run a quick command to send out mails via these smtp servers, then this post shows you how.
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 "
This e-mail address is being protected from spambots. You need JavaScript enabled to view it
" -s "This is the subject" -S smtp="mail.example.com:587" -S smtp-use-starttls -S smtp-auth=login -S smtp-auth-user="
This e-mail address is being protected from spambots. You need JavaScript enabled to view it
" -S smtp-auth-password="abc123" -S ssl-verify=ignore
This e-mail address is being protected from spambots. You need JavaScript enabled to view it

Here is a step by step version of the same command -
$ echo "This is the message body and contains the message" | mailx -v \
> -r "
This e-mail address is being protected from spambots. You need JavaScript enabled to view it
" \
> -s "This is the subject" \
> -S smtp="mail.example.com:587" \
> -S smtp-use-starttls \
> -S smtp-auth=login \
> -S smtp-auth-user="
This e-mail address is being protected from spambots. You need JavaScript enabled to view it
" \
> -S...

Read full post here
Send mail from command line with external smtp server on Linux

Read more at Binary Tides
Click Here!