邮件附件错误的媒体类型 Gmail API [英] Mail attachment wrong media type Gmail API

查看:31
本文介绍了邮件附件错误的媒体类型 Gmail API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 Javascript 客户端中的 Gmail API 发送带有 jpeg 文件的邮件.目前我写的代码如下:

I'm trying to send a message with a jpeg file attached through the Gmail API in Javascript client side. The code I've written so far is as follows:

$.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
});

其中 data 是一个像示例一样构建的字符串 在这里找到:

Where data is a string built up like the example found here:

--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--

我得到的错误是 不支持媒体类型图像/jpeg".有效的媒体类型:[message/rfc822],这是可以理解的,因为 [message/rfc822] 是根据 规范,但上面链接的示例另有说明.

The error I get is Media type 'image/jpeg' is not supported. Valid media types: [message/rfc822], which is understandable since [message/rfc822] is the only valid MIME-type for the media according to the specification, but the example linked above states otherwise.

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

What am I doing wrong? It would be much appreciated if someone could shed some light on this!

推荐答案

EDIT

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

EDIT

This first piece of code works for attachments with a combined size of a few mb. If you want to use the allowed limit of 35 mb, check the edit at the end of the answer.

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

After Steve pushed me in the right direction (the entire mail has to be in the "raw"-parameter), I simply tried the Python API and looked at the mail generated by that.

不带附件的邮件

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

带附件的邮件

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--

所以我只是围绕这个写了我的代码,而且效果很好!

So I just wrote my code around this, and it worked great!

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"
',
    '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"

',

    jpegData, '

',

    '--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
    })
  });
}

<小时>

编辑

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


Edit

The code above works, but a few alterations are needed to use the max limit of 35 mb.

带有附件的邮件标题下的邮件为例,更改后的ajax请求如下所示:

With a mail built up as the example under the heading Mail with attachment, the altered ajax-request looks as follows:

$.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
}); 

Content-Type 现在是 message/rfc822 而不是 application/json,url 获得了一个新参数 uploadType=multipart,最重要的是邮件不再是 Base64 编码的,而是以 rfc822 格式提供的.

The Content-Type is now message/rfc822 instead of application/json, the url has gotten a new parameter uploadType=multipart, and most importantly the mail is no longer Base64 encoded, but supplied in the rfc822-format.

这篇关于邮件附件错误的媒体类型 Gmail API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆