发送带有日志文件作为附件的电子邮

时间:2016-04-02 08:24:03

标签: shell hadoop oozie cloudera-cdh

我正在使用Hadoop(CDH 5.4.8)来处理非结构化数据,并且在成功处理之后,我想向相关团队发送邮件通知,并将日志文件作为附件发送。

CDH 5.4.8 Oozie不支持电子邮件操作中的附件功能。所以我想用shell脚本来做这件事。请让我知道最好的方法。

1 个答案:

答案 0 :(得分:0)

您可以通过将完整的邮件(标题和正文)传送到 sendmail ,轻松地从shell中发送电子邮件。这假定您正在执行此操作的主机已正确配置了邮件传输代理(例如 sendmail postfix )以发送电子邮件。

使用附件发送电子邮件的最简单方法是在邮件用户代理中创建一个简单的模板邮件(例如 Thunderbird ),并使用视图源将其内容复制为模板命令。修改该模板以满足您的需要,并将其放在shell脚本中。

以下是一个例子:

#!/bin/sh
cat <<\EOF |
To: Ramesh <ramesh@example.com>
From: Diomidis Spinellis <dds@aueb.gr>
Subject: Here are your Hadoop results
Message-ID: <5700BF28.2070500@aueb.gr>
Date: Sun, 3 Apr 2016 09:58:48 +0300
MIME-Version: 1.0
Content-Type: multipart/mixed;
 boundary="------------030303090406090809090501"

This is a multi-part message in MIME format.
--------------030303090406090809090501
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit

The attachment contains your Hadoop results.


--------------030303090406090809090501
Content-Type: application/octet-stream;
 name="data"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
 filename="data"

HviDNR105+2Tr0+0fsx3OyzNueQqPuAXl9IUrafOi7Y=
--------------030303090406090809090501--
EOF
sendmail ramesh@example.com

要使用实际数据配置固定消息,请使用生成它们的命令替换要修改的部分。 (请注意此文档EOF标记中缺少的反斜杠。)

#!/bin/sh
cat <<EOF |
To: Ramesh <ramesh@example.com>
From: Diomidis Spinellis <dds@aueb.gr>
Subject: Here are your Hadoop results
Message-ID: <5700BF28.2070500@aueb.gr>
Date: $(date -R)
MIME-Version: 1.0
Content-Type: multipart/mixed;
 boundary="------------030303090406090809090501"

This is a multi-part message in MIME format.
--------------030303090406090809090501
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit

The attachment contains your Hadoop results.


--------------030303090406090809090501
Content-Type: application/octet-stream;
 name="data"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
 filename="data"

$(base64 binary-data-file.dat)
--------------030303090406090809090501--
EOF
sendmail ramesh@example.com