// 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
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…