How to send Email with Body and Attachment from Linux Machine? mutt and mailx command Example

One of the common tasks for programmers working in a Linux machine is to send emails and transfer files from Linux machine to Windows machine. While there are utilities like WinSCP which you can use to transfer files between Windows and Linux machine, I generally found using mailx command much easier, especially when you don't have WinSCP or any other utility. But, apart from transferring files between Linux and Windows, there are many more scenarios where you need to send emails from Linux machine, 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. 

One of the easiest 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. 

There may be a couple of more options but I haven't explored much, for 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 and if you want to learn more, I suggest you go through the Linux Mastery: Master the Linux Command Line in 11.5 Hours course on Udemy.




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 option 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. How to send email with subject from Linux using mailx command

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 a simple email without an attachment
EOT

This will send an email with the 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 the 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




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" 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 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 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`" abc@gmail.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 abc@gmail.com, 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.

Email with Attachment from Linux


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 host, 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 an 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 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 - How to add a User and CC and BCC on Email from Linux

If you have to include other email addresses or users 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

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.

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

Btw, if you don't like the mailx command then alternatively you can use the 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 the mutt command in Linux:

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

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


5. How to send email from Linux with multiple attachments

You can attach more than one file into your email by using "-a" flag multiple times with file names as shown in below examples. You can see that we have added 4 log files into our email and all these files will be delivered to all the recipients 

$ echo "log files for today" 
   | mailx -s "log Files" 
            -a 20210914_log.csv 
            -a 20210914_app_log.csv 
            -a 20210914_data.csv 
            -a 20210915_data.csv 
            abc@gmail.com

It's common to make mistake while sending multiple files via emails. so make sure you use "-a" before each attachment, if you only use one attachment then only one file will be sent. 



6. How to send mail from Linux to multiple recipients

Here is another example of mailx command to send a file to multiple people from Linux. one will send first file to all the recipients while second will attach 4 files and send to one person. 

$ echo "log files for today" 
| mailx -s "log Files" 
        -a 20210914_log.csv 
         user@abc.com
         joe@god.com
         peter@gallore.com


Both these examples are quite similar the key difference is how many times you have used "-a" options. First example will send first file to all the recipients while second will attach 4 files and send to one person. Make sure you use as many as "-a" option in front of file which you want to attach.


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 a Windows machine and then send them as mail, you can directly send the mail from Linux now.


Other Linux Articles and Resources you may like
  • How to set up cron jobs in Linux (Crontab example)
  • 10 examples of Networking commands in Unix (nslookup)
  • 5 Example of kill commands in Unix and Linux (example)
  • Top 5 Courses to learn Vim Editor (online courses)
  • VI Editor examples and tips for beginners (vi examples)
  • 10 Linux command line courses for Beginners (courses)
  • How does the nslookup command work in UNIX? (answer)
  • 10 examples of lsof command in Linux? (examples)
  • 7 Best Linux Courses for DevOps Engineers (Linux courses)
  • How to use the netstat command to find which process is listening on a port? (example)
  • Linux find + du + grep example (example)
  • 10 Examples of curl command in Linux (cURL)
  • 10 Examples of chmod command in Linux (chmod)
  • A Practical Guide to Linux Commands, Editors, and Shell Programming (guide)

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

P. S. - If you are looking for some free online courses to start your Linux journey, you should check out my list of Free Linux Courses for Programmers, Cloud Engineers, Data scientists,  IT Professionals, and System Administrators.

7 comments:

  1. This is a very best article and whenever I have to send mail or files from Linux, I always refer to this article. Just to add my 2 cents, you can also use mailx command to send multiple files from Linux and you can also send mails to multiple recipients. The key is to use the "-a" option correctly.

    Here are two mailx example:
    $ echo "log files for today" | mailx -s "log Files" -a 20210914_log.csv 20210914_app_log.csv 20210914_data.csv 20210914_

    $ echo "log files for today" | mailx -s "log Files" -a 20210914_log.csv -a 20210914_app_log.csv -a 20210914_data.csv -a 20210914_data.csv abc@gmail.com

    First one will send first file to all the recipents while second will attach 4 files and send to one person. Make sure you use as many as "-a" option in front of file which you want to attach.

    ReplyDelete
  2. echo is very important, just using mailx command without echo will not work

    ReplyDelete
  3. I tried this command but keep getting error
    like Address family not supported, can you please advise?

    ReplyDelete
  4. I am trying to get the list of files in a directory
    For entry in $"search_dir"/*

    Let's say in x directory i have 3 files
    And when i print $entry i am having the file names

    But when giving below command

    echo "The following files exists : "$entry" | mailx -s abc@xyz.com

    I am getting 3 emails instead of one email.
    Can anyone suggest?

    ReplyDelete
  5. Are you running in loop? If you are using for loop then its expected as you are calling mailx three times, each time with a different file so you should get three emails with different file. If you want to send all three files in one email then don't run loop and just use -a firstfile -a secondfile etc. or use loop to create the command but run outside the loop

    ReplyDelete
  6. want to send a file as a body and few echo outputs should come as BOLD and COLOR, when I run echo's its coming as bold...etc but when I send using mailx ...< its showing as normal text command

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.