Using the mailx Command (with examples)
Use Case 1: Sending Mail with a Subject and Recipient Address
To send a plain text email with a subject and recipient address, use the following command:
mailx -s "subject" to_addr
Motivation: This use case is useful when you want to send a simple email message to a specific recipient. It is a quick and convenient way to send a short message without having to open a separate email client.
Explanation:
-s "subject"
: Specifies the subject of the email. Replace “subject” with the desired subject for your email.to_addr
: Specifies the recipient’s email address. Replace “to_addr” with the email address of the intended recipient.
Example Output:
$ mailx -s "Hello" user@example.com
Type your message. Press Ctrl+D when finished.
Hello, how are you doing?
^D
EOT
In this example, the mailx
command sends an email with the subject “Hello” to the recipient’s address. The user then types the email content, followed by Ctrl+D
to indicate the end of the message. The output shows EOT
(End of Text), indicating that the email has been sent.
Use Case 2: Sending Mail with Content from Another Command
To send an email with the message content generated by another command, use the following command:
echo "content" | mailx -s "subject" to_addr
Motivation: This use case is helpful when you want to send an email with dynamic content generated by a command output. It allows you to include the output of a script, command, or program in the body of the email.
Explanation:
echo "content"
: Generates the content of the email message.|
: Pipes (redirects) the output ofecho
command to themailx
command.to_addr
: Specifies the recipient’s email address. Replace “to_addr” with the email address of the intended recipient.
Example Output:
$ echo "This is the output of a command" | mailx -s "Command Output" user@example.com
In this example, the output of the echo
command is piped to the mailx
command as the email content. The email is sent with the subject “Command Output” to the recipient’s address.
Use Case 3: Sending Mail with Content Read from a File
To send an email with the content read from a file, use the following command:
mailx -s "subject" to_addr < content.txt
Motivation: This use case is useful when you want to send the content of a file as the email message. It allows you to easily send files via email without manually copying and pasting the content.
Explanation:
to_addr
: Specifies the recipient’s email address. Replace “to_addr” with the email address of the intended recipient.< content.txt
: Redirects the content of thecontent.txt
file as the body of the email.
Example Output:
$ mailx -s "Report" user@example.com < report.txt
In this example, the mailx
command sends an email with the subject “Report” to the recipient’s address. The content of the email is read from the report.txt
file and included in the email body.
Use Case 4: Sending Mail with Recipient and CC Address
To send an email to a recipient and carbon copy (CC) it to another address, use the following command:
mailx -s "subject" -c cc_addr to_addr
Motivation: This use case is helpful when you want to send an email to the primary recipient and also inform another person or group via carbon copy (CC). It ensures that both the primary recipient and the CC recipient receive the email.
Explanation:
-s "subject"
: Specifies the subject of the email. Replace “subject” with the desired subject for your email.-c cc_addr
: Specifies the email address to be carbon copied (CC). Replace “cc_addr” with the email address of the CC recipient.to_addr
: Specifies the recipient’s email address. Replace “to_addr” with the email address of the primary recipient.
Example Output:
$ mailx -s "Meeting Reminder" -c manager@example.com colleague@example.com
In this example, the mailx
command sends an email with the subject “Meeting Reminder” to the primary recipient (colleague@example.com
) and carbon copies the manager (manager@example.com
) as requested.
Use Case 5: Sending Mail with Sender Address
To specify a sender address when sending an email, use the following command:
mailx -s "subject" -r from_addr to_addr
Motivation: This use case is useful when you want to override the default sender address and specify a different email address as the sender. It allows you to send emails on behalf of another email account or a specific role.
Explanation:
-s "subject"
: Specifies the subject of the email. Replace “subject” with the desired subject for your email.-r from_addr
: Specifies the email address to be used as the sender. Replace “from_addr” with the desired sender’s email address.to_addr
: Specifies the recipient’s email address. Replace “to_addr” with the email address of the intended recipient.
Example Output:
$ mailx -s "Regarding Project" -r project@example.com user@example.com
In this example, the mailx
command sends an email with the subject “Regarding Project” to the recipient (user@example.com
) and specifies the sender’s address as project@example.com
.
Use Case 6: Sending Mail with an Attachment
To send an email with an attachment, use the following command:
mailx -a path/to/file -s "subject" to_addr
Motivation: This use case is helpful when you want to send a file as an attachment via email. It allows you to share files with others conveniently.
Explanation:
-a path/to/file
: Specifies the path to the file you want to attach. Replace “path/to/file” with the actual path and filename.-s "subject"
: Specifies the subject of the email. Replace “subject” with the desired subject for your email.to_addr
: Specifies the recipient’s email address. Replace “to_addr” with the email address of the intended recipient.
Example Output:
$ mailx -a /path/to/document.pdf -s "Important Document" user@example.com
In this example, the mailx
command sends an email with the subject “Important Document” to the recipient (user@example.com
) and attaches the file document.pdf
located at /path/to/
as an attachment.
Conclusion
In this article, we explored various use cases of the mailx
command with code examples. We learned how to send emails with different options, such as specifying the subject, recipient and CC addresses, sending email with content from another command or a file, specifying the sender address, and attaching files. These examples demonstrate the flexibility and convenience of the mailx
command in sending and receiving emails from the command line.