使用Gmail API发送大型附件 [英] Send large attachment with Gmail API

查看:71
本文介绍了使用Gmail API发送大型附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Gmail API发送带有大于5MB的大附件的电子邮件,您需要按照此处的说明进行操作: https://developers.google.com/gmail/api/guides/uploads

According to Gmail API to send an email with large attachments bigger then 5MB you need to use the instructions here: https://developers.google.com/gmail/api/guides/uploads

API的细节不是很清楚,我尝试使用在这里找到的解释: Gmail API PHP客户端库-如何使用PHP客户端库发送大型附件?

The API is a not very clear about the details and I tried to use the explanation I found here: Gmail API PHP Client Library - How do you send large attachments using the PHP client library?

每次都会收到"实体太大"错误消息.

I get "Entity too large" error message every time.

有人可以使用Gmail API成功发送大于5MB的附件,并帮助我了解我做错了什么吗?

Someone can succeded to send an attachment bigger than 5MB with Gmail API, and help me understand what I'm doing wrong?

我的作曲家文件:

{
    "require": {
        "google/apiclient": "^2.0",
        "pear/mail_mime": "^1.10"
    }
}

阅读所有内容后,我的代码是这样的:

My code after reading everything is this:

<?php
set_time_limit(100);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require_once __DIR__ . '/vendor/autoload.php';
require_once 'client.php';

$googleClient = getClient();

$mailService = new Google_Service_Gmail($googleClient);
$message = new Google_Service_Gmail_Message;

$file = __DIR__ . '/pexels-photo.jpg';

$mime = new Mail_mime;
$mime->addTo('mymail@domain.com');
$mime->setTXTBody('');
$mime->setSubject('test');

$mailMessage = base64_encode($mime->getMessage());
$message->setRaw($mailMessage);

$request = $mailService->users_messages->send(
    'me',
    $message,
    array( 'uploadType' => 'resumable' )
);

$googleClient->setDefer(true);

$media = new Google_Http_MediaFileUpload(
    $googleClient,
    $request,
    'message/rfc822',
    $mailMessage,
    $resumable = true,
    $chunkSizeBytes = 1 * 1024 * 1024
);

$media->setFileSize(filesize($file));

$status = false;
$handle = fopen($file, 'rb');
while (! $status && ! feof($handle)) {
    $chunk = fread($handle, $chunkSizeBytes);
    $status = $media->nextChunk($chunk);
}

fclose($handle);

$googleClient->setDefer(false);

推荐答案

@MP就我而言,问题是我需要设置未编码的消息大小,而不是实际文件的大小.像这样:

@MP In my case, the problem is that I needed to set the unencoded message size, not the actual file. like this:

$media->setFileSize(strlen($message));

这篇关于使用Gmail API发送大型附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆