简体   繁体   English

如何提供在 Google MailApp 中发送的 email 标头?

[英]How can I supply email headers sending in Google MailApp?

How can I include email headers information using Google MailApp api?如何使用 Google MailApp api 包含 email 标头信息?

I need to supply headers info such as List-Unsubscribe: or List-Unsubscribe-Post: .我需要提供标头信息,例如List-Unsubscribe:List-Unsubscribe-Post:

The following is a code sample by Google Apps Script.以下是 Google Apps Script 的代码示例。 It seems that there is no option to include such email header info.似乎没有选择包含这样的 email header 信息。 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]
});

I believe your goal as follows.我相信你的目标如下。

  • You want to add the custom headers of List-Unsubscribe and List-Unsubscribe-Post to the Gmail and send it using Google Apps Script.您想将List-UnsubscribeList-Unsubscribe-Post的自定义标头添加到 Gmail 并使用 Google Apps 脚本发送。

In this case, how about the following flow?在这种情况下,下面的流程怎么样?

  1. Create a draft as a temporal.将草稿创建为时态。
    • In this case, the arguments of MailApp.sendEmail in your script are used.在这种情况下,使用脚本中MailApp.sendEmail的 arguments。
  2. Add the custom headers.添加自定义标题。
  3. Delete Draft.删除草稿。
  4. Send the draft using Gmail API.使用 Gmail API 发送草稿。

When this flow is reflected to your script, it becomes as follows.当这个流程反映到您的脚本时,它变成如下。

Modified script:修改后的脚本:

Before you use this script, please enable Gmail API at Advanced Google services .在使用此脚本之前, 请在 Google 高级服务中启用 Gmail API

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)
  • In this sample, List-Unsubscribe: sample1 and List-Unsubscribe-Post: sample2 are used as the sample values.在此示例中, List-Unsubscribe: sample1List-Unsubscribe-Post: sample2用作示例值。 Please modify this for your actual situation.请根据您的实际情况进行修改。

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何强制 Google App Script MailApp.sendEmail 在循环中为每个 email 使用一个新线程? - how can I force Google App Script MailApp.sendEmail to use a new thread for each email in a loop? Google Apps Script 中的 MailApp.sendEmail() 不发送电子邮件 - MailApp.sendEmail() in Google Apps Script not sending email 如何阻止MailApp.sendEmail向我发送电子邮件的副本 - How to stop MailApp.sendEmail from sending myself a copy of the email 您如何使用 google Mailapp 回复电子邮件主题? - How do you reply to email thread with google Mailapp? 如何阻止 MailApp.sendEmail() 每约 80 个字符向电子邮件正文添加换行符? - How can I stop MailApp.sendEmail() from adding line breaks to an email body every ~80 chars? 如何确认从 Google 脚本 MailApp.sendEmail 发送的 email? - How to confirm email sent from Google scripts MailApp.sendEmail? 如何在 Google Apps 脚本中使用 MailApp 发送带有附件的 HTML email? - How to send a HTML email with attachment using MailApp in Google Apps Script? Apps 脚本未使用 MailApp.sendEmail() 发送 email - Apps Script not sending email with MailApp.sendEmail() MailApp.sendEmail 在谷歌应用程序脚本中未设置时向自己发送电子邮件 - MailApp.sendEmail sending email to myself when not set to in google apps script 我可以在Google Apps Script MailApp中执行条件语句吗? - Can I do conditional statements within Google Apps Script MailApp?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM