Bash script to send email

Email is a very common communication medium in today’s world now. Anyone can easily communicate or share any document with friends, family members, and colleagues by sending the email.    Generally, people use any free email server like Gmail, Yahoo, Hotmail etc. for sending the email. But you can send email from command line easily in Linux operating system. There are many ways to send email in Linux. Some common and easy ways to send email in Ubuntu operating system from the terminal is shown in this tutorial.


Many Linux command is available to send email from the command line or by using a bash script. But any email sending command will not work if no SMTP server is set up properly in the system. You can setup your own SMTP server to send email from the command line or you can use any free SMTP server of any well-known email service provider like Gmail or Yahoo. SMTP server of a Gmail account is used in this tutorial. To do this task, you have to select a Gmail account and enable the option of ‘Allow less secure apps’ for that account to send the email. You can visit the following tutorial to enable this option.

After enabling this option, you have to open the file ‘ /etc/ssmtp/ssmtp.conf’ with ‘root’ privilege and add the following lines at the end of the file. You have to set your email address to ‘AuthUser’ and your email password to ‘AuthPass’ to complete the setup.


Example-1: Using Sendmail Command

One of the popular email sending commands of Linux is `sendmail`. The user can easily send email from the command line by using this command. If Sendmail package is not installed in the system then run the following command to install the package.

$ sudo apt install ssmtp

Suppose, the email content is stored in a file named ‘email.txt’ with the following content. Here, the text after the ‘Subject:’ will be sent as email subject and the remaining part will be sent as email body.


Subject: Sending email using sendmail
Testing email body

Run `sendmail’ command with recipient email address like the following command.

$ sendmail  < email.txt

Example-2: Using ‘mail’ Command

The most common command for sending email in Linux is `mail` command. This command is not installed on Ubuntu by default. Run the following command to install `mail` command.

$ sudo apt install mailutils

The following command will show the version of this command if it is installed in the system.

$ mail -V

‘-s’ option is used in the `mail` command to define the subject of the email. Run `mail’ command by ‘-s’ option with email subject and the recipient email address like the following command. It will ask for Cc: address. If you don’t want to use Cc: field then keep it blank and press enter. Type the message body and press Ctrl+D to send the email.

$ mail -s ‘subject’

If you want to add email message body in the command then use ‘<<<’ operator like the following command.

$ mail -s ‘message subject’ <<< ‘testing message body’

You can also add the sender email address with  `mail` command by using the ‘-a’ option. The following command will send the email with the subject, ‘message subject’, sender’s name and email address, ‘Admin<[email protected]>’ and recipient email address, [email protected] and message body, ‘testing message’.

$ mail -s ‘message subject’ -a From:Admin<> <<< ‘testing message’

You can also send an email message body by using echo and pipe( | ) command. The following command uses the pipe (|) command to send the message body.

$ echo "testing message" | mail -s "message subject"

Email can be sent to multiple recipients by using `mail` command and adding the recipients’ addresses by separating comma. The following command will be sent an email to two recipients.

$ mail -s "Testing email" ,  < test.txt

Example-3: Using `mutt` command

Like `mail` command, `mutt` command is not installed on Ubuntu by default. Run the following command to install `mutt` command.

$ sudo apt  install mutt

`mutt` command works similar to `mail` command but `mutt` command has own editor to send the email. Run the following command with the valid email address to send the email.

$ echo “testing message” | mutt -s "Testing Email"

You can attach any file using the ‘-a’ option with `mail` or `mutt` command.  The following command will attach the file items.txt while sending the email.

$ echo "testing message" | mutt -s "message subject" -a items.txt

Example-4: Using ‘SSMTP’ Command

Another email command is `ssmtp` command. If ssmtp is not installed before then install the package first. The installation command for ssmtp is shown in the example-1. ssmtp and sendmail commands work similarly.

Run the following command with the valid email address. Type the email subject and body. Press ctrl+D to exit and send the email.

$ ssmtp

Example-5: Using mailx Command

You can send HTML content as email body without just sending the text message. Create an HTML file named ‘test.html’ to use as a message body.


<h3> Email Sending </h3>
<font size="2" color="red">Testing Message body</font>

`mailx` command works like `mail` command. You have to mention Content-Type as text/html to send HTML content as the message body in `mailx` command. The following command will send the content of ‘test.html' file as the email message body.

$ mailx -a ‘Content-Type: text/html’ -s "Sending email by using mailx "
 < test.html "[email protected]"

When you will check the inbox of recipient email account then the list of emails will be shown as the following image.


After practicing the above commands properly, hope the reader will be able to send email from the command line easily in Linux.

Sandclock IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, Sandclock IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Bash Comments

How to use single and multiple line comments in BASH Using comments in any script or code is very important to make the...

Bash text and background printing in different colors

A terminal is a very important application for any Linux operating system. It is mainly used to execute different commands...

74 Bash Operators Examples

Different types of operators exist in Bash to perform various operations using bash script. Some common groups of bash...
Bài Viết

Bài Viết Mới Cập Nhật

Hướng dẫn chuyển đổi windows server windows evaluation to standard và active windows server 2008 + 2012 + 2016 + 2019

How to Update Ubuntu Linux

Squid Proxy Manager cài đặt và quản lý Proxy Squid tự động trên ubuntu

Hướng dẫn cài đặt Apache CloudStack

Hướng dẫn ký file PDF bằng chữ ký số (chữ ký điện tử) và sửa lỗi mới nhất 2021 foxit reader