【问题标题】:Insert text with hyperlink in Gmail with Google Script?使用 Google Script 在 Gmail 中插入带有超链接的文本?
【发布时间】:2016-12-13 02:00:01
【问题描述】:

我正在尝试发送带有附件和附件 URL 的电子邮件。电子邮件正文如下所示:

“大家好-

这是明天会议的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)] });
}

【问题讨论】:

    标签: email google-apps-script attachment


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      我发现建议在 HTML 中使用 GAS 发送电子邮件。 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);
      

      【讨论】:

        【解决方案3】:

        我已将消息正文直接放在我的工作表上,并且我使用了以下代码来适应上述逻辑。与所有觉得有用的人分享,

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-07
          • 2020-07-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多