One may need to send emails directly from the command line. Sending emails is crucial to provide real-time notifications and generate reports or alerts. There are various ways to send email in Bash. In this article, I am going to show an easy way to send email to any address using your regular Gmail account.
Key Takeaways
- Sending emails by creating Bash script.
- Sending emails from the command line.
- mailx command for sending emails.
Free Downloads
2 Ways to Send Email Using malix Command in Bash
There are multiple steps one must perform to successfully send emails in Bash. There are several commands to send emails. In this demonstration, I will show you how to send emails using mailx command. The demonstration presumes that Google is your mail service provider. In other words, you use Gmail.
Method 1: Bash Script for Sending Emails
Creating a Bash script to send emails using mailx command is easy. However, before sending emails one must generate app-specific password and install the mailx command successfully.
Step 1: Generate App-Specific Password
In this procedure of sending email in Bash, the first thing one needs to do is to generate an app-specific password. The steps below show how to generate a password that is specific to an application in Gmail.
Steps to Follow >
❶ At first, enable the 2FA. Follow this documentation to enable 2FA on Gmail.
❷ Once you have 2FA enabled, visit this site to generate an app-specific password.
➌ After logging in to your account, you can see the following window. Select Mail from the Select app dropdown list as follows.
➍ From the Select device dropdown list, select Other (Custom name) option.
➎ Now give the name of the device. You can give something memorable as I named it mailx (Ubuntu) in this case. Finally, click on Generate to create the app-specific password.
A password of 16-character is given just after the click. The following image shows the generated app-specific password.
At this point, you have obtained an app-specific password from your E-mail provider. In the subsequent steps, I will show you how to set up an SMTP server and then send mail.
Step 2: SMTP Server Set-up
The next important thing is to configure a SMTP server. But before that one must check whether the server management packages are installed on the system system or not.
Steps to Follow >
❶ At the beginning, open the system (Ubuntu) terminal and run the command below to successfully install the mailx and necessary packages to set up a SMTP server.
sudo apt-get install mailutils
❷ Press “Y” to continue the installation.
➌ After installation it automatically opens the Postfix Configuration window. Select Ok, then press the right arrow and press on ENTER to continue configuring the package.
➍ At this point of configuration select Internet Site and continue with Ok.
➎ Change the System mail name as your wish and continue with Ok. This will successfully install the necessary packages.
➏ If you run the installation command again you will see a message that mailx is already installed in the system. Now, it’s time to configure the /etc/ssmtp/ssmtp.conf file.
➐ To open the SMTP configuration file run the command below:
sudo nano /etc/ssmtp/ssmtp.conf
sudo apt-get install ssmtp
➑ Now change the configuration file as follows.
Let us understand the meaning of each field I changed in the configuration file.
- SERVER: This field indicates from sender email ID.
- mailhub: This field indicates where the mail goes. If you are using Google’s e-mail services, we assume that this goes to Google’s e-mail server. In that case, this should be gmail.com. If your e-mail service provider is not Google, you should go through the documentation of your e-mail provider.
- AuthUser: This is the field where you enter your e-mail address. If, for example, my e-mail address is [email protected], I would enter this address in the AuthUser
- AuthPass: You have to place the 16-character long alphanumeric app specific password in this field. The password I generated is tdthrcmigwfduiow (there are no spaces).
- UseTLS: This uses the TLS protocol to wrap the SMTP contents that enable you to send encrypted e-mail. The recommended value of this field is “YES“.
- UseSTARTTLS: This specifies if the STARTTLS should be used or not. I would recommend you use STARTTLS and fill the field with “YES“.
- rewriteDomain: It should be the domain that can rewrite your mail. Gmail is the rewrite domain in my case.
- Hostname: You can leave the field as it is or change it to your mail ID.
- FromLineOverride: This field specifies whether you are allowed to override the ‘From header‘ of an e-mail or not. I fill the field with “YES”.
Step 3: Writing the Bash script
You are on the final line. Hopefully, now you can send email using the mailx command. Now, I am going to write the Bash script to send emails.
Steps to Follow >
➊ Firstly, write the following command to open a mailx.sh file in the build-in nano editor:
nano mailx.sh
- nano: Opens a file in the Nano text editor.
- mailx.sh: Name of the file.
➋ Copy the following script and paste it into nano. Press CTRL+O and ENTER to save the file; CTRL+X to exit. Alternatively, copy the following script. Paste the script in a text editor and save it as .sh file.
#!/bin/bash
recipient="[email protected]"
subject="Leave"
body="I would like to request you for next two days leave.\\n
\\
n
Regards
Laku"
echo -E "$body" | mailx -s "$subject" "$recipient"
The provided Bash script demonstrates a simple example of sending an email using the mailx command. The script sets the recipient’s email address, subject and body of the email. Then, it pipes the body content to the mailx command with the specified subject and recipient address.
➌ Now, Use the following command to make the file executable:
chmod u+x mailx.sh
- chmod: Changes permissions.
- u+x: Giving the owner executing permission.
- mailx.sh: Name of the script.
./mailx.sh
When executed, this script will send an email to the recipient ([email protected]) with the subject “Leave” and the given body text.
Method 2: Sending Emails from Terminal
This time I am going to demonstrate how you can send an email directly from the system terminal.
Go to the terminal, then write and send emails as shown below. Press CTRL+D after writing the whole email. The email will automatically send to the receiver.When the receiver opens the email he/she will find it.
This is one of the many ways of sending emails in Bash. You may explore other ways and commands of sending emails in Bash.
Comparative Analysis of Methods
Here is a comparative analysis between the above-discussed methods. You can look at the pros and cons of each method to determine the best one for you.
Method | Pros | Cons |
---|---|---|
Bash Script | ● Flexibility to customize the script as per needs. | ● A temporary file is needed to write and save the mail before sending. |
From Terminal | ● Emails can be sent directly from the terminal. So, no need for any temporary file. | ● Tough to schedule mail in this manner. |
I think it is better to write a Bash script instead of sending emails from the terminal directly. In that way you can craft it multiple times and execute the final edition of the script only.
Conclusion
In conclusion, sending emails from the system terminal or creating a Bash script for sending emails is not a straightforward task. However, once you send your first email from the command line, you will be a pro in sending emails with Bash.
People Also Ask
Related Articles
- How to Get Date in Bash [2 Methods with Examples]
- How to Print Time in Bash [2 Quick Methods]
- How to List Users in Bash [2 Easy Ways]
- How to Get Current Time in Bash [4 Practical Cases]
- How to Use Date Format in Bash [5 Examples]
- How to Get Timestamp in Bash [2 Practical Cases]
- How to Copy and Paste in Bash [2 Methods & Cases]
- How to Read Password in Bash [3 Practical Cases]
- Bash Script to Send Email with Attachment [Step-by-Step Guide]
- How to Get IP Address in Bash [3 Methods]
- How to Find and Replace String in Bash [5 Methods]
- How to Get Script Name Using Bash Script? [3 Easy Ways]
- How to Call Another Script in Bash [2 Methods]
- How to Generate UUID in Bash [3 Simple Methods]
- 3 Easy Ways to Write to a File in Bash Script
- How to Write the Output to a File in Bash Script [5 Practical Cases]
- How to Create a List in Bash Scripts? [2 Easy Methods]
- How to Clear History in Bash [2 Practical Cases]
- How to Clear Screen Using Bash Script? [2 Effective Methods]
- How to Check Ubuntu Version Using Bash Scripts? [2 Methods]
<< Go Back to Bash Script Examples | Bash Scripting Basics | Bash Scripting Tutorial