【发布时间】:2015-02-12 13:50:28
【问题描述】:
我有 this 小 bash 脚本(sendmail.sh)使用 mandril 发送电子邮件,当像这样使用 ./sendmail.sh "my@email.com" "Email Subject" "Email body" 时效果非常好。感谢black @ LET 但是我希望这个脚本从 echo 命令中获取它的电子邮件正文,就像 linux 邮件命令一样。 echo "email body" | mail -s "email subject" email@any.com
当我将以下脚本与此命令 echo "email body" |./sendmail.sh "my@email.com" "Email Subject" 一起使用时,它会打印 else 块中指定的错误(因为只给出了 2 个参数,但需要 3 个)
/sendmail.sh requires 3 arguments - to address, subject, content
Example: ././sendmail.sh "to-address@mail-address.com" "test" "hello this is a test message"
很惊讶为什么echo 命令的输出没有被用作脚本中 $3 参数的输入。
#!/bin/bash
#created by black @ LET
#MIT license, please give credit if you use this for your own projects
#depends on curl
key="" #your maildrill API key
from_email="" #who is sending the email
reply_to="$from_email" #reply email address
from_name="curl sender" #from name
if [ $# -eq 3 ]; then
msg='{ "async": false, "key": "'$key'", "message": { "from_email": "'$from_email'", "from_name": "'$from_name'", "headers": { "Reply-To": "'$reply_to'" }, "return_path_domain": null, "subject": "'$2'", "text": "'$3'", "to": [ { "email": "'$1'", "type": "to" } ] } }'
results=$(curl -A 'Mandrill-Curl/1.0' -d "$msg" 'https://mandrillapp.com/api/1.0/messages/send.json' -s 2>&1);
echo "$results" | grep "sent" -q;
if [ $? -ne 0 ]; then
echo "An error occured: $results";
exit 2;
fi
else
echo "$0 requires 3 arguments - to address, subject, content";
echo "Example: ./$0 \"to-address@mail-address.com\" \"test\" \"hello this is a test message\""
exit 1;
fi
【问题讨论】:
标签: linux bash email mandrill getopt