【问题标题】:Google App Engine - Mail() attachmentGoogle App Engine - Mail() 附件
【发布时间】:2018-08-19 01:32:41
【问题描述】:

我可以使用App Engine Mail API 成功发送电子邮件,但我找不到为什么附件没有附加到电子邮件中的问题。我的代码如下所示:

// Pull in the raw file data of the image file to attach it to the message.
$image_data = fopen('gs://pro-sitemaps-api/file.csv');

try {
    $message = new Message();
    $message->setSender('myemail@****.com');
    $message->addTo('myemail@****.com');
    $message->setSubject('Subject Google App Engine Test');
    $message->setTextBody('Test body');
    $message->addAttachment('file.csv', $image_data);
    $message->send();
    echo 'Mail Sent';
} catch (InvalidArgumentException $e) {
    echo 'There was an error'.$e;
}

我的文件托管在 Google Storage(bucket) 中。存储文件的文件类型为.csv

谁能告诉我我做错了什么?

编辑:
添加 "mode":"r" 给我这个错误:

$image_data = fopen('gs://pro-sitemaps-api/file.csv', 'r');

输出:

error: {
code: "500",
message: "An error occurred parsing (locally or remotely) the arguments to mail.Send().",
status: "UNKNOWN",
details: [ ]
}
}





编辑: 这有效,但这只发送顶行(标题)。不是所有的线路。尝试将其添加到数组中并将数组作为附件发送,但随后出现相同的错误。:

$fpmail = fopen('gs://pro-sitemaps-api/file.csv', 'r');
    //$attach = fread($fpmail);
    $attach = fgets($fpmail);
    print_r($attach);

    try {
        $message = new Message();
        $message->setSender('asim@redperformance.no');
        $message->addTo('asim@redperformance.no');
        $message->setSubject('Test');
        $message->setTextBody('Test nyeste');
        $message->addAttachment('file.csv', $attach);
        $message->send();
        echo 'Mail Sent';
    } catch (InvalidArgumentException $e) {
        echo $e;
    }

    fclose($fpmail);

【问题讨论】:

  • 尝试将您的 fopen 指令调用更改为读取模式:$image_data = fopen('gs://pro-sitemaps-api/file.csv', 'r');
  • @Mangu 没用。将“模式”更改为“r”会出现此错误“解析(本地或远程)mail.Send() 的参数时发生错误。”,以及为什么会否决?
  • 我没有投反对票,这个问题看起来很有趣。你之前有没有任何错误信息?如果是这样,请编辑您的问题以包含它们。
  • 没有附件,邮件成功并在我的收件箱中检索。但是通过附件我得到了上面的这个错误。 (编辑答案)
  • @Mangu 这是一个奇怪的错误,找不到其他面临同样问题的人

标签: google-app-engine google-cloud-platform google-cloud-storage google-app-engine-php


【解决方案1】:

我从来没有在 PHP 中编写过代码,但看起来您实际上并没有读取文件的内容。

在大多数编程语言中,像fopen 这样的调用只是获取文件内容的第一步,但实际上并没有获取内容。你可能需要这样的东西:

$f = fopen('gs://pro-sitemaps-api/file.csv', 'r');
$image_data = $f.read();

但您需要弄清楚如何在 PHP 中从文件中读取数据。

【讨论】:

  • 试图找出方法。尝试读取并添加数组中的内容并作为附件发送,但同样的错误。
猜你喜欢
  • 2014-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-22
  • 1970-01-01
  • 2015-02-25
  • 2012-04-26
  • 2012-12-11
相关资源
最近更新 更多