Skip to content

Instantly share code, notes, and snippets.

@shamil
Created November 2, 2014 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shamil/aeecf4790f356a47a8df to your computer and use it in GitHub Desktop.
Save shamil/aeecf4790f356a47a8df to your computer and use it in GitHub Desktop.
How to send email using mailx through SMTP

###How to send email using mailx through SMTP

Via command line

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.domain.tld \
    -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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment