Linux 使用带有 uuencode 的“sendmail”发送邮件,并带有主题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8063084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 07:06:46  来源:igfitidea点击:

Send a mail using 'sendmail' with uuencode, and having a subject alongwith

linuxsendmailuuencode

提问by bilkulbekar

I am trying to send a mail with attachment using sendmail. The problem is I am unable to send a subject line along with this.

我正在尝试使用 sendmail 发送带有附件的邮件。问题是我无法同时发送主题行。

The following command line fires two mails instead of one - one with the "Subject : Report", and the other with the attachment:

以下命令行会触发两封邮件,而不是一封 - 一封带有“ Subject : Report”,另一封带有附件:

/usr/bin/gmime-uuencode "/tmp/url_by_ip.txt" "Report.txt" | echo "Subject: Report" | /usr/sbin/sendmail <sender> <receiver>

回答by Kristiono Setyadi

Try this:

尝试这个:

mail -s 'Report' [email protected] < tmp/url_by_ip.txt

mail -s 'Report' [email protected] < tmp/url_by_ip.txt

回答by Basile Starynkevitch

Perhaps you want some utilities to send MIMEmail? There is the vmime C++ library, many scripting languages (Python, Ruby, Perl, Ocaml, ...) have packages for MIME. There are also commands usable by shell scripts e.g. mpackand many many others.

也许您想要一些实用程序来发送MIME邮件?有vmime C++ 库,许多脚本语言(Python、Ruby、Perl、Ocaml 等)都有 MIME 包。shell 脚本也有可用的命令,例如mpack和许多其他命令。

回答by Shawn Chin

If you can use other commands, I'd suggest muttwhich can handle attachments quite easily:

如果您可以使用其他命令,我建议您可以mutt轻松处理附件:

mutt -a file_to_attach -s "your subject" [email protected] < /tmp/mail_content

If you're stuck with /usr/sbin/sendmailthen you have a lot more to do. sendmailhas no concept of attachments and treats email content as a flat US-ASCII text (see this answerfor details).

如果你被困住了,/usr/sbin/sendmail那么你还有很多事情要做。sendmail没有附件的概念并将电子邮件内容视为平面 US-ASCII 文本(有关详细信息,请参阅此答案)。

To send attachents with sendmailyou'll need to format your mail content as a MIME message. For some examples, see this forum thread on unix.com.

要发送附件,sendmail您需要将邮件内容格式化为 MIME 消息。有关一些示例,请参阅unix.com 上的此论坛主题



To get you on your way, here's a quick untestedexample using bash. For brevity, I've hardcoded the variables but you can quite easily convert the example to a script/function that takes the relevant vars as arguments.

为了让您继续前进,这里有一个使用 bash 的未经测试的快速示例。为简洁起见,我对变量进行了硬编码,但您可以很容易地将示例转换为将相关变量作为参数的脚本/函数。

#!/bin/bash
# --- user params ---
MAILFROM="[email protected]"
MAILTO="[email protected]"
SUBJECT="TPS Report"
BODY_FILE="/home/peter/coversheey.txt"  # file holding mail body
ATT_FILE="/tnp/url_by_ip.txt"   # file to attach
ATT_AS_FILE="Report.txt"   # name to attach as

# --- generated values ---
BOUNDARY="unique-boundary-$RANDOM"
BODY_MIMETYPE=$(file -ib $BODY_FILE | cut -d";" -f1)   # detect mime type
ATT_MIMETYPE=$(file -ib $ATT_FILE | cut -d";" -f1)     # detect mime type
ATT_ENCODED=$(base64 < $ATT_FILE)  # encode attachment

# --- generate MIME message and pipe to sendmail ---
cat <<EOF | /usr/sbin/sendmail $MAILTO
MIME-Version: 1.0
From: $MAILFROM
To: $MAILTO
Subject: $SUBJECT
Content-Type: multipart/mixed; boundary="$BOUNDARY"

--$BOUNDARY
Content-Type: $BODY_MIMETYPE
Content-Disposition: inline

$(cat $BODY_FILE)
--$BOUNDARY
Content-Type: $ATT_MIMETYPE; name="$ATT_AS_FILE"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="$ATT_AS_FILE"

$ATT_ENCODED
--$BOUNDARY
EOF

Of course, if you're happy to use a higher level scripting language (Python, Ruby, Perl, ...) then there will be exisiting modules that will already do the heavy lifting for you.

当然,如果您乐于使用更高级别的脚本语言(Python、Ruby、Perl 等),那么现有的模块将为您完成繁重的工作。

p.s. There's also the mpackutility which does the MIME conversion for you, but AFAIK it doesn't come by default on most *nix boxes.

ps 还有mpack实用程序可以为您进行 MIME 转换,但 AFAIK 在大多数 *nix 机器上默认情况下不会出现。

回答by user1025247

Create a file with your mail header like for example mail.txt:

创建一个包含邮件标题的文件,例如 mail.txt:

Subject: here is the subject

Here the script to send the email:

这里是发送电子邮件的脚本:

#!/bin/bash
cat mail.txt > mail_to_send
cat attachment | uuencode attachment >> mail_to_send
sendmail [email protected] < mail_to_send
rm mail_to_send