bash 使用 unix shell 脚本发送电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1781989/
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-09-09 18:43:06  来源:igfitidea点击:

Sending email using unix shell scripting

bashemailshellunixscripting

提问by kk.

I have to write a script to send mails using unix shell scripts.

我必须编写一个脚本来使用 unix shell 脚本发送邮件。

The following script allows me to have variable message body.

以下脚本允许我拥有可变的消息正文。

Is it possible to have a variable subject part in the code below?

是否可以在下面的代码中包含可变主题部分?

#!/bin/bash
# Sending mail to remote user

sender="[email protected]"
receiver="[email protected]"
body="THIS IS THE BODY"
subj="THIS IS THE SUBJECT."


echo $body | mail $receiver -s "THIS IS THE SUBJECT" // this works fine
echo $body | mail $receiver -s $subj // ERROR - sends one mail with only
//"THIS" as subject and generates another error mail for the other three words 

回答by Aaron Digulla

You forgot the quotes:

你忘记了引号:

echo $body | mail $receiver -s "$subj"

Note that you must use double quotes (otherwise, the variable won't be expanded).

请注意,您必须使用双引号(否则,变量将不会被扩展)。

Now the question is: Why double quotes around $subjand not $bodyor $receiver. The answer is that echodoesn't care about the number of arguments. So if $bodyexpands to several words, echowill just print all of them with a single space in between. Here, the quotes would only matter if you wanted to preserve double spaces.

现在的问题是:为什么双引号$subj而不是$bodyor $receiver。答案是echo不关心参数的数量。因此,如果$body扩展为几个单词,echo将只打印所有单词,中间留一个空格。在这里,引号仅在您想保留双空格时才重要。

As for $receiver, this works because it expands only to a single word (no spaces). It would break for mail addresses like John Doe <[email protected]>.

至于$receiver,这是有效的,因为它只扩展到一个单词(没有空格)。对于像John Doe <[email protected]>.

回答by Gilbert

Old question, I know, but likely ever popular. My favorite way provides more flexibility and has worked on any UNIX/POSIX environment I have used since the dawn of my UNIX use. Only the sendmail path may change to meet the local implementation.

老问题,我知道,但可能很流行。我最喜欢的方式提供了更多的灵活性,并且在我使用 UNIX 开始使用的任何 UNIX/POSIX 环境中都有效。只有 sendmail 路径可能会更改以满足本地实现。

sed <<"ENDMAIL" -e '/^From [^ ]/s/^From /From  /' -e 's/^\.$/. /' | /usr/sbin/sendmail -t -f "$sender"
To: $receiver, $receiver2, $receiver3
From: $sender
Subject: $subj
Any-Other-Standard-Optional-Headers: place headers in any order, including,
MIME-Version: 1.0
Content-Type: text/plain;
X-Mailer: any identity you want to give your E-mailing application
X-Your-Custom-Headers: X- headers can be your own private headers
x-Last-Header: a blank line MUST follow the last header

Your body text here

ENDMAIL
  • In many environments lines starting with "From " then a non-space, are reserved as an internal "start new message" marker in bundles of messages. Stick an extra space in to avoid truncating your messages. Though for maximum portability on any system with perl replace sed with: perl -p -e 's/^From ([^ ])/From $1/'as sed in some UNIX systems is not really up to what I like and especially if echo "From me"gets a third space when piped into your local sed.
  • A line consisting of just a period is the classic end-of-message marker to the sendmail family of agents. Change such lines to end in a space... looks the same but no longer triggers end-of-message truncations.
  • You can use full E-mail addresses, like:
    "Long Name" <[email protected]>
  • Except for sendmail's -f option all the text needing quotes, escapes, and the like, are buried in the "here document", which suddenly are much less bothersome.
  • Read the man page on the sendmail "-" options used here.
  • 在许多环境中,以“ From ”开头然后是非空格的行被保留为消息包中的内部“开始新消息”标记。插入一个额外的空间以避免截断您的消息。尽管为了在任何使用 perl 的系统上实现最大的可移植性,请将 sed 替换为:perl -p -e 's/^From ([^ ])/From $1/'如某些 UNIX 系统中的 sed 并不真正符合我的喜好,尤其是echo "From me"在通过管道传输到本地 sed 时获得第三个空间时。
  • 仅由句点组成的行是 sendmail 代理系列的典型消息结束标记。将这些行更改为以空格结尾...看起来相同但不再触发消息结束截断。
  • 您可以使用完整的电子邮件地址,例如:
    "Long Name" <[email protected]>
  • 除了 sendmail 的 -f 选项之外,所有需要引号、转义符等的文本都被埋在“here document”中,这突然变得不那么麻烦了。
  • 阅读此处使用的 sendmail“-”选项的手册页。

Get more elaborate and you can send attachments, different message encoding, different E-mail formats, multipart messagesm and more. Do some research on this and beware the number of hyphens needed in different parts of the message.

获得更详细的信息,您可以发送附件、不同的消息编码、不同的电子邮件格式、多部分消息等等。对此进行一些研究,并注意消息不同部分所需的连字符数量。

Content-Type: multipart/mixed;
  boundary="----Some-unique-char-string-not-in-any-message"

------Some-unique-char-string-not-in-any-message
. . .

回答by ghostdog74

you can use mailx and always put your quotes around variables

您可以使用 mailx 并始终将引号放在变量周围

mailx -s "$subj" [email protected] < myfile.txt

回答by Balualways

mailx will not present in many of the environments. but you can have the same functionality using mail/sendmail

mailx 不会出现在许多环境中。但是您可以使用 mail/sendmail 获得相同的功能

cat myfile.txt | mail -s " Hi My mail goes here " [email protected]