我正在尝试通过gmail api集成通过我的系统发送电子邮件,但收到错误消息,即Google_Service_Exception{“ error”:{“ errors”:[{“ domain”:“ global”,“ reason”:“ invalidArgument”,“ message”:“所需的收件人地址”}],“ code”:400,“ message”: “需要收件人地址”}}这是laravel中的代码:
$obj = new \Google_Service_Gmail_Message ();
$raw = "MIME-Version: 1.0\n\r
From: Muhammad Bilal <[email protected]>\n\r
To: Umer Sheikh <[email protected]>\n\r
Subject: SIMPLE TEXT EMAIL\n\r
Date: Thu, 5 Mar 2020 18:45:33 +0500\n\r
Message-ID: <[email protected]>\n\r
Content-Type: text/plain; charset=\"UTF-8\"\n\r
\n\r
NO BODY";
$raw = rtrim(strtr(base64_encode($raw), '+/', '-_'), '=');
$obj->setRaw($raw);
$obj->setId('1234');
$obj->setInternalDate(null);
$obj->setLabelIds(null);
$payload = new \Google_Service_Gmail_MessagePart ();
$body = new \Google_Service_Gmail_MessagePartBody ();
$body->setData('test');
$body->setSize('1');
$payload->setBody($body);
$obj->setPayload($payload);
$results = $service->users_messages->send($user, $obj);
您收到的错误消息是由于Content-Type
的类型应为message/rfc822
和not text/plain
:
如Users.messages: send
documentation中所述:
此方法支持/ upload URI,并接受具有以下特征的上载媒体:
最大文件大小: 35MB
可接受的媒体MIME类型:
Users.messages: send
因此,整个电子邮件应使用RFC 2822格式的base64url编码字符串。
参考
message/rfc822