How To Send Mails To an External User With Mailx on Linux

Question: How to send emails to external users with mailx running on CentOS/RHEL system?

The following command shows how to send a mail to an external user using mailx:

# echo "this is the body of the email" | mailx -vvv -s "test mail" -r "From" -S smtp="your-smtp" someone@address

Here,
-vvv = Verbosity.
-s = Specifies the subject.
-r = Email sent from.
-S = Specifies the smtp server.

To be able to send mails out of the internal network using mailx, the external relay or smtp that is going to be handling all mails, needs to be specified in order for emails to be delivered to the correct address.

You can also edit the mailx config file /etc/mail.rc to add the following configuration to the end of the file, e.g.:

set smtp=your.smtp.server
set from="from email address"

Then run the mailx command and then enter the body of the email, hit enter, and finally press Ctrl+D to deliver it.

# mailx -vvv -s "email subject"  external-email-address
 
[Body of the email]

Ctrl D to deliver the message
Related Post