使用googleapis以html附件发送电子邮件为pdf

时间:2017-05-16 14:42:12

标签: node.js express pdf google-api gmail-api

我尝试使用node.js上的google apis发送包含某些HTML代码作为pdf文件的电子邮件,这可能吗?

据我所知,在使用gmail.users.messages.send方法时,我需要将所有内容添加到原始字段,包括附件。问题是没有关于如何添加一个......

的例子

根据原始邮件,我尝试使用内容类型:multipart / mixed和application / pdf。它创建的邮件只包含一个名为noname的文件,没有任何格式,并且包含在邮件内容中,而不是文件中应包含的内容。

我还尝试将附件添加为数组的一部分,例如nodemailer语法,但它没有用。

1 个答案:

答案 0 :(得分:0)

基于此documentation,创建带附件的邮件就像创建任何其他邮件一样,但将文件作为多部分MIME邮件上传的过程取决于编程语言。

以下是一些可能有用的示例:

function sendEmailWithAttachments() {

  var attachments = ["File_ID_1", "File_ID_2"];

  var message = {
    to: {
      name: "Google Scripts",
      email: "amit@labnol.org"
    },
    from: {
      name: "Amit Agarwal",
      email: "amit@labnol.org"
    },
    body: {
      text: "Mr hänn is schon lang nümme g'she.",
      html: "Mr hänn is schon <b>lang nümme</b> g'she."
    },
    subject: "ctrlq, tech à la carte",
    files: getAttachments_(attachments)
  };

  // Compose Gmail message and send immediately
  callGmailAPI_(message);

}


function callGmailAPI_(message) {

  var payload = createMimeMessage_(message);

  var response = UrlFetchApp.fetch(
    "https://www.googleapis.com/upload/gmail/v1/users/me/messages/send?uploadType=media", {
      method: "POST",
      headers: {
        "Authorization": "Bearer " + ScriptApp.getOAuthToken(),
        "Content-Type": "message/rfc822",
      },
      muteHttpExceptions: true,
      payload: payload
    });

  Logger.log(response.getResponseCode());
  Logger.log(response.getContentText());

}

// UTF-8 characters in names and subject
function encode_(subject) {
  var enc_subject = Utilities.base64Encode(subject, Utilities.Charset.UTF_8);
  return '=?utf-8?B?' + enc_subject + '?=';
}

// Insert file attachments from Google Drive
function getAttachments_(ids) {
  var att = [];
  for (var i in ids) {
    var file = DriveApp.getFileById(ids[i]);
    att.push({
      mimeType: file.getMimeType(),
      fileName: file.getName(),
      bytes: Utilities.base64Encode(file.getBlob().getBytes())
    });
  }
  return att;
}

// Create a MIME message that complies with RFC 2822
function createMimeMessage_(msg) {

  var nl = "\n";
  var boundary = "__ctrlq_dot_org__";

  var mimeBody = [

    "MIME-Version: 1.0",
    "To: " + encode_(msg.to.name) + "<" + msg.to.email + ">",
    "From: " + encode_(msg.from.name) + "<" + msg.from.email + ">",
    "Subject: " + encode_(msg.subject), // takes care of accented characters

    "Content-Type: multipart/alternative; boundary=" + boundary + nl,
    "--" + boundary,

    "Content-Type: text/plain; charset=UTF-8",
    "Content-Transfer-Encoding: base64" + nl,
    Utilities.base64Encode(msg.body.text, Utilities.Charset.UTF_8) + nl,
    "--" + boundary,

    "Content-Type: text/html; charset=UTF-8",
    "Content-Transfer-Encoding: base64" + nl,
    Utilities.base64Encode(msg.body.html, Utilities.Charset.UTF_8) + nl

  ];

  for (var i = 0; i < msg.files.length; i++) {

    var attachment = [
      "--" + boundary,
      "Content-Type: " + msg.files[i].mimeType + '; name="' + msg.files[i].fileName + '"',
      'Content-Disposition: attachment; filename="' + msg.files[i].fileName + '"',
      "Content-Transfer-Encoding: base64" + nl,
      msg.files[i].bytes
    ];

    mimeBody.push(attachment.join(nl));

  }

  mimeBody.push("--" + boundary + "--");

  return mimeBody.join(nl);

}