Sending Email Using mailx in Linux Through Internal SMTP

Sending Email from mailx Command in Linux Using Gmail’s SMTP introduces how to send email using mailx or mail command in Linux through Gmail’s SMTP which requires some configuration. On the other hand, there are many environments that do not require SSL/TLS/etc. One example is the SMTP server (smtp.ust.hk) for HKUST requires no authentication for sending email from IPs inside the campus network. In this tutorial, I will introduce how to send email using mailx through internal SMTP (using HKUST’s smtp server smtp.ust.hk as the example).

There are at least two methods: using all-in-one command or putting configurations into profile. The all-in-one-command way needs no other configurations except the command line itself, while the way using configuration has a clearer command.

All-in-one command

This is an all-in-one command that sends email to $TO_EMAIL_ADDRESS . You need to also replace these $FROM_EMAIL_ADDRESS and $FRIENDLY_NAME variables with your email and name.

$ mailx -v -s "$EMAIL_SUBJECT"
-S smtp=smtp://smtp.ust.hk
-S from="$FROM_EMAIL_ADDRESS($FRIENDLY_NAME)"
$TO_EMAIL_ADDRESS

mailx will read the email content from STDIN. Type in the email main content and input “Ctrl+d” to tell mailx you have finished the content. The mail will be sent out. You can also use pipes like:

$ echo "The mail content" | mail -v -s ...

Using configuration file

If you use this configuration frequently, you man consider putting the configuration into mailx‘s configuration file ~/.mailrc.

set smtp=smtp://smtp.ust.hk
set from="$FROM_EMAIL_ADDRESS($FRIENDLY_NAME)"

Then you can send email directly by:

$ mailx -v -s "$EMAIL_SUBJECT" $TO_EMAIL_ADDRESS

About Eric Zhiqiang Ma

Eric Zhiqiang Ma is a PhD candidate at Dep. of CSE, HKUST. He is interested in system software for cloud computing, virtualization of large-scale distributed systems and etc. Also find Eric on Facebook, Twitter, LinkedIn and Google+. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties. More Posts - Website

Leave a Reply

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

73 − seventy =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Click to subscribe to Fclose.com Posts by Email if you have not.