Oct 23, 2023 4 min read

How to Send Mails With Attachments Using mailx Command?

Send mails with attachments using mailx command with our step-by-step tutorial. A utility in systems that allows you to send emails directly.

Send Mails With Attachments Using mailx Command
Table of Contents

Introduction

Before we start talking about how to send mails with attachments using mailx command, let's briefly understand-What is Mailx ?

The mailx command is a widely-used utility in Unix-like systems that allows you to send emails directly from the command line. While mailx supports plain text emails, it also enables you to attach files to your messages. Users can create, send, and receive emails with attachments by using this command from the command line.

This tutorial will walk you through the process of using the mailx command to send emails with attachments, enabling you to share files conveniently. We will also address a few FAQs on how to send mails with attachments using mailx command.

How Do I Use The mailx Command To Send Mail With Attachments?

Emails can be sent from the terminal using the "mailx" command. It can be used to send emails with attachments and emails in plain text.

Prerequisite

Use the following instructions to install the "mailx" command line support for Linux:

sudo apt install mailutils  # Debian/Ubuntu/Linux-Mint
sudo yum install mailx      # CentOS/Redhat
sudo dnf install mailx      # Fedora/RHEL

Syntax With Explanation

The following gives the "mailx" command's basic syntax for sending emails with attachments:

echo "This is the body of the email" | mailx -a /path/to/attachment.pdf -s "Subject of the email" [email protected]
  • "echo" command is used to provide the email's body.
  • "-a" option is used to attach a file to the email.
  • "-s" option specifies the subject of the email.
  • "[email protected]" is the recipient's email address.

Example 1: Send an Email With Attachments

Use the "-a" option and the path to the file you want to attach to send an email with an attachment. The default mail that the user can choose during the "mailx" command installation is the sender mail. For instance, the following is how "[email protected]" will receive the "file.txt" and "file2.txt" attachments:

echo "Hello Sir, Always Stay Happy" | mailx -A file.txt file2.txt -s "Acknowledgment" [email protected]

The output indicates that a certain address has received both the "file.txt" and "file2.txt" attachments.

Example 2: Send an Email Indicating the Sender Address

When sending an attachment, users can use the "-r" option to indicate the sender address. In this instance, "[email protected]" will attach the "file.txt" and send it to "[email protected]":

echo "Have a Nice Day" | mailx -A file.txt -s "Wish" -r [email protected] [email protected]

The output reveals that "[email protected]" received both "file.txt" attachments.

Example 3: Send an Email Mentioning the CC and BCC Recipient

Using the "c" and "b" options, you can specify a CC and BCC recipient, respectively. In this instance, "[email protected]" sends "file.txt" as an attachment while keeping "[email protected]" and "[email protected]" in the CC and BCC fields, respectively:

echo "Hello Sir, Great Wishes" | mailx -s "Announcement" -A file.txt [email protected] c [email protected] b [email protected]

The above-mentioned addresses have received the "file.txt" attachment.

FAQs on Using the mailx Command to Send Mail with Attachments

How do I use mailx to send a basic email without attachments? 

To send a basic email without attachments using mailx, execute the command followed by the recipient's email address. Then, press enter and type the subject of the email. After pressing enter once more, you can compose the message. To send it, press Ctrl + D or enter . on a new line.

What options are available for attaching files with mailx?

To attach files with mailx, use the -a option followed by the file path(s) you want to attach. You can specify multiple files by separating their paths with commas.

How do I send an email with attachments using mailx? 

Compose your email as usual, specifying the recipients, subject, and message body. Then, use the -a option to attach one or more files to the email.

Which file types can be attached with mailx?

mailx can attach any type of file. It treats attachments as binary data and does not differentiate file types during the attachment process.

Is there a size limit for attachments with mailx? 

The size limit for attachments with mailx depends on the email server's and recipient's email service's limits. Some servers may have restrictions on the size of attachments, so it is advisable to check with your email service provider or system administrator.

Can I send multiple attachments with different names using mailx? 

Yes, you can send multiple attachments with different names using mailx. Simply include the -a option followed by the file paths separated by commas.

Can I send both attachments and inline text using mailx? 

Yes, you can compose an email with both attachments and inline text using mailx. Simply include the attachment files with the -a option, and then compose the rest of the email using the regular mailx syntax.

Can mailx handle multiple recipients? 

Yes, mailx can handle multiple recipients. Simply separate the email addresses with commas when specifying the recipients.

Conclusion

The "mailx" function is available on Linux and allows users to send and receive email messages together with attachments. To send an email with attachments, use the syntax "echo "body" | mailx -a attachment.pdf -s "Subject" [email protected]".

This tutorial shows how to use the "mailx" command to send emails with attachments.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.