邮件附件错误的媒体类型Gmail API

Tho*_*lle 9 javascript email ajax gmail-api

我正在尝试使用通过Gmail API在Javascript客户端附加的jpeg文件发送消息.我到目前为止编写的代码如下:

$.ajax({
  type: "POST",
  url: "https://www.googleapis.com/upload/gmail/v1/users/me/messages/send?uploadType=multipart",
  headers: {
    'Authorization': 'Bearer ' + accessToken,
    'Content-Type': 'multipart/related; boundary="foo_bar_baz"'
  },
  data: data
});
Run Code Online (Sandbox Code Playgroud)

哪里data是一个字符串建立类似的例子在这里找到:

--foo_bar_baz
Content-Type: application/json; charset=UTF-8

{ 
  "raw": "RnJvbTogRW1pbCBUaG9saW4gPGVtdGhvbGluQGdtYWlsLmNvbT4KVG86IEV4YW1wbGUgTmFtZSA8ZW10aG9saW5AZ21haWwuY29tPgpTdWJqZWN0OiBzZHNkCgpzZHNk"
}

--foo_bar_baz
Content-Type: image/jpeg

data:image_jpeg;base64,_9j_4AAQSkZJRgABAQEAYABgAAD_2wBDAAIBAQIBAQICAgICAgIC…bHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3-Pn6_9oADAMBAAIRAxEAPwD-f-iiigD_2Q==

--foo_bar_baz--
Run Code Online (Sandbox Code Playgroud)

我得到的错误是Media type 'image/jpeg' is not supported. Valid media types: [message/rfc822]可以理解的,因为[message/rfc822]根据规范,媒体是唯一有效的MIME类型,但上面链接的示例另有说明.

我究竟做错了什么?如果有人能够对此有所了解,我将不胜感激!

Tho*_*lle 13

编辑

第一段代码适用于组合大小为几mb的附件.如果要使用允许的限制为35 mb,请检查答案末尾的编辑.


史蒂夫把我推向了正确的方向(整个邮件必须在"原始"参数中),我只是尝试了Python API并查看了由此生成的邮件.

没有附件的邮件

Content-Type: text/plain; charset="UTF-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
to: receiver@gmail.com
from: sender@gmail.com
subject: Subject Text

The actual message text goes here
Run Code Online (Sandbox Code Playgroud)

带附件的邮件

Content-Type: multipart/mixed; boundary="foo_bar_baz"
MIME-Version: 1.0
to: receiver@gmail.com
from: sender@gmail.com
subject: Subject Text

--foo_bar_baz
Content-Type: text/plain; charset="UTF-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

The actual message text goes here

--foo_bar_baz
Content-Type: image/jpeg
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="example.jpg"

{JPEG data}

--foo_bar_baz--
Run Code Online (Sandbox Code Playgroud)

所以我只是编写了我的代码,它运行得很好!

var reader = new FileReader();
reader.readAsDataURL(attachment);
reader.onloadend = function (e) {
  // The relevant base64-encoding comes after "base64,"
  var jpegData = e.target.result.split('base64,')[1];
  var mail = [
    'Content-Type: multipart/mixed; boundary="foo_bar_baz"\r\n',
    'MIME-Version: 1.0\r\n',
    'to: receiver@gmail.com\r\n',
    'from: sender@gmail.com\r\n',
    'subject: Subject Text\r\n\r\n',

    '--foo_bar_baz\r\n',
    'Content-Type: text/plain; charset="UTF-8"\r\n',
    'MIME-Version: 1.0\r\n',
    'Content-Transfer-Encoding: 7bit\r\n\r\n',

    'The actual message text goes here\r\n\r\n',

    '--foo_bar_baz\r\n',
    'Content-Type: image/jpeg\r\n',
    'MIME-Version: 1.0\r\n',
    'Content-Transfer-Encoding: base64\r\n',
    'Content-Disposition: attachment; filename="example.jpg"\r\n\r\n',

    jpegData, '\r\n\r\n',

    '--foo_bar_baz--'
  ].join('');

  // The Gmail API requires url safe Base64 
  // (replace '+' with '-', replace '/' with '_', remove trailing '=')
  mail = btoa(mail).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');

  $.ajax({
    type: "POST",
    url: "https://www.googleapis.com/gmail/v1/users/me/messages/send",
    headers: {
      'Authorization': 'Bearer ' + accessToken,
      'Content-Type': 'application/json'
    },
    data: JSON.stringify({
      raw: mail
    })
  });
}
Run Code Online (Sandbox Code Playgroud)

编辑

上面的代码有效,但需要进行一些更改才能使用35 mb的最大限制.

邮件带附件的标题下建立了一个邮件,更改后的ajax请求如下所示:

$.ajax({
  type: "POST",
  url: "https://www.googleapis.com/gmail/v1/users/me/messages/send?uploadType=multipart",
  headers: {
    'Authorization': 'Bearer ' + accessToken,
    'Content-Type': 'message/rfc822'
  },
  data: mail
}); 
Run Code Online (Sandbox Code Playgroud)

Content-Type现在message/rfc822,而不是application/json,网址已经得到了新的参数uploadType=multipart,而且最重要的邮件不再是Base64编码,但在提供rfc822-format.