[英]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
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. 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 -p -e 's/^From ([^ ])/From $1/'
as sed在某些UNIX系统中并不是我真正喜欢的,特别是如果echo "From me"
获得第三个空间,当你用管道进入当地的sed。 "Long Name" <email-addr@example.com>
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.