【问题标题】:How can I supply email headers sending in Google MailApp?如何提供在 Google MailApp 中发送的电子邮件标头?
【发布时间】:2021-08-14 21:36:28
【问题描述】:

如何使用 Google MailApp api 包含电子邮件标头信息?

我需要提供标题信息,例如 List-Unsubscribe:List-Unsubscribe-Post:

以下是 Google Apps 脚本的代码示例。似乎没有包含此类电子邮件标题信息的选项。 sendEmail(recipient, subject, body, options)

// Send an email with two attachments: a file from Google Drive (as a PDF) and an HTML file.
var file = DriveApp.getFileById('1234567890abcdefghijklmnopqrstuvwxyz');
var blob = Utilities.newBlob('Insert any HTML content here', 'text/html', 
'my_document.html');
MailApp.sendEmail('mike@example.com', 'Attachment example', 'Two files are attached.', {
                   name: 'Automatic Emailer Script',
                   attachments: [file.getAs(MimeType.PDF), blob]
});

【问题讨论】:

    标签: google-apps-script gmail-api email-headers


    【解决方案1】:

    我相信你的目标如下。

    • 您希望将 List-UnsubscribeList-Unsubscribe-Post 的自定义标头添加到 Gmail 并使用 Google Apps 脚本发送。

    这样的话,下面的流程呢?

    1. 创建草稿作为临时。
      • 在这种情况下,使用脚本中MailApp.sendEmail 的参数。
    2. 添加自定义标题。
    3. 删除草稿。
    4. 使用 Gmail API 发送草稿。

    当这个流程反映到你的脚本中时,它变成如下。

    修改脚本:

    在使用此脚本之前,please enable Gmail API at Advanced Google services

    var obj = {"List-Unsubscribe": "sample1", "List-Unsubscribe-Post": "sample2"}; // Please set the custom headers.
    
    // 1. Create a draft as a temporal.
    var file = DriveApp.getFileById('1234567890abcdefghijklmnopqrstuvwxyz');
    var blob = Utilities.newBlob('Insert any HTML content here', 'text/html', 'my_document.html');
    var draft = GmailApp.createDraft('mike@example.com', 'Attachment example', 'Two files are attached.', {
      name: 'Automatic Emailer Script',
      attachments: [file.getAs(MimeType.PDF), blob]
    });
    
    // 2. Add the custom headers.
    var data = Object.entries(obj).map(([k, v]) => `${k}: ${v}`).join("\n") + "\n" + draft.getMessage().getRawContent();
    
    // 3. Delete Draft.
    draft.deleteDraft();
    
    // 4. Send the draft using Gmail API.
    var res = Gmail.Users.Messages.send({raw: Utilities.base64EncodeWebSafe(data)}, "me");
    console.log(res)
    
    • 在此示例中,List-Unsubscribe: sample1List-Unsubscribe-Post: sample2 用作示例值。请根据您的实际情况进行修改。

    参考资料:

    【讨论】:

    • 我会试试的。谢谢你的建议。 :)
    • @zawhtut 感谢您的回复。当我提出的答案没有用时,我深表歉意。
    • 我还没试过。我会让你知道它是否有效...... :)
    • @zawhtut 感谢您的回复。没问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-19
    相关资源
    最近更新 更多