我Swift_Message
用来构造消息并对消息Swift_Mime_ContentEncoder_Base64ContentEncoder
进行编码,然后Google_Service_Gmail_Message
我用->setRaw()
方法设置编码的消息。
我用这个发送邮件已经有一段时间了,它曾经工作得很好。从昨天开始它停止工作并且错误消息说
"error": {
"code": 400,
"message": "Invalid value at 'message.raw' (TYPE_BYTES), Base64 decoding failed for \"[base64 encoded message with CRLF after every 76th character]\"",
"errors": [
{
"message": "Invalid value at 'message.raw' (TYPE_BYTES), Base64 decoding failed for \"[base64 encoded message with CRLF after every 76th character]\"",
"reason": "invalid"
}
],
"status": "INVALID_ARGUMENT"
}
当我删除 CRLF 时它可以工作。有什么想法吗?
参考:https ://www.rfc-editor.org/rfc/rfc2822#section-2.1.1
我的代码
$msg = new Swift_Message();
$msg->setCharset('UTF-8')
->addTo(/*recipient*/)
->setSubject(/*sbject*/)
->addPart(/*text content*/, "text/plain")
->addPart(/*html content*/, "text/html");
$base64 = (new Swift_Mime_ContentEncoder_Base64ContentEncoder)->encodeString($msg->toString());
$base64_msg = rtrim(strtr($base64, '+/', '-_'), '=');
$mailer = $this->_getGmailService();// new Google_Service_Gmail(new Google_Client())
$message = new Google_Service_Gmail_Message();
$message->setRaw($base64_msg);
$message->setThreadId($threadId);
$mailer->users_messages->send('me', $message);