简体   繁体   English

用于发送带附件的电子邮件的 GMAIL API

[英]GMAIL API for sending Email with attachment

i' m working on a javascript client able to read a CSV which contains an image url list.我正在开发一个能够读取包含图像 url 列表的 CSV 的 javascript 客户端。

I m able to read the csv by the means of jquery-csv and to draw each image in a html5 canvas.我能够通过 jquery-csv 读取 csv 并在 html5 画布中绘制每个图像。

The next step is to apply to each image a text layer and to send the image by email using gmail api.下一步是为每个图像应用一个文本层,并使用 gmail api 通过电子邮件发送图像。

So my diffifulty is to find an example showing me how to take a canvas and to attach it to an email using only javascript.所以我的困难是找到一个例子,向我展示如何仅使用 javascript 获取画布并将其附加到电子邮件中。

Do have i to build a json according to the multipart gmail guidelines and to send it as POST body as specified?我是否必须根据 multipart gmail 指南构建一个 json 并将其作为指定的 POST 正文发送?

Can you send me some example?你能给我发一些例子吗?

// Get the canvas from the DOM and turn it into base64-encoded png data.
var canvas = document.getElementById("canvas");
var dataUrl = canvas.toDataURL();

// The relevant data is after 'base64,'.
var pngData = dataUrl.split('base64,')[1];

// Put the data in a regular multipart message with some text.
var mail = [
  'Content-Type: multipart/mixed; boundary="foo_bar_baz"\r\n',
  'MIME-Version: 1.0\r\n',
  'From: sender@gmail.com\r\n',
  'To: receiver@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/png\r\n',
  'MIME-Version: 1.0\r\n',
  'Content-Transfer-Encoding: base64\r\n',
  'Content-Disposition: attachment; filename="example.png"\r\n\r\n',

   pngData, '\r\n\r\n',

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

// Send the mail!
$.ajax({
  type: "POST",
  url: "https://www.googleapis.com/upload/gmail/v1/users/me/messages/send?uploadType=multipart",
  contentType: "message/rfc822",
  beforeSend: function(xhr, settings) {
    xhr.setRequestHeader('Authorization','Bearer {ACCESS_TOKEN}');
  },
  data: mail
}); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM