使用 Google 脚本在 Gmail 中插入带有超链接的文本?
Insert text with hyperlink in Gmail with Google Script?
我正在尝试发送带有附件和附件 URL 的电子邮件。电子邮件正文如下所示:
“大家好-
这是明天会议的agenda。
早上见!
-李四
我已经尽我所能编写了代码。我知道我写错了下半部分。请协助。
function sendEmailTest() {
//test send email.
// Send an email with an attachment: a file from Google Drive.
var file = DocumentApp.openById(file2.getId());
var ssUrl = 'URL_BUFFERED';
var sheetName = 'Sheet1'; // name of sheet to use
var rangeName = 'C30'; // range of values to include
var dateRange = SpreadsheetApp.openByUrl(ssUrl)
.getSheetByName(sheetName)
.getRange(rangeName)
.getValues();
// Name of Google file to attach.
var file2 = file.makeCopy('Weekly Agenda | '+dateRange);
// Set Agenda URL.
var theBody = GmailApp.openById(file2.getId()).getBody();
var elementReplaced = theBody.replaceText("%toReplace%", "agendaURL");
elementReplaced.asText().setLinkUrl("https://google.com");
//Send email.
MailApp.sendEmail('who@whowantstoknow.com', file2, 'Hi Everyone- \n Here\'s the ' +agendaURL+'agenda for tomorrow
's meeting. \n See you in the morning! \n-John Doe', {
name: 'Automatic Emailer Script',
attachments: [file.getAs(file2)] });
}
在电子邮件中插入超链接需要以 HTML 格式发送电子邮件,而不是像现在这样以纯文本格式发送。将 sendEmail 调用的语法更改为 sendEmail(Object)
,其中参数是一个包含您的消息的 htmlBody 字段的对象。像这样:
var message = {
to: "recipient@example.com",
subject: "Weekly Agenda | " + dateRange,
htmlBody: "Hi Everyone-\n Here's the <a href='" + agendaURL + ''">agenda</a> for tomorrow's meeting.\n See you in the morning!\n-John Doe",
name: "Automatic Emailer Script",
attachments: [file2.getAs(MimeType.PDF)]
};
MailApp.sendEmail(message);
最好对邮件正文使用双引号,因为这样您就不需要在文本中转义撇号。
我发现 HTML 中建议使用 GAS 发送电子邮件。 zaq 在我找到重写代码的更好方法后立即发布了他的答案。谢谢扎克!这是我的工作代码:
var emailTo = 'totheuniverse@omnipresence.com';
var subject = "Weekly Agenda | " +dateRange;
var options = {}
options.htmlBody = "Hi Everyone-" +'<br />'+'<br />'+ "Here\'s the " + '<a href=\"' +agendaURL+ '">agenda</a>' + " for tomorrow\
's meeting." +'<br />'+'<br />'+ "See you in the morning!" +'<br />'+'<br />'+ "-John Doe";
options.attachment = [file];
MailApp.sendEmail(emailTo, subject, '', options);
我已将邮件正文直接放在我的 sheet 上,并且我使用了以下代码来调整上述逻辑。与所有觉得有用的人分享,
<a href="https://www.youtube.com/watch?v=YHIwETgynFw&feature=youtu.be">click here</a>
我正在尝试发送带有附件和附件 URL 的电子邮件。电子邮件正文如下所示:
“大家好-
这是明天会议的agenda。
早上见!
-李四
我已经尽我所能编写了代码。我知道我写错了下半部分。请协助。
function sendEmailTest() {
//test send email.
// Send an email with an attachment: a file from Google Drive.
var file = DocumentApp.openById(file2.getId());
var ssUrl = 'URL_BUFFERED';
var sheetName = 'Sheet1'; // name of sheet to use
var rangeName = 'C30'; // range of values to include
var dateRange = SpreadsheetApp.openByUrl(ssUrl)
.getSheetByName(sheetName)
.getRange(rangeName)
.getValues();
// Name of Google file to attach.
var file2 = file.makeCopy('Weekly Agenda | '+dateRange);
// Set Agenda URL.
var theBody = GmailApp.openById(file2.getId()).getBody();
var elementReplaced = theBody.replaceText("%toReplace%", "agendaURL");
elementReplaced.asText().setLinkUrl("https://google.com");
//Send email.
MailApp.sendEmail('who@whowantstoknow.com', file2, 'Hi Everyone- \n Here\'s the ' +agendaURL+'agenda for tomorrow
's meeting. \n See you in the morning! \n-John Doe', {
name: 'Automatic Emailer Script',
attachments: [file.getAs(file2)] });
}
在电子邮件中插入超链接需要以 HTML 格式发送电子邮件,而不是像现在这样以纯文本格式发送。将 sendEmail 调用的语法更改为 sendEmail(Object)
,其中参数是一个包含您的消息的 htmlBody 字段的对象。像这样:
var message = {
to: "recipient@example.com",
subject: "Weekly Agenda | " + dateRange,
htmlBody: "Hi Everyone-\n Here's the <a href='" + agendaURL + ''">agenda</a> for tomorrow's meeting.\n See you in the morning!\n-John Doe",
name: "Automatic Emailer Script",
attachments: [file2.getAs(MimeType.PDF)]
};
MailApp.sendEmail(message);
最好对邮件正文使用双引号,因为这样您就不需要在文本中转义撇号。
我发现 HTML 中建议使用 GAS 发送电子邮件。 zaq 在我找到重写代码的更好方法后立即发布了他的答案。谢谢扎克!这是我的工作代码:
var emailTo = 'totheuniverse@omnipresence.com';
var subject = "Weekly Agenda | " +dateRange;
var options = {}
options.htmlBody = "Hi Everyone-" +'<br />'+'<br />'+ "Here\'s the " + '<a href=\"' +agendaURL+ '">agenda</a>' + " for tomorrow\
's meeting." +'<br />'+'<br />'+ "See you in the morning!" +'<br />'+'<br />'+ "-John Doe";
options.attachment = [file];
MailApp.sendEmail(emailTo, subject, '', options);
我已将邮件正文直接放在我的 sheet 上,并且我使用了以下代码来调整上述逻辑。与所有觉得有用的人分享,
<a href="https://www.youtube.com/watch?v=YHIwETgynFw&feature=youtu.be">click here</a>