###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