How to Send Email in Bash [2 Easy Methods]

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.

You can read the Comparative Analysis of Methods to find the easy one for you.

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.

Selecting Mail for generating app specific password

➍ From the Select device dropdown list, select Other (Custom name) option.

Selecting Custom name to give a name of the device

➎ 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.

Giving the name of the device

A password of 16-character is given just after the click. The following image shows the generated app-specific password.

Get the app specific password to send email in Bash

Note: Remember there is no space in the generated 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 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

Installing mailx command to "send email in Bash"❷ Press “Y” to continue the installation.Giving confirmation for 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.Continue to configure mailx➍ At this point of configuration select Internet Site and continue with Ok.Select Internet Site and continue➎ Change the System mail name as your wish and continue with Ok. This will successfully install the necessary packages.Change mail name➏ 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.Check the installation of mailx➐ To open the SMTP configuration file run the command below:

sudo nano /etc/ssmtp/ssmtp.conf
Note: The above command can’t open the configuration file if smtp is not installed in the system. Run the command below to install smtp at this stage.
sudo apt-get install ssmtp

➑ Now change the configuration file as follows.Change the configuration file to send email.

Change the configuration file to send emailLet 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 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 file in the build-in nano editor:


Creating a .sh file to send email

  • nano: Opens a file in the Nano text editor.
  • 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.


recipient="[email protected]"

body="I would like to request you for next two days leave.\\n
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
  • chmod: Changes permissions.
  • u+x: Giving the owner executing permission.
  • Name of the script.
Changing permission to send email➍ Finally, Run the script by the following command:

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.

Before moving to the steps below make sure you complete Step 1 and Step 2 of Method 1.

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.Mailing from terminalWhen the receiver opens the email he/she will find it.Mail received by receiverThis 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.


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

How to send emails with multiple recipients in Bash?
To send emails to multiple recipients using mailx command, add the recipients mail one after one delimited by comma.
How to preserve whitespaces while sending emails using mailx command?
To preserve whitespace, including newlines, when sending emails using the mailx command in Bash, you can use the -E option. This option ensures that the body of the email is sent to the recipients without any modifications.
How can I check whether my emails are delivered or not?
Run the mailx or mail command in the terminal to check the delivered emails.

Related Articles

<< Go Back to Bash Script Examples | Bash Scripting Basics | Bash Scripting Tutorial

Rate this post
A Complete Guide for Beginners Enroll Course Now
icon linux
Md Zahidul Islam Laku

Hey, I'm Zahidul Islam Laku currently working as a Linux Content Developer Executive at SOFTEKO. I completed my graduation from Bangladesh University of Engineering and Technology (BUET). I write articles on a variety of tech topics including Linux. Learning and writing on Linux is nothing but fun as it gives me more power on my machine. What can be more efficient than interacting with the Operating System without Graphical User Interface! Read Full Bio

Leave a Comment