[Translated] 20150409 4 Tools Send Email with Subject, Body and Attachment in Linux.md

This commit is contained in:
Gore Liu 2015-05-20 14:38:40 +08:00
parent 0c302a953d
commit 2693e62ddf
2 changed files with 261 additions and 263 deletions

View File

@ -1,263 +0,0 @@
Translating by goreliu ...
4 Tools Send Email with Subject, Body and Attachment in Linux
================================================================================
In today's article we will cover a few ways you can use to send emails with attachments from the command line interface on Linux. It can have quite a few uses, for example to send an archive with an application from an application server to email or you can use the commands in scripts to automate some process. For our examples,we will use the file foo.tar.gz as our attachment.
There are various ways to send emails from command line using different mail clients but here I am sharing few mail client utility used by most users like mailx, mutt and swaks.
All the tools we will present to you are very popular and present in the repositories of most Linux distributions, you can install them using the following commands:
For **Debian / Ubuntu** systems
apt-get install mutt
apt-get install swaks
apt-get install mailx
apt-get install sharutils
For Red Hat based systems like **CentOS** or **Fedora**
yum install mutt
yum install swaks
yum install mailx
yum install sharutils
### 1) Using mail / mailx ###
The mailx utility found as the default mailer application in most Linux distributions now includes the support to attach file. If it is not available you can easily install using the following commands please take note that this may not be supported in older versions, to check this you can use the command:
$ man mail
And the first line should look 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 . . .
As you can see it supports the -a attribute to add a file to the email and -s attribute to subject to the email. Use few of below examples to send mails.
**a) Simple Mail**
Run the mail command, and then mailx would wait for you to enter the message of the email. You can hit enter for new lines. When done typing the message, press Ctrl+D and mailx would display EOT.
After than mailx automatically delivers the email to the destination.
$ mail user@example.com
HI,
Good Morning
How are you
EOT
**b) To send email with subject**
$ echo "Email text" | mail -s "Test Subject" user@example.com
-s is used for defining subject for email.
**c) To send message from a file**
$ mail -s "message send from file" user@example.com < /path/to/file
**d) To send message piped using the echo command**
$ echo "This is message body" | mail -s "This is Subject" user@example.com
**e) To send email with attachment**
$ echo “Body with attachment "| mail -a foo.tar.gz -s "attached file" user@example.com
-a is used for attachments
### 2) mutt ###
Mutt is a text-based email client for Unix-like systems. It was developed over 20 years ago and it's an important part of Linux history, one of the first clients to support scoring and threading capabilities. Use few of below examples to send email.
**a) Send email with subject & body message from a file**
$ mutt -s "Testing from mutt" user@example.com < /tmp/message.txt
**b) To send body message piped using the echo command**
$ echo "This is the body" | mutt -s "Testing mutt" user@example.com
**c) To send email with attachment**
$ echo "This is the body" | mutt -s "Testing mutt" user@example.com -a /tmp/foo.tar.gz
**d) To send email 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 stands for Swiss Army Knife for SMTP and it is a featureful, flexible, scriptable, transaction-oriented SMTP test tool written and maintained by John Jetmore. You can use the following syntax to send an email with attachment:
$ swaks -t "foo@bar.com" --header "Subject: Subject" --body "Email Text" --attach foo.tar.gz
The important thing about Swaks is that it will also debug the full mail transaction for you, so it is a very useful tool if you also wish to debug the mail sending process:
As you can see it gives you full details about the sending process including what capabilities the receiving mail server supports, each step of the transaction between the 2 servers.
### 4) uuencode ###
Email transport systems were originally designed to transmit characters with a seven-bit encoding -- like ASCII. This meant they could send messages with plain text but not "binary" text, such as program files or image files that used all of an eight-bit byte. The program is used to solve this limitation is “uuencode”( "UNIX to UNIX encoding") which encode the mail from binary format to text format that is safe to transmit & program is used to decode the data is called “uudecode”
We can easily send binary text such as a program files or image files using uuencode with mailx or mutt email client is shown by following example:
$ uuencode example.jpeg example.jpeg | mail user@example.com
### Shell Script : Explain how to send email ###
#!/bin/bash
FROM=""
SUBJECT=""
ATTACHMENTS=""
TO=""
BODY=""
# Function to check if entered file names are really files
function check_files()
{
output_files=""
for file in $1
do
if [ -s $file ]
then
output_files="${output_files}${file} "
fi
done
echo $output_files
}
echo "*********************"
echo "E-mail sending script."
echo "*********************"
echo
# Getting the From address from user
while [ 1 ]
do
if [ ! $FROM ]
then
echo -n -e "Enter the e-mail address you wish to send mail from:\n[Enter] "
else
echo -n -e "The address you provided is not valid:\n[Enter] "
fi
read FROM
echo $FROM | grep -E '^.+@.+$' > /dev/null
if [ $? -eq 0 ]
then
break
fi
done
echo
# Getting the To address from user
while [ 1 ]
do
if [ ! $TO ]
then
echo -n -e "Enter the e-mail address you wish to send mail to:\n[Enter] "
else
echo -n -e "The address you provided is not valid:\n[Enter] "
fi
read TO
echo $TO | grep -E '^.+@.+$' > /dev/null
if [ $? -eq 0 ]
then
break
fi
done
echo
# Getting the Subject from user
echo -n -e "Enter e-mail subject:\n[Enter] "
read SUBJECT
echo
if [ "$SUBJECT" == "" ]
then
echo "Proceeding without the subject..."
fi
# Getting the file names to attach
echo -e "Provide the list of attachments. Separate names by space.
If there are spaces in file name, quote file name with \"."
read att
echo
# Making sure file names are poiting to real files
attachments=$(check_files "$att")
echo "Attachments: $attachments"
for attachment in $attachments
do
ATTACHMENTS="$ATTACHMENTS-a $attachment "
done
echo
# Composing body of the message
echo "Enter message. To mark the end of message type ;; in new line."
read line
while [ "$line" != ";;" ]
do
BODY="$BODY$line\n"
read line
done
SENDMAILCMD="mutt -e \"set from=$FROM\" -s \"$SUBJECT\" \
$ATTACHMENTS -- \"$TO\" <<< \"$BODY\""
echo $SENDMAILCMD
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 ".
send_mail.sh
Attachments: send_mail.sh
Enter message. To mark the end of message type ;; in new line.
This is a message
text
;;
### Conclusion ###
There are many ways of send emails from command line / shell script but here we have shared 4 tools available for unix / linux based distros. Hope you enjoyed reading our article and please provide your valuable comments and also let us know if you know about any new tools.
--------------------------------------------------------------------------------
via: http://linoxide.com/linux-shell-script/send-email-subject-body-attachment-linux/
作者:[Bobbin Zachariah][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[a]:http://linoxide.com/author/bobbin/

View File

@ -0,0 +1,261 @@
4个可以发送带主题、正文和附件的电子邮件的工具
================================================================================
今天的文章里我们会讲到一些使用Linux命令行工具来发送带附件的电子邮件的方法。它有很多用处比如在应用程序所在服务器上使用电子邮件发送一个档案过来或者你可以在脚本中使用这些命令来做一些自动化操作。在本文的例子中我们会使用foo.tar.gz文件作为附件。
有不同的命令行工具可以发送邮件,这里我分享几个多数用户会使用的工具,如`mailx`、`mutt`和`swaks`。
我们即将呈现的多数工具都是非常有名的并且存在于多数Linux发行版默认的软件仓库中你可以使用如下命令安装
**Debian / Ubuntu** 系统
apt-get install mutt
apt-get install swaks
apt-get install mailx
apt-get install sharutils
在基于Red Hat的系统**CentOS** 或者 **Fedora**
yum install mutt
yum install swaks
yum install mailx
yum install sharutils
### 1) 使用 mail / mailx ###
`mailx`工具在多数Linux发行版中是默认的邮件程序现在已经支持发送附件了。如果它不在你的系统中你可以使用上边的命令安装。有一点需要注意老版本的mailx可能不支持发送附件运行如下命令查看是否支持。
$ man mail
第一行看起来是这样的:
mailx [-BDdEFintv~] [-s subject] [-a attachment ] [-c cc-addr] [-b bcc-addr] [-r from-addr] [-h hops] [-A account] [-S variable[=value]] to-addr . . .
如果你看到它支持`-a`的选项(-a 文件名,将文件作为附件添加到邮件)和`-s`选项(-s 主题,指定邮件的主题),就是支持的。可以使用如下的几个例子发送邮件。
**a) 简单的邮件 **
运行`mail`命令,然后`mailx`会等待你输入邮件内容。你可以按回车来换行。当输入完成后按Ctrl + D`mailx`会显示EOT。
然后`mailx`会自动将邮件发送给收件人。
$ mail user@example.com
HI,
Good Morning
How are you
EOT
**b) 发送有主题的邮件 **
$ echo "Email text" | mail -s "Test Subject" user@example.com
`-s`的用处是指定邮件的主题。
**c) 从文件中读取邮件内容并发送 **
$ mail -s "message send from file" user@example.com < /path/to/file
**d) 将从管道获取到的`echo`命令输出作为邮件内容发送 **
$ echo "This is message body" | mail -s "This is Subject" user@example.com
**e) 发送带附件的邮件 **
$ echo “Body with attachment "| mail -a foo.tar.gz -s "attached file" user@example.com
`-a`选项用于指定附件。
### 2) mutt ###
Mutt是类Unix系统上的一个文本界面邮件客户端。它有20多年的历史在Linux历史中也是一个很重要的部分它是最早支持进程打分和多线程处理的程序之一。按照如下的例子来发送邮件。
**a) 从文件中读取邮件的主题和正文,并发送 **
$ mutt -s "Testing from mutt" user@example.com < /tmp/message.txt
**b) 将从管道获取到的`echo`命令输出作为邮件内容发送 **
$ echo "This is the body" | mutt -s "Testing mutt" user@example.com
**c) 发送带附件的邮件 **
$ echo "This is the body" | mutt -s "Testing mutt" user@example.com -a /tmp/foo.tar.gz
**d) 发送带有多个附件的邮件 **
$ echo "This is the body" | mutt -s "Testing" user@example.com -a foo.tar.gz a bar.tar.gz
### 3) swaks ###
SwaksSwiss Army Knife瑞士军刀是SMTP的瑞士军刀它功能强大、灵活、可编程、面向事务的SMTP测试工具由John Jetmore开发和维护。你可以使用如下语法发送带附件的邮件
$ swaks -t "foo@bar.com" --header "Subject: Subject" --body "Email Text" --attach foo.tar.gz
关于Swaks一个重要的事情是它会为你显示整个邮件发送过程所以如果你想调试邮件发送过程它是一个非常有用的工具
正如你看到的那样,它给你提供了邮件发送过程的所有细节,包括邮件接收服务器的功能支持、两个服务器之间的每一步交互。
### 4) uuencode ###
邮件传输系统最初是被设计来传送7位编码类似ASCII的内容的。这就意味这它是用来发送文本内容而不能发会使用8个位的二进制内容如程序文件或者图片。`uuencode`“UNIX to UNIX encoding”UNIX之间使用的编码方式程序用来解决这个限制。使用`uuencode`,发送端将二进制格式的转换成文本格式来传输,接收端再转换回去。
我们可以简单地使用`uuencode`和`mailx`或者`mutt`配合,来发送二进制内容,类似这样:
$ uuencode example.jpeg example.jpeg | mail user@example.com
### Shell脚本解释如何发送邮件 ###
#!/bin/bash
FROM=""
SUBJECT=""
ATTACHMENTS=""
TO=""
BODY=""
# 检查文件名对应的文件是否存在
function check_files()
{
output_files=""
for file in $1
do
if [ -s $file ]
then
output_files="${output_files}${file} "
fi
done
echo $output_files
}
echo "*********************"
echo "E-mail sending script."
echo "*********************"
echo
# 读取用户输入的邮件地址
while [ 1 ]
do
if [ ! $FROM ]
then
echo -n -e "Enter the e-mail address you wish to send mail from:\n[Enter] "
else
echo -n -e "The address you provided is not valid:\n[Enter] "
fi
read FROM
echo $FROM | grep -E '^.+@.+$' > /dev/null
if [ $? -eq 0 ]
then
break
fi
done
echo
# 读取用户输入的收件人地址
while [ 1 ]
do
if [ ! $TO ]
then
echo -n -e "Enter the e-mail address you wish to send mail to:\n[Enter] "
else
echo -n -e "The address you provided is not valid:\n[Enter] "
fi
read TO
echo $TO | grep -E '^.+@.+$' > /dev/null
if [ $? -eq 0 ]
then
break
fi
done
echo
# 读取用户输入的邮件主题
echo -n -e "Enter e-mail subject:\n[Enter] "
read SUBJECT
echo
if [ "$SUBJECT" == "" ]
then
echo "Proceeding without the subject..."
fi
# 读取作为附件的文件名
echo -e "Provide the list of attachments. Separate names by space.
If there are spaces in file name, quote file name with \"."
read att
echo
# 确保文件名指向真实文件
attachments=$(check_files "$att")
echo "Attachments: $attachments"
for attachment in $attachments
do
ATTACHMENTS="$ATTACHMENTS-a $attachment "
done
echo
# 读取完整的邮件正文
echo "Enter message. To mark the end of message type ;; in new line."
read line
while [ "$line" != ";;" ]
do
BODY="$BODY$line\n"
read line
done
SENDMAILCMD="mutt -e \"set from=$FROM\" -s \"$SUBJECT\" \
$ATTACHMENTS -- \"$TO\" <<< \"$BODY\""
echo $SENDMAILCMD
mutt -e "set from=$FROM" -s "$SUBJECT" $ATTACHMENTS -- $TO <<< $BODY
** 脚本输出 **
$ 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 ".
send_mail.sh
Attachments: send_mail.sh
Enter message. To mark the end of message type ;; in new line.
This is a message
text
;;
### 总结 ###
有很多方法可以使用命令行/Shell脚本来发送邮件我们只分享了其中4个类Unix系统可用的工具。希望你喜欢我们的文章并且提供您的宝贵意见让我们知道您想了解哪些新工具。
--------------------------------------------------------------------------------
via: http://linoxide.com/linux-shell-script/send-email-subject-body-attachment-linux/
作者:[Bobbin Zachariah][a]
译者:[goreliu](https://github.com/goreliu)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[a]:http://linoxide.com/author/bobbin/