编程语言
首页 > 编程语言> > Gmail PHP API发送电子邮件

Gmail PHP API发送电子邮件

作者:互联网

我正在尝试通过Gmail PHP API(https://developers.google.com/gmail/api/v1/reference/users/messages/send)发送电子邮件.一切似乎都达到了我发送信息的程度.我的代码是:

private function createMessage($email) {
    $message = new Google_Service_Gmail_Message();
     $message->setRaw(strtr(base64_encode($email), '+/=', '-_,'));      // $email is a raw email data string
    return $message;
}

public function sendMessage($userID, $email) {
    try {
        $msg = $this->createMessage($email);
        $this->service->users_messages->send($userID, $msg);
    } catch (Exception $e) {
        print 'An error occurred: ' . $e->getMessage();
    }
}

代码在破线:

$this-> service-> users_messages-> send($userID,$msg);

有错误:

An error occurred: Error calling POST https://www.googleapis.com/gmail/v1/users/myemailaddress@gmail.com/messages/send: (400) Invalid value for ByteString:

知道这里发生了什么吗?谢谢!

解决方法:

您设置为“原始”的电子邮件字符串需要是url safe base64编码(字母略有不同).

标签:php,gmail-api
来源: https://codeday.me/bug/20190722/1506192.html