Bas*_*asj 7 javascript email gmail google-apps-script gmail-api
使用Google Apps脚本(http://script.google.com),我从文档中了解如何发送,转发,转移到垃圾邮件等,但我找不到如何删除电子邮件的文件附件,即:
如果通过API无法实现,是否有办法将消息重新发送给自己,同时保留1,2和3?
注意:GmailAttachment
该类看起来很有趣,并允许列出收件人:
var threads = GmailApp.getInboxThreads(0, 10);
var msgs = GmailApp.getMessagesForThreads(threads);
for (var i = 0 ; i < msgs.length; i++) {
for (var j = 0; j < msgs[i].length; j++) {
var attachments = msgs[i][j].getAttachments();
for (var k = 0; k < attachments.length; k++) {
Logger.log('Message "%s" contains the attachment "%s" (%s bytes)',
msgs[i][j].getSubject(), attachments[k].getName(), attachments[k].getSize());
}
}
}
Run Code Online (Sandbox Code Playgroud)
但我找不到如何删除附件.
注意:我已经研究过许多其他解决方案,我已经阅读了几乎所有关于此的文章(具有专用Web服务的解决方案,本地客户端如Thunderbird + Attachment提取器插件等),但它们都不是真的很酷.这就是为什么我正在寻找通过Google Apps脚本手动执行此操作的解决方案.
看起来必须重新创建消息 - ish:
消息是不可变的:它们只能被创建和删除.除了应用于给定消息的标签之外,不能更改任何消息属性.
通过Gmail API插入()使用高级Gmail服务,您可以使用以下方法解决问题:Gmail.Users.Messages.insert(resource, userId)
示例:[ EMAIL_ID
使用email_id
或以任何方式填写您想要收到的电子邮件]
function removeAttachments () {
// Get the `raw` email
var email = GmailApp.getMessageById("EMAIL_ID").getRawContent();
// Find the end boundary of html or plain-text email
var re_html = /(-*\w*)(\r)*(\n)*(?=Content-Type: text\/html;)/.exec(email);
var re = re_html || /(-*\w*)(\r)*(\n)*(?=Content-Type: text\/plain;)/.exec(email);
// Find the index of the end of message boundary
var start = re[1].length + re.index;
var boundary = email.indexOf(re[1], start);
// Remove the attachments & Encode the attachment-free RFC 2822 formatted email string
var base64_encoded_email = Utilities.base64EncodeWebSafe(email.substr(0, boundary));
// Set the base64Encoded string to the `raw` required property
var resource = {'raw': base64_encoded_email}
// Re-insert the email into the user gmail account with the insert time
/* var response = Gmail.Users.Messages.insert(resource, 'me'); */
// Re-insert the email with the original date/time
var response = Gmail.Users.Messages.insert(resource, 'me',
null, {'internalDateSource': 'dateHeader'});
Logger.log("The inserted email id is: %s",response.id)
}
Run Code Online (Sandbox Code Playgroud)
这将从电子邮件中删除附件并将其重新插入您的邮箱.
编辑/更新:新的RegExp用于处理html和仅纯文本的电子邮件 - 现在应该处理多个边界字符串
归档时间: |
|
查看次数: |
901 次 |
最近记录: |