Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Linux \ command-line tool for send e-mail     - Ceph cluster disk is no workaround for the remaining space (Server)

- Vagrant Getting Start (Linux)

- Smooth upgrade to OpenSSH 6.1 Procedure (Linux)

- quotacheck command file can not be created aquota.user and aquota.group solutions (Linux)

- How to Install Cantata MPD 1.3.3 for Ubuntu and Derived Version Users (Linux)

- Cacti Linux-based system monitoring and alarm (Linux)

- Spark parquet merge metadata problem (Server)

- Linux kernel boot to retain large memory method summary (Linux)

- C # socket udp broadcast (Programming)

- Use JMS listener Oracle AQ, trigger the execution of Java programs in the database changes (Database)

- How to create a bootable USB disk to use MultiSystem on Ubuntu (Linux)

- Installation and Configuration Munin monitoring server on Linux (Server)

- Based on Python: OpenCV simple image manipulation (Programming)

- MySQL flip-flop (Database)

- 12 kinds of detection of CPU information on a Linux system tools (Linux)

- ORA-08102 errors (Database)

- Large computer network security policy Experience (Linux)

- Disable unnecessary services under Linux (Linux)

- To compile install and test Swift under Linux (Linux)

- CentOS 6.4 OpenNebula installation (Linux)

  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.
- Shorewall firewall settings under Ubuntu (Linux)
- Linux installed and tested the deployment of Kafka distributed cluster (Server)
- Linux module mechanism of (Programming)
- ogg Oracle to SQL Server 2005 to achieve synchronization (Database)
- Based LNMP + uWSGI deploy Django applications (Server)
- MySQL to NoSQL avatar (Database)
- Python context managers (Programming)
- Under CentOS 7 installation and deployment environment Ceph (Server)
- Android float ball and boot from the start (Programming)
- Effective Java - lazy initialization (Programming)
- Linux security concerns again (Linux)
- Android to determine whether the device to open WIFI, GPRS data connection (Programming)
- C ++ Object Model Comments (Programming)
- for (int a: i) the use of the Java programming (Programming)
- Debian SSD ext4 4K aligned (Linux)
- Recover accidentally deleted Nginx logs (Server)
- Oracle 12CIN-memory in table spaces (Database)
- Linux system ARP attack solution (Linux)
- Moosefs Distributed File System Configuration (Server)
- Linux Network Programming - signal blocking and shielding (block, unblock) (Programming)
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.