One of the common tasks for programmers working in a Linux machine is to send emails, for example, your Java applications are running on Linux like 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 the 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 have got the list of clients connecting to your application, you can just send the CSV file to yourself in an email or to your application DL, or a group of users you want to.
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 an 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 the -a flag for attachments. But, mailx is not the only way to send email from Linux, you can also use the mutt command if it's 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.
Linux mailx + mutt Command Examples
I mostly use mailx for sending emails, hence most of the examples are of the 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 the -c options and BCC users using the -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 an application user and send an email then it would be the email configured for that application user account.
1. Sending email from Linux using mailx
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" firstname.lastname@example.org This is a simple email without an attachment EOT
This will send an email with the subject as "test" to recipient email@example.com. Once you type this command it will ask you to type the content in the body. Once you are done, you can press the Ctrl + D to end the typing and it will send the email.
2. Writing Mail body using Echo command
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 the 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" firstname.lastname@example.org
This time mailx will not wait for your input and will directly send the email with text "texting" in the body.
Example 3 - Email with Attachment from Linux
This is the most popular use of the mailx command for me. I always find using this option be it sending a file from a Linux host to my windows host or writing scripts that send email from a Linux machine. Until RHEL 5, my favorite way to send an attachment in the email was by using the uuencode command e.g.
$ gzip -c log.txt | uuencode log.gz | mail -s "log file for `date`" email@example.com
This command will first compress the log.txt and then will send as attachment log.gz with the subject "log file for a date" to firstname.lastname@example.org, where the 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 the -c option. If you want to learn more about gzip and other compression and archiving commands then check out the Linux Command Line Basics course on Udemy. It's available for 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 the 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 the -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 email@example.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 firstname.lastname@example.org -b email@example.com firstname.lastname@example.org
This email will be sent to email@example.com and cc'd to firstname.lastname@example.org and BCC to email@example.com 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 the 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. here is a simple example of using the mutt command in Linux:
echo "message body" | mutt -s "subject" -a "file_path" "another_file" firstname.lastname@example.org
You can also leave the echo command to enter the message body interactively. Similar to the mailx command, -a flag allows you to attach files and you can add more than one file as well. That's all about some of the useful options of the mailx command to send emails from Linux. We have seen how to send an email with attachments in both RHEL 5 and RHEL 6 servers as well as examples of interactively typing a message and using the echo command to send the mail without waiting for input. Now, you don't need to use tools like WinSCP or FileZilla to first copy your data into Windows machine and then send them as mail, you can directly send the mail from Linux now.
The Tech Platform