Insert text with hyperlink in Gmail with Google Script?
Asked Answered
W

3

7

I am trying to send an email with an attachment and the URL of the attachment. The email body would look like this:

"Hi Everyone-

Here's the agenda for tomorrow's meeting.

See you in the morning!

-John Doe"

I have written the code to my best ability. I know I wrote the bottom half erroneously. Please assist.

  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('[email protected]', 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)] });
}
Wilkens answered 13/12, 2016 at 2:0 Comment(0)
W
5

I have found that sending out emails using GAS is suggested to do so in HTML. zaq just posted his answer right after I found a better way to re-write my code. Thank you zaq though! Here is my working code:

    var emailTo = '[email protected]';
    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);
Wilkens answered 15/12, 2016 at 0:9 Comment(0)
E
8

Inserting a hyperlink in email requires sending the email in HTML, rather than in plain text as you do now. Change the syntax of sendEmail call to sendEmail(Object) where the argument is an object with htmlBody field containing your message. Like this:

var message = {
  to: "[email protected]",
  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); 

Using double quotes for message body is preferable, because then you don't need to escape apostrophes in text.

Ernest answered 13/12, 2016 at 4:3 Comment(0)
W
5

I have found that sending out emails using GAS is suggested to do so in HTML. zaq just posted his answer right after I found a better way to re-write my code. Thank you zaq though! Here is my working code:

    var emailTo = '[email protected]';
    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);
Wilkens answered 15/12, 2016 at 0:9 Comment(0)
C
0

I have put the message body directly on my sheet and I have used the below code adapting the above logic. Sharing with all who find it useful,

<a href="https://www.youtube.com/watch?v=YHIwETgynFw&feature=youtu.be">click here</a>
Currajong answered 12/3, 2020 at 13:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.