Pustaka Taratsa

Baca teks (EPUB): Linux: mailx command (en)

1 bagian

Teks ini diambil langsung dari berkas EPUB; tata letak dan gambar tidak disertakan. Untuk tampilan lengkap, silakan merujuk pada dokumen asli.

Daftar isi
  1. Linux: mailx command (en)
  2. Install the mailx command
  3. Using the mailx command
  4. 1. Simple mail
  5. 2. Take message from a file
  6. 3. Multiple recipients
  7. 4. CC and BCC
  8. 5. Specify From name and address
  9. 6. Specify "Reply-To" address
  10. 7. Attachments
  11. 8. Use external SMTP server
  12. 9. Verbose - watch SMTP communication
  13. References

Linux: mailx command (en)

Sumber: https://onnocenter.or.id/wiki/index.php/Linux:_mailx_command_(en)

Send emails from the command line

The mail command is essential and should be available on any Linux server so that various services and other web applications can generate and transmit emails.

In a previous post on the mail command, we saw how the mail command can be used to send emails from the command line on your Linux server.

In this tutorial, we will be using an enhanced version of the mail command called mailx (or just mail when installed), which can do many more things than the older mail command from the GNU mailutils package. How it works

The mail/mailx command requires a local SMTP server (MTA) running to deliver the emails. The email route is somewhat like this:

mail -> sendmail -> local MTA -> recipient MTA [Inbox]

The recipient MTA would be Gmail's SMTP server if your recipient is someone at gmail.com, for example. For the local MTA, you need to install an SMTP server like Postfix. A basic installation of Postfix with minimal configuration would work in most cases.

Install the mailx command

On Ubuntu/Debian based systems, the mailx command is available from two different packages:

  1. heirloom-mailx
  2. bsd-mailx

We shall be using the heirloom-mailx package because it has more features and options. On CentOS/Fedora based systems, there is only one package named "mailx," which is the heirloom package.

To find out which mailx package is installed on your system, check the "man mailx" output and scroll down to the end, and you should see some useful information.

# ubuntu/debian
$ sudo apt-get install heirloom-mailx

# fedora/centos
$ sudo yum install mailx

Using the mailx command

Once installed, the mailx command can be directly referenced with the name mail, so you just type that in the command line.

1. Simple mail

Run the following command, and then mailx would wait for you to enter the email message. You can hit enter for new lines. When done typing the message, press Ctrl+D, and mailx would display EOT. After that, mailx automatically delivers the email to the destination.

$ mail -s "This is the subject" [email protected]
Hi someone,
How are you?
I am fine.
Bye
EOT

2. Take message from a file

The message body of the email can also be taken from a file:

$ mail -s "This is the Subject" [email protected] < /path/to/file

The message can also be piped using the echo command:

$ echo "This is the message body" | mail -s "This is the Subject" [email protected]

3. Multiple recipients

To send the mail to multiple recipients, specify all the emails separated by a comma:

$ echo "This is the message body" | mail -s "This is the Subject" 
[email protected],[email protected]

4. CC and BCC

The "-c" and "-b" options can be used to add CC and BCC addresses respectively:

$ echo "This is the message body" | mail -s "This is the Subject" -c [email protected] [email protected]

5. Specify From name and address

To specify a "FROM" name and address, use the "-r" option. The name should be followed by the address wrapped in "<>":

$ echo "This is the message body" | mail -s "This is the Subject" -r 
"Harry<[email protected]>" [email protected]

6. Specify "Reply-To" address

The reply-to address is set with the internal option variable "replyto" using the "-S" option:

$ echo "This is the message" | mail -s "Testing replyto" -S replyto="[email protected]" 
[email protected]

7. Attachments

Attachments can be added with the "-a" option:

$ echo "This is the message body" | mail -s "This is the Subject" -r 
"Harry<[email protected]>" -a /path/to/file [email protected]

8. Use external SMTP server

This is an exclusive feature, available only with heirloom mailx and not with bsd mailx, the mail command from GNU mailutils, or the mutt command.

The mailx command can use an external SMTP server to relay the message. The syntax is a bit lengthy but makes sense:

$ echo "This is the message" | mailx -v -r "[email protected]" -s "This is the subject" -S smtp=mail.telkom.net [email protected]

For Gmail specifically, you would need to enable less secure apps settings before you can send mail like this.

9. Verbose - watch SMTP communication

When using external SMTP servers, you can choose to watch the entire SMTP communication done in the background. This is useful especially when testing or debugging SMTP servers.

$ echo "This is the message body and contains the message from heirloom mailx" | mailx -v -s "This is the subject" -S smtp="smtp.gmail.com:587" -S smtp-use-starttls -S smtp-auth=login -S smtp-auth-user="[email protected]" -S smtp-auth-password="mypassword" -S ssl-verify=ignore [email protected]

References