【问题标题】:How sync a DRAFT email to a existing thread in Gmail having attachment over 20MB using Gmail API如何使用 Gmail API 将 DRAFT 电子邮件同步到 Gmail 中附件超过 20MB 的现有线程
【发布时间】:2018-10-30 14:19:59
【问题描述】:

我正在使用 MediaFileUpload 类创建一封电子邮件,其附件大小超过 20MB。我已经浏览了 Gmail API 上传附件文档。但以下异常一直出现。请帮我解决这个问题。

    $service = $this->getClientService();
    $client = $this->getClientConnection();

    $draft = new Google_Service_Gmail_Draft();
    $draft->setMessage($message);

    // mime message format
    $rawMessage = $this->getOriginalRawMessage();
    $client->setDefer(TRUE);
    try {
        $request = $service->users_drafts->create($userId, $draft,['uploadType' => 'multipart']);
        $media = new Google_Http_MediaFileUpload($client, $request, 'multipart/related',$rawMessage, false, self::MESSAGE_UPLOAD_CHUNK_SIZE);
        $media->setFileSize(strlen($rawMessage));
        // Upload the various chunks. $draftResponse will be false until the process is complete.
        $mediaResponse = false;
        while (!$mediaResponse) {
            $mediaResponse = $media->nextChunk();
        }
        $client->setDefer(false);

      return $mediaResponse;

获取异常

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "badContent",
    "message": "Media type 'application/json; charset=UTF-8' is not supported. Valid media types: [message/rfc822]"
   }
  ],
  "code": 400,
  "message": "Media type 'application/json; charset=UTF-8' is not supported. Valid media types: [message/rfc822]"
 }
}

【问题讨论】:

  • 您的标题 mime 类型错误

标签: gmail-api


【解决方案1】:
   $service = $this->getClientService();
    $googleClient = $this->getClientConnection();

    $draft = new Google_Service_Gmail_Draft();
    $draft->setMessage($message);
    try {
        // Original raw message (i.e rfc822 format),
        // Note: Content should not be in encoded format
        $mailMessage = $this->getOriginalMessage();

        // This flag is not to call the request, immediately, instead it queues, when flag is true
        $googleClient->setDefer(true);

        $request = $service->users_drafts->create('me', $draft);

        // create mediafile upload
        $media = new Google_Http_MediaFileUpload($googleClient, $request, 'message/rfc822', $mailMessage, true,
                self::MESSAGE_UPLOAD_CHUNK_SIZE);

        // Length of mail message(rfc822)
        $media->setFileSize(strlen($mailMessage)); 

        // Upload the various chunks. $status will be false until the process is complete.
        $draftObject = false;
        while (!$draftObject) {
            $draftObject = $media->nextChunk();
        }

        // Reset to the client to execute requests immediately in the future.
        $googleClient->setDefer(false);
        return $draftObject;

【讨论】:

    猜你喜欢
    • 2020-02-08
    • 1970-01-01
    • 1970-01-01
    • 2019-08-27
    • 2016-07-19
    • 1970-01-01
    • 2021-04-17
    • 2021-06-13
    • 2016-09-22
    相关资源
    最近更新 更多