How to Use a Shell Script to Send an Email
Email is old, yet is one of the most used means of communication. People can send documents, media, files, etc. via email. Let’s see how you can send an email using shell scripting. However, before an email can go through, you must connect to a mail server.
Here you will find out:
- how to send an email with a shell script
- how to use "mail", "sendmail" and "mutt" command
- when DiskInternals can help you
Are you ready? Let's read!
How can a shell script help you to send an email?
There are quite many ways to send emails if you’re running Linux. You can send an email from the command line or using a shell script. It is possible to create multiple automated scripts and schedule when they should run using the crontab command.
Now, there is no possible way to monitor if the automated scripts execute successfully or not. Thus, most people add the email command so as to receive emails about the status of their automated scripts.
A simple bash script to send an email
Here’s a simple example of a bash script that will send an email message to an identified email address.
In the script above, the address is the default UNIX/Linux hostname used for logging in. Thus, it serves as the sender’s address. For example, if you logged in with "example.com," and then you specified “subject” in the FROM_ADDRESS: line, the final sender’s address will be subject@example.com.
This is the actual code to send this email:
Where –s is used to specify the mail’s subject.
How to attach files using a “mail” command
It is possible to include attachments in your email. Here’s the command to add attachment(s) to an email message.
-a is used for mailx and -A for mailutils (for Debian-based systems)
Example:
Example:
Above, the script contains up to three other email addresses that the message should also be sent to. So, you have to set these other email addresses for “carbon copying”.
Use “sendmail” command
Another way to send email messages on a Linux system is by using the “sendmail” command. This is one of the most common email-sending commands used by Linux system operators. However, you will have to install the “sendmail” program to be able to run this command.
Install with this command: $ Sudo apt install ssmtp
After installation, you can now send the email using the command below:
Here, the email message is saved in a file named “mail.txt.” The email message already contains the email subject and body.
Use “mutt” command to send email
Using “mutt” command, you can send an email on a Linux system. But “mutt” is used mainly to read emails on a terminal from local user mailboxes, POP, or IMAP servers. To install “mutt”, run this command:
Example 1:
Example 2:
If you wish to attach a file, then add the –a variable:
Open Linux files in Windows
Some people don't know it is possible to access files on a Linux partition when operating Windows. Using DiskInternals Linux Reader, you can not only view all your Linux partitions, but you can also access the files saved inside. It provides access to different file systems Ext2/3/4, ReiserFS, Reiser4, HFS, HFS+, FAT, exFAT, NTFS, ReFS, UFS2, RomFS(reader), ZFS (preview only*), XFS (preview only*), Hikvision NAS and DVR (preview only*)). DiskInternals Linux Reader is a handy software tool with a simple interface that is straightforward to use.




With DiskInternals Linux Reader installed on your dual-boot PC or virtual machine, it is very easy to access your Linux files at any time.
Related articles
- About a bash date command
- How to Access Linux Ext2 or Ext3 on Windows
- How to Access Ext4 from Windows
- How to Mount Ext4 on Windows for Free
- Mount Linux Drive on Windows for Free
- Bash Script: All You Need to Know
- An Algorithm: How to Create Bash While Loop
- Linux Shell: What You Need to Know at First
- 5 Basic Shell Script Examples for Your First Script
- Bash: How to Check if the File Does Not Exist
- Bash Time Command on Linux
- How to use bash get script directory in Linux
- How to Check Bash String Equality
- How to Use Linux Wait Command
- Bash: How to Loop Through Files in Directory
- 20 Examples of Bash Find Command
- Bash Cat Command in Examples
- Bash Script SSH: How to Use It
- Bash: A Script For User Input
- Linux ZSH: the basic you need to know
- Basic of Grep in Linux Shell Script
- Shell Script Cut: Basic You Need to Know
- Bash: How to Check if String Not Empty in Linux
- Bash: how to split strings in Linux
- Linux: Sudo in Bash Scripts
- Hello World Script in Bash
- A Linux bin/bash Shell
- Linux: New Line in Shell Script
- Linux: Bash String Ends With
- Linux: Bash Printf Examples
- Linux: Bash First Line
- Linux: $0 in a Shell Script
- Linux: A Bash Startup Script
- Linux: Write a Shell Script
- Shell Script: Replace String in File
- A crontab service shell script
- Bash and sh: is there any difference
- A Bash Dirname Command
- Linux: A Bash Source Command
- A Bash to Loop through Lines in File
- Linux: A Bash Linter
- A Bash Nested Loop
- A Bash Test Command
- A Bash Status of Last Command
- Linux: A Bash Basename Command
- Using Bash to Write to a File
- About Bash Language
- Bash for Loop in One Line
- AWK in a Bash Script
- Learn about useful bash scripts
- Learn about systemd startup script
- About chaining bash commands
- Learn about a bash error code
- Using a bash tee command
- About a bash export command
- Learn about a bash wait command
- Arch Linux install script
- About advanced bash scripting
- To run a shell script in Dockerfile
- About a bash UNTIL loop
- Learn about C shell script
- Learn about Korn shell scripting
- Using /usr/bin/env command
- Whether bash waits for command to finish
- Using bash if 0
- Using && in an IF statement in bash
- If you want to run shell script in background
- The disk you inserted was not readable by this computer error
- Learn about SFTP in bash scripting
- Learn to run Perl script in Linux
- Examples of using the Expect
- Copy command in shell scripts
- Shell script usage
- Install Oracle Database
- Here is everything you need to know about Bash for Windows
- Guide in Pictures
- How to Use Shell Script Sleep Command
- A Bash Multiline Command
