Linux 发送电子邮件的shell脚本

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

Shell script to send email

linuxemailshellscripting

提问by

I am on linux machine and I monitor a process usage. Most of the time I will be away from my system and I have access to internet on my device. So I planned to write a shell-script that can mail me the output of the process.

我在 linux 机器上,我监视进程使用情况。大部分时间我都会离开我的系统,我可以在我的设备上访问互联网。所以我计划编写一个 shell 脚本,可以将过程的输出邮寄给我。

Is it possible?

是否可以?

If so how to make a shell-script send me mail?

如果是这样,如何使 shell 脚本向我发送邮件?

Please provide a snippet to get started.

请提供一个片段以开始使用。

采纳答案by trojanfoe

Yes it works fine and is commonly used:

是的,它工作正常并且常用:

$ echo "hello world" | mail -s "a subject" [email protected]

回答by BlackBear

Basically there's a program to accomplish that, called "mail". The subject of the email can be specified with a -s and a list of address with -t. You can write the text on your own with the echo command:

基本上有一个程序可以实现这一点,称为“邮件”。电子邮件的主题可以用 -s 指定,地址列表用 -t 指定。您可以使用 echo 命令自行编写文本:

echo "This will go into the body of the mail." | mail -s "Hello world" [email protected]

or get it from other files too:

或者也从其他文件中获取:

mail -s "Hello world" [email protected] < /home/calvin/application.log

mail doesn't support the sending of attachments, but Mutt does:

mail 不支持发送附件,但 Mutt 支持:

echo "Sending an attachment." | mutt -a file.zip -s "attachment" [email protected]

Note that Mutt's much more complete than mail. You can find better explanation here

请注意,Mutt 比邮件更完整。你可以在这里找到更好的解释

PS: thanks to @slhck who pointed out that my previous answer was awful. ;)

PS:感谢@slhck 指出我之前的回答很糟糕。;)

回答by slhck

mail -s "Your Subject" [email protected] < /file/with/mail/content

(/file/with/mail/contentshould be a plaintext file, not a file attachment or an image, etc)

/file/with/mail/content应该是纯文本文件,而不是文件附件或图像等)

回答by Volker Stolz

Well, the easiest solution would of course be to pipe the output into mail:

好吧,最简单的解决方案当然是将输出通过管道传输到邮件中:

vs@lambda:~$ cat test.sh
sleep 3 && echo test | mail -s test your@address
vs@lambda:~$ nohup sh test.sh
nohup: ignoring input and appending output to `nohup.out'

I guess sh test.sh &will do just as fine normally.

我想sh test.sh &正常情况下会做得一样好。

回答by ajreal

top -b -n 1 | mail -s "any subject" [email protected]

回答by zcaudate

sendmailworks for me on the mac (10.6.8)

sendmail在 mac (10.6.8) 上对我有用

echo "Hello" | sendmail -f [email protected] [email protected]

回答by david

#!/bin/sh
#set -x
LANG=fr_FR

# ARG
FROM="[email protected]"
TO="[email protected]"
SUBJECT="test é"
MSG="BODY éé"
FILES="fic1.pdf fic2.pdf"

# http://fr.wikipedia.org/wiki/Multipurpose_Internet_Mail_Extensions
SUB_CHARSET=$(echo ${SUBJECT} | file -bi - | cut -d"=" -f2)
SUB_B64=$(echo ${SUBJECT} | uuencode --base64 - | tail -n+2 | head -n+1)

NB_FILES=$(echo ${FILES} | wc -w)
NB=0
cat <<EOF | /usr/sbin/sendmail -t
From: ${FROM}
To: ${TO}
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=frontier
Subject: =?${SUB_CHARSET}?B?${SUB_B64}?=

--frontier
Content-Type: $(echo ${MSG} | file -bi -)
Content-Transfer-Encoding: 7bit

${MSG}
$(test $NB_FILES -eq 0 && echo "--frontier--" || echo "--frontier")
$(for file in ${FILES} ; do
        let NB=${NB}+1
        FILE_NAME="$(basename $file)"
        echo "Content-Type: $(file -bi $file); name=\"${FILE_NAME}\""
        echo "Content-Transfer-Encoding: base64"
        echo "Content-Disposition: attachment; filename=\"${FILE_NAME}\""
        #echo ""
        uuencode --base64 ${file} ${FILE_NAME}
        test ${NB} -eq ${NB_FILES} && echo "--frontier--" || echo 
"--frontier"
done)
EOF