通过sh,curl,mailgun

时间:2016-04-11 22:11:24

标签: bash curl

我有这个send_update.sh文件,我想用它来发送电子邮件

我下面的内容很好。

但是,我想将文本更改为文件update.txt的内容

我尝试制作变量var=cat update.txt-F text=$var,但它只是给了我一个错误:

"message": "Need at least one of 'text' or 'html' parameters specified"

#!/bin/sh
curl -s --user 'api:key-mykey' \
    http:mail-gun-api
    -F from=...
    -F to=...
    -F subject='Hello My name is here' \
    -F text='First email!'
遗憾的是,我不能只使用我常用的sendmail,因为谷歌计算引擎不允许它。

2 个答案:

答案 0 :(得分:5)

对于bash脚本,要将变量设置为可以使用的命令输出:

var=$(cat /full/path/to/file/update.txt)

如果这样做,update.txt文件的内容现在已分配给变量$var。您可能希望完全避免使用$var变量,并执行以下操作:

-F text=$(cat /full/path/to/file/update.txt)

如果文件位于您当前的工作目录中,您可能不需要包含update.txt的完整路径。

答案 1 :(得分:2)

curl在内部支持此行为。

引自man curl(卷曲版本7.48.0):

  -F, --form <name=content>
         (HTTP)  This  lets  curl  emulate a filled-in form in which a user has pressed the submit button. This causes curl to POST data using the Content-Type multipart/form-data according to RFC 2388.
         This enables uploading of binary files etc. To force the 'content' part to be a file, prefix the file name with an @ sign. To just get the content part from a file, prefix the  file  name  with
         the  symbol <. The difference between @ and < is then that @ makes a file get attached in the post as a file upload, while the < makes a text field and just get the contents for that text field
         from a file.

         Example, to send your password file to the server, where 'password' is the name of the form-field to which /etc/passwd will be the input:

         curl -F password=@/etc/passwd www.mypasswords.com

         To read content from stdin instead of a file, use - as the filename. This goes for both @ and < constructs. Unfortunately it does not support reading the file from a named pipe or  similar,  as
         it needs the full size before the transfer starts.

         You can also tell curl what Content-Type to use by using 'type=', in a manner similar to:

         curl -F "web=@index.html;type=text/html" url.com

         or

         curl -F "name=daniel;type=text/foo" url.com

         You can also explicitly change the name field of a file upload part by setting filename=, like this:

         curl -F "file=@localfile;filename=nameinpost" url.com

         If filename/path contains ',' or ';', it must be quoted by double-quotes like:

         curl -F "file=@\"localfile\";filename=\"nameinpost\"" url.com

         or

         curl -F 'file=@"localfile";filename="nameinpost"' url.com

         Note that if a filename/path is quoted by double-quotes, any double-quote or backslash within the filename must be escaped by backslash.

         See further examples and details in the MANUAL.

         This option can be used multiple times.

编辑:

OP使用-F text="</path/to/update.txt"解决了它,如评论中所述。

相关问题