4 examples to Send Email with Attachment from Linux using mailx command

One of the common task for programmers working in Linux machine is to send emails, for example, your Java applications are running on Linux e.g. RHEL 5 or RHEL 6 version and you need to send a report of all the clients connecting to your application. An easy way to accomplish this is to write a shell script, which will dump all clients in the log file and then use grep command and generate a report. If you have to grep logs from multiple hosts, you can also use the SSH command to run the same command on multiple hosts. Once you go the list of clients connecting to your application, you can send the CSV file to yourself in an email. Now, the question is how to send an email from Linux? and more importantly how to send an email with attachments? Well, there are different options based on whether you are running on RHEL 5 or RHEL 6.

For example, my favorite way to send an email with attachment was by using the uuencode and mail command but, unfortunately, that didn't work when we migrated to RHEL 6. So, I had to use the mailx command with -a flag for attachments.

But, mailx is not the only way to send email from Linux, you can also use the mutt command if its available to you.

In this article, I'll show you how to send an email with body and attachment using mailx and mutt command from a Linux host. 

There may be a couple of more options but I haven't explored much, for me these two works fine in both RHEL 5 and RHEL 6 hosts and I was able to finish my scripts with these two commands. I'll explore more when I get some time but if you have some thoughts feel free to share with us.

How to send email with body and attachment from Linux

I mostly use mailx for sending emails, hence most of the examples are of mailx command. It's also important to know the syntax of the mailx command, which will help you to understand the examples as we move one step at a time.

Syntax of mailx command :

mailx -eiIUdEFntBDNHRV~ -T FILE -u USER -h hops -r address -s SUBJECT -a FILE -q FILE -f FILE -A ACCOUNT -b USERS -c USERS -S OPTION users

You can see that -s is used for a subject and -a is used for attachment. You can also CC users using -c options and BCC users using -b option.

Now, one question, which may come to your mind is what would be the sender's email address when you send mail from Linux? Well, it depends on the user who is sending an email.

For example, if you log in using your personal use and send mail then it would be your email but if you log in as application user and send an email then it would be the email configured for that application user account.

Example 1 - Sending email from Linux

If you just want to send a simple email e.g. without attachment, you can simply use the mailx command to do that.

$ mailx -s "test" abc@gmail.com
This is simple email without attachment

This will send a send an email with subject as "test" to recipient abc@gmail.com. Once you type this command it will ask you to type the content in the body. Once you are done, you can press Ctrl + D to end the typing and it will send the email.

Btw, If you are not familiar with Linux operating system and shell itself then Learn Linux in 5 Days and Level Up Your Career on Udemy is a good place, to begin with.

4 examples to Send Email with Attachment from Linux using mailx command

Example 2 - Writing body using echo

In the last example, you need to write the content of the body and then press the Ctrl + D to send the email, but if you already have a message written you can also use echo command along with mailx to send the email directly.

The output of echo can be used as the body as shown below:

$ echo "testing" | mailx -s "test" abc@gmail.com

This time mailx will not wait for your input and will directly send the email with text "texting" in the body.

Btw, if you are not familiar with essential Linux commands and shell built-ins like echo then I suggest you to first go through  Learn Basic Commands in Linux Shell to familiarize yourself with these basics commands.

Example 3 - Email with Attachment from Linux

This is the most popular use of mailx command for me. I always find using this option be it sending a file from Linux host to my windows host or writing scripts which sends email from a Linux machine. Until RHEL 5, my favorite way to send an attachment in email was by using the uuencode command e.g.

$ gzip -c log.txt | uuencode log.gz | mail -s "log file for `date`" abc@gmail.com

This command will first compress the log.txt and then will send as attachment log.gz with subject "log file for a date" to abc@gmail.com, here date will be replaced by current date because we have used `date`, which will execute the date command and put its output here.

The gzip -c command writes output to standard output while keeping the original files unchanged i.e. not changing it as log.gz, which it does without -c option. If you want to learn more about gzip and other compression and archiving command then check out the  Linux Command Line Basics course on Udemy.  It's available in just $9.99 in Udemy's flash sale.

This command has served me well for a long time but unfortunately, it doesn't work in RHEL 6 hosts. If you try that command on an RHEL 6 hosts, instead of receiving an attachment, you will receive some gibberish in email body with no attachment.

So, how do we solve this problem? Is there any other way to send an email to attachment from RHEL 6 hosts? Well, yes, we'll use the mailx command with -a flag, which is used to attach a file.

Here is the example of a mailx command to send a file as an attachment to RHEL 6 servers:

$ echo "mailx works fine in RHEL 6" | mailx -s "texting" -a log.gz abc@gmail.com

This will send an email with the body as "mailx works fine in RHEL 6" and attachment as log.gz with the subject line as "texting".

Example 4 - Adding a User and CC and BCC

If you have to include other email address or user as CC or BCC while sending reports from Linux then you can use the mailx -c and mailx -b for adding users as shown in the following example:

$ echo "message" | mailx -s "test" -c group@gmail.com -b admin@gmail.com abc@gmail.com

This email will be sent to abc@gmail.com and cc'd to group@gmail.com and BCC to admin@gmail.com

How to to Send Email with Attachment from Linux using mailx command

That's all about some of the useful options of mailx command to send emails from Linux. We have seen how to send an email with attachment in both RHEL 5 and RHEL 6 servers as well as examples of interactively typing a message and using echo command to send the mail without waiting for input.

The mailx command has several more options but I didn't need any more other than these. If you want to explore, you can use info mailx command to get more information about mailx. If you want to learn more you can also take Linux Command Line Interface (CLI) Fundamentals course from PluralSight.

Btw, if you don't like the mailx command then alternatively you can use mutt command. It can also attach a file to the email and can do whatever we have done using mailx in this article.

here is a simple example of using mutt command in Linux:

echo "message body" | mutt -s "subject" -a "file_path" "another_file" abc@gmail.com

You can also leave echo command to enter message body interactively. Similar to mailx command,
-a flag allows you to attach files and you can add more than one files as well.

Other Linux Articles and Resources you may like
How to setup cron jobs in Linux
10 example of networking command in Unix
5 Example of kill commands in Unix and Linux
VI Editor examples and tips for beginners
Linux Administration Bootcamp: Go from Beginner to Advanced
Shell Scripting: Discover How to Automate Command Line Tasks

Thanks for reading this article so far. If you like this article and my explanation of sending email from a Linux machine then please share with your friends and colleagues. 

No comments:

Post a Comment