1
#!/bin/bash
### script to send email with attachment ###
### Declare Email Subject

SUBJECT="TESTING EMAIL"

## Declare Reciever's Email Id

EMAIL="reciever@testemail.com"

## Declare CopyTo Email Id

COPYTO_EMAIL=copyto@testemail.com
destfile="/home/acer/text.txt"

## Removing output files of the script from previous run

rm -f out.mail

## Remove the message body and sent files used in previous versions

rm -f mailbody.txt
rm -f OUTPUTRESULT.CSV

## Create the mail body message in a text file
## Initialize the text file

cat  > mailbody.txt
echo "Hi User,\n" >> mailbody.txt
echo "The result file for the server - SERVERNAME - is attached with this email.\n" >> mailbody.txt
echo "\n\nRegards,\nAdmin" >>mailbody.txt

## Sending email using mail command

cat mailbody.txt > out.mail

# Copy the OUTPUT.CSV file generated to another file OUTPUT_RESULT.CSV

uuencode $destfile >> out.mail

# are in their respenter code hereective variables - $SUBJECT and $EMAIL.

mail -s "$SUBJECT" "$EMAIL" "$COPYTO_MAIL" < out.mail
echo "Email sent."

在此脚本中执行邮件命令后,终端会提示用户输入。当我按 ctrl+d 时,只显示发送的 msg 电子邮件。

它不应该要求用户输入。

我能做什么?

你能告诉我为什么它会提示用户输入吗?

4

2 回答 2

4

是什么让您认为是在终端提示用户输入的邮件命令之后?你真的追查过吗?( bash -x)

在我看来,阻碍你的是这条线:

cat  > mailbody.txt

它是阻塞的,因为在这种情况下,cat从标准输入读取到写入mailbody.txt,并且由于您没有将标准输入绑定到任何更具体的东西,它仍然从终端读取。

于 2013-10-20T09:15:10.450 回答
1

联机帮助页mail(1)在我的系统上声明以下内容:

发送邮件

To send a message to one or more people, mail can be invoked with argu-
ments which are the names of people to whom the mail will be sent.  You
are then expected to type in your message, followed by a control-D ('^D')
at the beginning of a line.  The section below, Replying to or
originating mail, describes some features of mail available to help you
compose your letter.

不知何故,您可能必须输入control-D

mail在没有用户交互的情况下,我在使用命令行或mutt从命令行发送电子邮件时也遇到了麻烦。我的解决方法是从命令行使用php 邮件功能。

像这样的东西(未经测试,只是从我的脑海中写的):

php -r "mail('user@example.com', 'my subject', 'my body message');"
于 2013-10-20T10:16:06.080 回答