请求收件人,主题和正文的Bash邮件脚本

时间:2017-07-17 12:52:58

标签: linux bash shell email

我在尝试创建自己的bash邮件脚本时遇到问题,每次都要求在控制台中输入收件人,主题和正文。然后发送它。任何人都可以帮忙吗?非常感谢!

P.S我正在使用mail命令

2 个答案:

答案 0 :(得分:1)

read -p "What is your subject ? " subj
read -p "What is your message? " mess
read -p "What is the recipient address? " add
grep -E '[[:alnum:]]+@[[:alnum:]]+(.[[:alnum:]]+){1,2}' <<< $add
if [[ "$?" == "0" ]
then
    echo "$mess" | mail -s "$subj" $add
else
    echo "ERROR - The recipient address is in the wrong format"
fi

在这里,我们读回主题,正文和收件人的回复,然后我们检查电子邮件地址的正确格式,如果收件人地址的格式正确,则使用变量发送邮件。如果没有,请显示错误消息。

答案 1 :(得分:0)

您将使用read命令获取用户输入。提示:使用-p选项。

不要忘记"quote" all your variables,特别是对于持有未知用户输入的变量。

相关问题