发送电子邮件附件的GMAIL API [英] GMAIL API for sending Email with attachment

查看:973
本文介绍了发送电子邮件附件的GMAIL API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我可以通过jquery的方式读取csv -csv,并在html5画布中绘制每个图像。



下一步是将每个图像应用于文本图层,并通过电子邮件发送图像使用gmail api。



所以我的差异就是找到一个例子,告诉我如何画画,并把它附加到一个电子邮件只使用javascript。



是否按照multipart gmail指南建立一个json,并按照指定的方式将其作为POST正文发送?



你可以给我发一些例子?

解决方案

  //从DOM获取画布,并将其转换为base64编码的png数据。 
var canvas = document.getElementById(canvas);
var dataUrl = canvas.toDataURL();

//相关数据位于base64之后。
var pngData = dataUrl.split('base64,')[1];

//将数据放入带有一些文本的常规多部分消息中。
var mail = [
'Content-Type:multipart / mixed; boundary =foo_bar_baz\r\\\
',
'MIME-Version:1.0\r\\\
',
'From:sender@gmail.com\rn\\ ,
'To:receiver@gmail.com\rn\\,'
'主题:主题Text \\\\\\\',

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

'实际的消息文本在这里\r\\\
\r\\\
',

'--foo_bar_baz \r\\\
',
'Content-Type:image / png\rr/\
',
'MIME-Version:1.0\r\\\
',
'Content-Transfer-Encoding:base64 \\\
',
'Content-Disposition:attachment; filename =example.png\\\\\\\\\\,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
'--foo_bar_baz--'
] .join('');

//发送邮件!
$ .ajax({
type:POST,
url:https://www.googleapis.com/upload/gmail/v1/users/me/messages/send?
beforeSend:function(xhr,settings){
xhr.setRequestHeader('Authorization','Bearer {YOUR_API_KEY}');

},
data:mail
});


i' m working on a javascript client able to read a CSV which contains an image url list.

I m able to read the csv by the means of jquery-csv and to draw each image in a html5 canvas.

The next step is to apply to each image a text layer and to send the image by email using 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.

Do have i to build a json according to the multipart gmail guidelines and to send it as POST body as specified?

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 {YOUR_API_KEY}');
  },
  data: mail
}); 

这篇关于发送电子邮件附件的GMAIL API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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