How to send email using the command line in Linux

Network administration - In Linux command line can also be used very useful when you know how to use it. You can separate data, check processes, and perform many other useful tasks through it. The command line tool can also be used to create a report and mail it to a certain address. In this article, I will show you how to use the Linux command to send mail with a script. In addition, it is the sending of attachments from the command line.

MAIL

Before running a quick check to make sure that the 'sendmail' application is installed and working properly. Please execute the command below, replace 'you@youremailid.com' with your email address.

# mail -s 'Hello world' you@youremailid.com

How to send email using the command line in Linux Picture 1 Press Enter / return key, then you will be taken to a new line. Enter the text 'This is a test from my server'. After entering the text, press the return key again. Then press the combination of Control + D keys to continue. Prompt the command to ask you if you want to mark a copy of the mail to another address, press Control + D again. Check your mailbox. This command will send a mail to an email ID mentioned with the subject 'Hello world'.

To add content to the body of mail while running the command, you can use the options below. If you want to add your own text:

# echo 'This sẽ đi vào cơ sở dữ liệu của thư.' | mail -s 'Hello world' you@youremailid.com

And if you want mail to read content from a file:

# mail -s 'Hello world' you@youremailid.com

Some other useful options in this mail order are:

-s subject (subject of mail)
-c email-address (Mark a copy for this 'email-address' address or CC)
-b email-address (Mark BCC)

Here's how you can use these options:

# echo 'Welcome to the world of Calvin n Hobbes' | mail -s 'Hello world' calvin@cnh.com -c hobbes@cnh.com -b susie.derkins@cnh.com

MUTT

One of the major returns in using mail commands is that it does not support sending attachments. However, the mutt command supports that. We have found that this feature is very useful for scripts to create non-original reports or small-sized backups. Mutt allows you to do a lot of work besides just sending attachments. It also has more features than mail commands. Let's go explore the basics that can be done with this command. Here's how you can attach a file to your mail:

# echo 'Sending an attachment.' | mutt -a backup.zip -s 'attachment' calvin@cnh.com

This will send a mail to calvin@cnh.com with the subject 'attachment', the body is 'Sending an attachment.' and includes an attachment backup.zip. Like the mail command, you can use the '-c' option to mark CC to another mail ID.

Send mail from a script

With the basics already introduced, it is possible to send mails from your scripts. This is a simple script that we introduce to read the usage space on the hard drive area and mail that data to you.

#! / bin / bash
df -h | mail -s 'disk space report' calvin@cnh.com

Save these lines in a file on the Linux server and run this file. You will receive an email containing the results of the commands in it. However, if you need to send more data, you need to write the data into a text file and enter it into the body of the mail while composing the mail. Here is an example of a script that performs the task of viewing the hard disk and memory performance, writing that data to a temporary file, and then importing it into the body of a mail and sending it:

#! / bin / bash
df -h> /tmp/mail_report.log
free -m >> /tmp/mail_report.log
mail -s 'disk and RAM report' calvin@cnh.com

There is a more complicated issue here. You have to get a back up of files and mail later. First the mail folder will be saved. It will then be sent as an email attachment with the mutt command. This is the script to do that:

#! / bin / bash
tar -zcf /tmp/backup.tar.gz / home / calvin / files
echo | mutt -a -s /tmp/backup.tar.gz 'daily backup of data' calvin@cnh.com

The Echo at the end of the last line will add a blank part to the body of the mail that will be set.

3.8 ★ | 17 Vote

May be interested

  • How to schedule Linux commands with 'at'How to schedule Linux commands with 'at'
    utility at very easy to use. users only need to give time and date as command line parameters, then enter one or more commands to execute.
  • How to find the MAC address using the command line in LinuxHow to find the MAC address using the command line in Linux
    today, tipsmake.com will talk about an important command in linux, ip. this command works on all linux distributions, including ubuntu, arch linux, debian, fedora, etc. '
  • How to check internet speed with the command line in LinuxHow to check internet speed with the command line in Linux
    today, tipsmake.com will read how to check the speed of the internet from the command line via terminal in various popular linux distributions, including ubuntu, fedora and arch linux.
  • How to manage Linux services using SystemdHow to manage Linux services using Systemd
    one of the most important skills that any new systemd user needs to learn is how to manage computer services. this article will teach you the basics: start, stop, enable and disable services from the linux command line.
  • Instructions for using find command in LinuxInstructions for using find command in Linux
    the find command is one of the most important and handy commands on a linux system. as its name suggests, the command can find files on a linux pc based on a variety of conditions and variables you set.
  • How to use the which command in LinuxHow to use the which command in Linux
    the which command in linux determines the executable binary, which will execute when you issue a command to the shell. if you have different versions of the same program on your computer, you can use which to find out which shell will use.
  • The dd command in Linux, How to use the dd commandThe dd command in Linux, How to use the dd command
    dd is a command line utility for unix-like and unix operating systems, with the main purpose of converting and copying files.
  • How to Manage Users in LinuxHow to Manage Users in Linux
    unlike windows, as a unix-like system, linux was conceived of as a multi-user system from its inception. the following sections deal with user management through the linux command line. type adduser -d /home/users/ into the command line.
  • 5 free websites and online games to learn about the Linux command line5 free websites and online games to learn about the Linux command line
    linux is popular for its reliability and many practical applications. if you want to know more about linux, here are 5 websites that will help you learn it interactively.
  • How to Send Mail in LinuxHow to Send Mail in Linux
    this wikihow teaches you some basic ways to send email on your linux desktop system. if you're using a desktop version of linux like debian or ubuntu, the operating system comes with a built-in graphical mail app called thunderbird. if you...