使用Google Script在Gmail中插入带超链接的文字?

时间:2016-12-13 02:00:02

标签: email google-apps-script attachment

我正在尝试发送包含附件和附件网址的电子邮件。电子邮件正文如下所示:

"大家好 -

这是明天会议的agenda

早上见!

-John Doe"

我已经将代码编写为我最好的能力。我知道我错误地写了下半部分。请协助。

  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)] });
}

3 个答案:

答案 0 :(得分:2)

在电子邮件中插入超链接需要以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); 

最好在邮件正文中使用双引号,因为这样您就不需要在文本中转义撇号。

答案 1 :(得分:1)

我发现建议使用GAS发送电子邮件以HTML格式发送。 zaq在找到重写代码的更好方法之后立即发布了他的答案。谢谢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);

答案 2 :(得分:0)

我将消息正文直接放在表单上,​​并且使用了下面的代码来适应上面的逻辑。与所有认为有用的人共享

<a href="https://www.youtube.com/watch?v=YHIwETgynFw&feature=youtu.be">click here</a>