简体   繁体   English

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

[英]Sending email using unix shell scripting

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="root@sped56.lss.emc.com"
receiver="root@sped56.lss.emc.com"
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 

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 $subj and not $body or $receiver . 现在的问题是:为什么在$subj周围$subj双引号而不是$body$receiver The answer is that echo doesn't care about the number of arguments. 答案是echo不关心参数的数量。 So if $body expands to several words, echo will just print all of them with a single space in between. 因此,如果$body扩展为多个单词, echo将打印所有这些单词之间的单个空格。 Here, the quotes would only matter if you wanted to preserve double spaces. 在这里,引号只有在你想要保留双倍空格时才有意义。

As for $receiver , this works because it expands only to a single word (no spaces). 至于$receiver ,这是有效的,因为它只扩展为一个单词(没有空格)。 It would break for mail addresses like John Doe <doe@none.com> . 对于像John Doe <doe@none.com>这样的邮件地址,它会中断。

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. 我最喜欢的方式提供了更大的灵活性,并且在我使用UNIX之后的任何UNIX / POSIX环境中都有用。 Only the sendmail path may change to meet the local implementation. 只有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. 在许多环境中,以“ From ”开头,然后是非空格的行被保留为消息包中的内部“开始新消息”标记。 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. 虽然在任何系统上具有perl替换sed的最大可移植性: perl -p -e 's/^From ([^ ])/From $1/' as sed在某些UNIX系统中并不是我真正喜欢的,特别是如果echo "From me"获得第三个空间,当你用管道进入当地的sed。
  • A line consisting of just a period is the classic end-of-message marker to the sendmail family of agents. 仅包含句点的行是sendmail系列代理的经典消息结束标记。 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-addr@example.com>
  • 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. 除了sendmail的-f选项之外,所有需要引号,转义等的文本都隐藏在“here here”中,这突然间不那么麻烦了。
  • Read the man page on the sendmail "-" options used here. 阅读此处使用的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
. . .

你可以使用mailx并始终在变量周围加上引号

mailx -s "$subj" my.email@my.domain.com < myfile.txt

mailx will not present in many of the environments. mailx不会出现在许多环境中。 but you can have the same functionality using mail/sendmail 但您可以使用mail / sendmail获得相同的功能

cat myfile.txt | mail -s " Hi My mail goes here " mailid@domain.com 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM