【问题标题】:sending email with google app engine from remote host, how do I attache a file?从远程主机使用谷歌应用引擎发送电子邮件,如何附加文件?
【发布时间】:2013-11-15 09:26:16
【问题描述】:

我正在使用谷歌应用引擎发送基于https://github.com/godsid/appEngineSendMail的电子邮件

所以我从远程主机发出 curl post 请求,它可以工作。

但是如何将文件附加到应用引擎?

这是我的 curl post_fields

$fields = array(
                'to' => urlencode($to),
                'sender' => urlencode($from),
                'body' => urlencode($body),
                'subject' => urlencode($subject),
                'key' => urlencode('****')
                );

$fields['attachments']= array("@". $_FILES['attachment']['tmp_name']);

我已经搜索了 2 天的答案...感谢任何帮助或提示

【问题讨论】:

    标签: php python google-app-engine curl


    【解决方案1】:

    问题是我正在对字段数组进行 url_encoding,这样做会阻止 cURL 实际注意到 @filename 并将文件附加到请求中。

    所以我不得不将请求分成多个部分

    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
    

    并将字段附加到请求而不对其进行编码

    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    

    我在 python 方面的错误是使用 self.request.get(),这是 github 上的脚本使用的,我基于的那个

    所以我不得不像这样使用self.request.post[]

    attachment = self.request.POST["attachment"] #actual attachment file
    attachment_name = self.request.POST["attachment_name"] #string value of attahcment name sent from php side
    message.attachments = [(attachment_name, attachment.value)]
    

    *注意 self.request.get 是一个函数,self.request.post 是一个数组

    希望有一天这能帮助某人:)

    【讨论】:

      【解决方案2】:

      您可以轻松放入多个附件。对于每个附件,您需要输入文件名和文件数据。

      $fields['attachments'] = array(
          $_FILES['attachment']['name'] => file_get_contents($_FILES['attachment']['tmp_name'])
      );
      

      【讨论】:

      • 哦,我不知道我必须将文件内容放入帖子数据中,我认为 @-sign 神奇地解决了这个问题....但是我如何将它们添加到电子邮件中在python(谷歌引擎)方面?
      • 如果您向 yoururl.com/mail 提交一个带有有效 to 字段和主题的 POST 请求(您可以在 main.py 文件中更改发件人),那么它将发送电子邮件。一个简单的 POST 到 yoururl.com/(基本 url)应该会显示一些调试信息。
      猜你喜欢
      • 2012-06-13
      • 2012-08-18
      • 2011-08-16
      • 1970-01-01
      • 1970-01-01
      • 2011-07-23
      • 2017-06-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多