Gmail PHP API发送电子邮件
问题描述:
我想通过Gmail PHP API发送电子邮件(https://developers.google.com/gmail/api/v1/reference/users/messages/send)。一切似乎都在发送消息。我的代码是:Gmail PHP API发送电子邮件
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/[email protected]/messages/send: (400) Invalid value for ByteString:
任何想法,这里发生了什么?谢谢!
答
您设置为'raw'的电子邮件字符串需要为url安全 base64编码(略有不同的字母表)。
答
的问题,像埃里克说,是url安全基础64.您需要更改URL转换到更多的东西是这样的:
$message_object = new Google_Service_Gmail_Message();
$encoded_message = rtrim(strtr(base64_encode("PUT MESSAGE HERE"), '+/', '-_'), '=');
$message_object->setRaw($encoded_message);
同时一定要使用有效的mail mime,所以“PUT这里的信息“在现实中不起作用。你需要包含有效的标题,如主题等。有some various PHP libraries来做这些,或者如果你可以自己用纯文本。
请问您能否提供您发送完整原始讯息的例子?我很难发送电子邮件。 – 2014-11-01 09:06:16