In this tutorial, we are going to write a bash script that will send an email from your terminal on Linux. The script is simple, it asks you enter 3 inputs, message, subject and the email address where you want to send this email to.
Script also checks if the package mailutils is installed on your system or not, and if this package is not found on your system then the script will install it for you.
mailutils is the package that we are using to send the email, and it is required to be installed on your system before you use our script to send the emails.
Once script checks for the requirements you have to press enter to continue sending your emails.
Step 1 - Create sendsh.sh
I chose sendsh.sh as the name for the script, so to create sendsh.sh we can use the touch command
Step 2 - Bash script
Copy the script below into sendsh.sh, and press ctrl +x to save the code
#!/bin/bash #Name: sendsh.sh #Version : 1.0 #Written by: Endrit Qerreti #Copyright (c) Endrit Qerreti 2023 if_mailutils="$(dpkg -l | grep mailutils)" if [[ -z "$if_mailutils" ]]; then echo -e "mailutils doesn't seem to be installed on your system\nPress enter to install it\n" read required install_mailutils="$(sudo apt install mailutils -y)" else echo -e "mailutils is installed, you are good to go ahead and send your email,\npress enter to continue" read continue clear message="Write the message:" echo -e "\n"$message"\n" read message subject="Set the subject:" echo -e "\n"$subject"\n" read subject email="Set the email address you want to send the email to:" echo -e "\n"$email"\n" read email echo -e "\nThe email is being sent to $email" send_email=$(echo "$message" | mail -s "$subject" $email) fi
However, postfix it's not the only solution to achieve this, you can also use ssmtp instead of postfix, follow our tutorial here.
Get our premium bash collection
And you should be good to go and make sendsh.sh executable
Step 3 - Make sendsh.sh executable
To make sendsh.sh executable by only the user account that you are using, run
chmod u+x sendsh.sh
If you wan to make it executable by everyone then you can use
chmod +x sendsh.sh
Assuming you have multiple users on your system and you want all of them to be able to run the script, then use
Step 4 - Launch sendsh.sh
Once you have saved the bash script into sendsh.sh, and given the right permission for executing, all you have to do now is to launch it
Open your terminal and run
Step 5 - Send your email
When you launch sendsh.sh, script will perform a check to see if mailutils package is installed on your system.
If mailutils is installed on your system you should see the message " mailutils is installed, you are good to go ahead and send your email, press enter to continue" and you will need to press enter to continue using the script
If mailutils is not installed on your system, script will prompt with "mailutils doesn't seem to be installed on your system, press enter to install it" Then simply press enter to install mailutils.
Next, you need to enter :
3) Email address to send the email
And the script will display a message that the email was sent.
In this tutorial, we wrote a simple bash script that you can use to send emails directly from your terminal to any email address. You don't have to type or remember a long command in order to send an email using terminal, sendsh.sh makes this task easier for you.