2013-10-20 13 views
1
#!/bin/bash 
### script to send email with attachment ### 
### Declare Email Subject 

SUBJECT="TESTING EMAIL" 

## Declare Reciever's Email Id 

EMAIL="[email protected]" 

## Declare CopyTo Email Id 

[email protected] 
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只顯示郵件發送的味精。爲什麼在unix中執行郵件命令後提示輸入

它不應該要求用戶輸入。

我該怎麼辦?

你能告訴我爲什麼它會提示用戶輸入嗎?

+0

那麼它可能是值得嘗試但有一個點在郵件的末尾添加一行。就像「\ n」。 –

回答

4

是什麼讓你覺得它是在郵件命令之後終端提示用戶輸入的?你真的追蹤過它嗎? (bash -x

在我看來,有什麼阻止你的是這一行:

cat > mailbody.txt 

它的阻止,因爲在這種情況下,cat從標準輸入讀取寫入mailbody.txt,因爲你沒有約束標準輸入任何更具體的東西,它仍然從終端讀取。

+0

+1您點擊Ctrl-d後看到「發送電子郵件」的原因是,cat> mailbody.txt和mail -s ...之間的所有內容都沒有可見的輸出。 – chepner

1

聯機幫助頁郵件(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. 

不管怎樣,你必須或許進入控制d

我也有麻煩發送電子郵件與mailmutt從命令行沒有用戶交互。我的解決方法是使用命令行中的php mail函數。

像這樣的東西(未測試,只是從我的腦海中寫道):

php -r "mail('[email protected]', 'my subject', 'my body message');" 
相關問題