1

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);
4

2 回答 2

1

我用base64_encode($message->toString());而不是

(new Swift_Mime_ContentEncoder_Base64ContentEncoder)->encodeString($msg->toString());
于 2020-03-21T11:31:54.913 回答
0

您使用的库方法执行 Base64 编码,并且您需要文档中所述的Base64URL编码字符串

于 2020-03-13T13:54:41.050 回答