Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Linux \ command-line tool for send e-mail     - Nginx load balancing configuration (TCP proxy) (Server)

- DNF Command Tutorial (Linux)

- Use nice, cpulimit and cgroups limit cpu usage (Linux)

- Analysis of potential problems through custom Orabbix monitoring Oracle (Database)

- CentOS 5.5 kernel upgrade installation iftop (Linux)

- Linux kernel network subsystem analysis (Programming)

- Initialization and starting process of Linux (Linux)

- crontab task scheduling Health Check (Linux)

- Whisker Menu 1.4.3 Install menu Linux (Linux)

- MySQL 5.6 Open full query log (Database)

- CentOS6.7 text installation system (Linux)

- CentOS cross compiler core Raspberry Pi 2 (Linux)

- MySQL root password reset under CentOS (Database)

- Protect against network attacks using Linux system firewall (Linux)

- Enterprise Hadoop cluster architecture - NFS installation (Server)

- Linux how to view the graphics models notebook (Linux)

- Linux system firewall defense network attacks (Linux)

- Java List add duplicate the same object (Programming)

- PULL operation mechanism parsing XML Comments (Programming)

- MySQL Tutorial: Building MySQL Cluster under Linux (Database)

  command-line tool for send e-mail
  Add Date : 2018-11-21      
  Today's article we will talk about some of the Linux command line to send e-mail with an attachment tool approach. It has many uses, such as on the server where the application that uses e-mail to send a document over, or you can use these commands to do some automation in the script. In the example in this article, we will use foo.tar.gz file as an attachment.

There are different command line tool can send mail, here I share the majority of users will use several tools, such as mailx, mutt and swaks.

We're presenting these tools are very famous, and is present in most Linux distributions default software repository, you can use the following command to install:

In Debian / Ubuntu systems

apt-get install mutt
apt-get install swaks
apt-get install mailx
apt-get install sharutils
In Red Hat-based systems such as CentOS or Fedora

yum install mutt
yum install swaks
yum install mailx
yum install sharutils

1) using the mail / mailx

mailx tool in most Linux distributions is the default mail program, and now has the support sending attachments. If it's not on your system, you can use the top command to install. One thing to note, older versions of mailx may not support sending attachments, run the following command to see if supported.

$ Man mail
The first line looks like this:

mailx [-BDdEFintv ~] [- s subject] [- a attachment] [- c cc-addr] [- b bcc-addr] [- r from-addr] [- h hops] [- A account] [- S variable [= value]] to-addr ...
If you see that it supports the -a option (-a file name, file as an attachment to the message) and -s option (-s topic, specify a subject), it is supported. You can use the following several examples of sending mail.

a) Simple Mail

Run the mail command, and then waits for you to enter mailx mail content. You can press Enter to change line. When the entry is complete, press Ctrl + D, mailx displays EOT indicates the end.

Mailx then automatically send the message to the recipient.

$ Mail user@example.com
How are you
b) Send an e-mail topic

$ Echo "Email text" | mail -s "Test Subject" user@example.com
-s usefulness is designated subject of the message.

c) to read the message from the file and send

$ Mail -s "message send from file" user@example.com < / path / to / file
d) acquired from the pipeline to the output of the echo command as mail data

$ Echo "This is message body" | mail -s "This is Subject" user@example.com
e) send messages with attachments

$ Echo "Bodywith attachment" | mail -a foo.tar.gz -s "attached file" user@example.com
-a option is used to specify the attachment.

2) mutt

Mutt is a text-based interface mail client on Unix-like systems. It has 20 years of history, in the history of Linux is a very important part, which is one of the first to support the process of scoring and multi-threaded processing client program. According to the following examples to send mail.

a) with the topic, read the body of the message from the file, and send

$ Mutt -s "Testing from mutt" user@example.com < /tmp/message.txt
b) Get echo command output as the message content sent through the pipeline

$ Echo "This is the body" | mutt -s "Testing mutt" user@example.com
c) send messages with attachments

$ Echo "This is the body" | mutt -s "Testing mutt" user@example.com -a /tmp/foo.tar.gz
d) send a message with multiple attachments

$ Echo "This is the body" | mutt -s "Testing" user@example.com -a foo.tar.gz -a bar.tar.gz

3) swaks

Swaks (Swiss Army Knife, Swiss Army knife) is a Swiss Army knife on the SMTP service, it is a powerful, flexible, programmable, SMTP transaction-oriented testing tools, developed and maintained by the John Jetmore. You can use the following syntax to send messages with attachments:

$ Swaks -t "foo@bar.com" - header "Subject: Subject" - body "Email Text" - attach foo.tar.gz
About Swaks an important place is that it will show you the entire process of sending e-mail, so if you want to debug mail sending process, it is a very useful tool.

It will give you all the details of the e-mail process, including the incoming mail server function supports every step of the interaction between the two servers send.

(LCTT Annotation: The original here less sharutils related presentations, and more than a presentation of uuencode.)

4) uuencode

Message transfer system was originally designed to transmit 7-bit encoding (similar ASCII) content. This means that is used to send text, but can not be made use 8-bit binary content (such as a program file or picture). uuencode ( "UNIX to UNIX encoding", the encoding method used between UNIX) program used to solve this limitation. Use uuencode, the sender will be converted into a binary format to transfer text format, and then converted back to the receiving end.

We can simply use uuencode and mailx mutt with or to send binary content, like this:

$ Uuencode example.jpeg example.jpeg | mail user@example.com

Shell Scripts: explains how to send mail

#! / Bin / bash
FROM = ""
TO = ""
BODY = ""
# Check the file name corresponding to the file exists
function check_files ()
output_files = ""
for file in $ 1
if [-s $ file]
output_files = "$ {output_files} $ {file}"
echo $ output_files
echo "*********************"
echo "E-mail sending script."
echo "*********************"
# Read the e-mail address entered by the user
while [1]
if [! $ FROM]
echo -n -e "Enter the e-mail address you wish to send mail from: \ n [Enter]"
echo -n -e "The address you provided is not valid: \ n [Enter]"
read FROM
echo $ FROM | grep -E '.. ^ + @ + $'> / dev / null
if [$ -? eq 0]
# Read the user enters the recipient's address
while [1]
if [! $ TO]
echo -n -e "Enter the e-mail address you wish to send mail to: \ n [Enter]"
echo -n -e "The address you provided is not valid: \ n [Enter]"
read TO
echo $ TO | grep -E '.. ^ + @ + $'> / dev / null
if [$ -? eq 0]
# Read messages relating to user input
echo -n -e "Enter e-mail subject: \ n [Enter]"
if [ "$ SUBJECT" == ""]
echo "Proceeding without the subject ..."
# Read as an attachment file name
echo -e "Provide the list of attachments. Separate names by space.
If there are spaces in file name, quote file name with \ "."
read att
# Make sure the file names pointing to the real file
attachments = $ (check_files "$ att")
echo "Attachments: $ attachments"
for attachment in $ attachments
ATTACHMENTS = "$ ATTACHMENTS-a $ attachment"
# Read the full text of the message
echo "Enter message. To mark the end of message type ;; in new line."
read line
while [ "$ line"! = ";;"]
BODY = "$ BODY $ line \ n"
read line
SENDMAILCMD = "mutt -e \" set from = $ FROM \ "-s \" $ SUBJECT \ "\
$ ATTACHMENTS - \ "$ TO \" <<< \ "$ BODY \" "
mutt -e "set from = $ FROM" -s "$ SUBJECT" $ ATTACHMENTS - $ TO <<< $ BODY
** ** Script output

$ Bash send_mail.sh
E-mail sending script.
Enter the e-mail address you wish to send mail from:
[Enter] test@gmail.com
Enter the e-mail address you wish to send mail to:
[Enter] test@gmail.com
Enter e-mail subject:
[Enter] Message subject
Provide the list of attachments.Separate names by space.
If there are spaces in file name, quote file name with ".
Attachments: send_mail.sh
Enter message. To mark the end of message type ;; in new line.
This is a message
to sum up

There are many ways you can use the command-line / Shell script to send mail, here we only shared a Unix-like system in which four tools available. I hope you like our articles, and provide your comments and let us know what you'd like to learn new tools.
- A brief description of Java 8 new features introduced syntax (Programming)
- Linux group account deletion, and other related operations (Linux)
- Log in CentOS 6.5 Multi-user setting VNC (Server)
- CentOS install Java 1.8 (Linux)
- Use FirewallD build dynamic firewall (Linux)
- Linux Getting Started Tutorial: How to set up a static MAC address on VMware ESXi virtual machine (Mobile)
- Linux System Getting Started Learning: Disable HTTP forwarding wget in (Linux)
- Use Ganglia to monitor your cluster (Server)
- Linux System Getting Started Learning: modify environment variables in Linux PATH (Linux)
- Oracle 12C truncate table cascade (Database)
- Linux compiler of GCC (Linux)
- VirtualBox virtual machine to install Linux (Linux)
- It is time to upgrade your gulp 4.0 (Programming)
- 11 examples in Linux df command (Linux)
- Java NIO2: Buffer (Programming)
- Linux folder and extract the differential file comparison techniques -rsync Magical (Linux)
- Definition Format Oracle basis of various statements (Database)
- PXE installation of Linux servers (Server)
- CentOS 6.5 / 6.6 modify the default SSH port number (Linux)
- Sniffer install applications in Linux supplement (Linux)
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.