Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
606 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
// 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"
',
  'MIME-Version: 1.0
',
  'From: [email protected]
',
  'To: [email protected]
',
  '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/png
',
  'MIME-Version: 1.0
',
  'Content-Transfer-Encoding: base64
',
  'Content-Disposition: attachment; filename="example.png"

',

   pngData, '

',

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...