gpt4 book ai didi

google-apps-script - (Gmail) 从电子表格发送电子邮件。如何添加带有图像的签名?

转载 作者:行者123 更新时间:2023-12-02 07:02:14 25 4
gpt4 key购买 nike

由于我要使用 GMAIL 发送大量电子邮件,我决定使用脚本并遵循本教程来自动化此过程。 Tutorial: Sending emails from a Spreadsheet

“消息”由我创建的另一个函数生成,称为 prepareEmails

问题如下:

1) 我如何告诉 prepareEmails 添加我的个人签名?我不能简单地将它的文本复制到那个函数中,因为我的签名包含一个图像(我有它的 URL),我希望将该图像放入签名中。

2) 我怎样才能使我的签名加粗?

谢谢大家

最佳答案

有一个开放的Issue 2441请求在使用 GMailService 时将 gmail 签名附加到电子邮件的能力。访问并加注星标以接收更新。

正如@wchiquito 所建议的,您可以制作一个脚本来附加图像,生成签名。您还可以使用 HTML 标记,例如 <B></B>以粗体呈现文本,等等。

这是一种不同的方法,它将使用电子邮件草稿作为模板。这样,您就可以使用在线编辑器生成包含各种字体和图像的签名,并最终获得类似于自动签名插入的功能。

模板需要保存在您的草稿文件夹中,并且需要有一个标签来指示电子邮件正文的位置。

Screenshot

例子

function sendWithTemplate() {
var msgBody = "Test of sending a message using a template with a signature.";
sendGmailTemplate(Session.getActiveUser().getEmail(), 'test', msgBody );
}

Screenshot

脚本

/**
* Insert the given email body text into an email template, and send
* it to the indicated recipient. The template is a draft message with
* the subject "TEMPLATE"; if the template message is not found, an
* exception will be thrown. The template must contain text indicating
* where email content should be placed: {BODY}.
*
* @param {String} recipient Email address to send message to.
* @param {String} subject Subject line for email.
* @param {String} body Email content, may be plain text or HTML.
* @param {Object} options (optional) Options as supported by GmailApp.
*
* @returns GmailApp the Gmail service, useful for chaining
*/
function sendGmailTemplate(recipient, subject, body, options) {
options = options || {}; // default is no options
var drafts = GmailApp.getDraftMessages();
var found = false;
for (var i=0; i<drafts.length && !found; i++) {
if (drafts[i].getSubject() == "TEMPLATE") {
found = true;
var template = drafts[i];
}
}
if (!found) throw new Error( "TEMPLATE not found in drafts folder" );

// Generate htmlBody from template, with provided text body
var imgUpdates = updateInlineImages(template);
options.htmlBody = imgUpdates.templateBody.replace('{BODY}', body);
options.attachments = imgUpdates.attachments;
options.inlineImages = imgUpdates.inlineImages;
return GmailApp.sendEmail(recipient, subject, body, options);
}


/**
* This function was adapted from YetAnotherMailMerge by Romain Vaillard.
* Given a template email message, identify any attachments that are used
* as inline images in the message, and move them from the attachments list
* to the inlineImages list, updating the body of the message accordingly.
*
* @param {GmailMessage} template Message to use as template
* @returns {Object} An object containing the updated
* templateBody, attachments and inlineImages.
*/
function updateInlineImages(template) {
//////////////////////////////////////////////////////////////////////////////
// Get inline images and make sure they stay as inline images
//////////////////////////////////////////////////////////////////////////////
var templateBody = template.getBody();
var rawContent = template.getRawContent();
var attachments = template.getAttachments();

var regMessageId = new RegExp(template.getId(), "g");
if (templateBody.match(regMessageId) != null) {
var inlineImages = {};
var nbrOfImg = templateBody.match(regMessageId).length;
var imgVars = templateBody.match(/<img[^>]+>/g);
var imgToReplace = [];
if(imgVars != null){
for (var i = 0; i < imgVars.length; i++) {
if (imgVars[i].search(regMessageId) != -1) {
var id = imgVars[i].match(/realattid=([^&]+)&/);
if (id != null) {
var temp = rawContent.split(id[1])[1];
temp = temp.substr(temp.lastIndexOf('Content-Type'));
var imgTitle = temp.match(/name="([^"]+)"/);
if (imgTitle != null) imgToReplace.push([imgTitle[1], imgVars[i], id[1]]);
}
}
}
}
for (var i = 0; i < imgToReplace.length; i++) {
for (var j = 0; j < attachments.length; j++) {
if(attachments[j].getName() == imgToReplace[i][0]) {
inlineImages[imgToReplace[i][2]] = attachments[j].copyBlob();
attachments.splice(j, 1);
var newImg = imgToReplace[i][1].replace(/src="[^\"]+\"/, "src=\"cid:" + imgToReplace[i][2] + "\"");
templateBody = templateBody.replace(imgToReplace[i][1], newImg);
}
}
}
}
var updatedTemplate = {
templateBody: templateBody,
attachments: attachments,
inlineImages: inlineImages
}
return updatedTemplate;
}

归功于应得的功劳:“Yet Another Mail Merge”脚本包含在邮件合并期间保留电子邮件中的内联图像的代码 - 我从中借用了它。谢谢罗曼!

关于google-apps-script - (Gmail) 从电子表格发送电子邮件。如何添加带有图像的签名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18493808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com