How to send email using the command line in Linux
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.
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
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.
You should read it
- How to use Darwin Mail to manage Gmail professionally
- How to send Email with Windows PowerShell
- Instructions on how to manage email and mailboxes in the Mail application on iPhone / iPad
- How to schedule email delivery on Yandex Mail
- Use and manage Database Mail
- Use Outlook Express to send / receive Yahoo mail
- Instructions on how to Recall, recover mail sent in MS Outlook
- What is Outlook Mail? How to register and use Outlook mail for beginners
- What is Forward Mail? How Forward forwarding mail to another email in Gmail
- What is Loop mail?
- How to set up your own email server on Windows PC
- How to Set Up Apple Mail
Maybe you are interested
How to turn off Facebook Platform to stop sharing personal data Summary, all things from A to Z to Facebook How to build a bot that automatically chat on Facebook Messenger Instructions to delete Mocha account on the phone Rothschild is the richest family of all time If you are looking for the purpose of your life, read this article!