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 | 👨 3404 Views

Above is an article about: "How to send email using the command line in Linux". Hope this article is useful to you. Don't forget to rate the article, like and share this article with your friends and relatives. Good luck!

« PREV POST
NEXT POST »