0

I have created the following script to add a file to a GMail:

function mySendEMail(NIDMail) {
  var mySubject = "This is only a Test";
  var myURL = DriveApp.getFileById(NIDMail).getUrl();
  var myBody = "Hello World,\n\n";
  myBody = myBody + "dddddddddddd\n";

  var file = DriveApp.getFileById(NIDMail);

  MailApp.sendEmail("xxxxx@googlemail.com", mySubject, myBody, {
     attachments: [file.getAs(MimeType.PDF)],
     })
}

With this script I add the file per PDF, but how I can add a attachment per Link to GoogleDrive (like with the GDrive-Symbol)?

Greetings

J. Doe
  • 151
  • 12

1 Answers1

0

I believe you are trying to replicate the behavior of "Insert file using Drive" in the email composition where the attachment is the link to the file in the Google Drive and not the downloadable version of the file.

This feature is not yet available to the attachments of sendEmail but you can use the htmlBody (documentation) as parameter to sendEmail and add a clickable image or text as element. This will automatically includes a Drive URL (similar to 'Insert file using Drive') in the attachments section of email.

Sample Email:

enter image description here

Here's the code snippet I used to produce the sample email above:

function mySendEMail() {
  var NIDMail = "xxxxxxxxxx";
  var mySubject = "This is only a Test";
  var myURL = DriveApp.getFileById(NIDMail).getUrl();
  var myBody = "Hello World,\n\n";
  var gdriveLogo = "https://firebasestorage.googleapis.com/v0/b/drive-assets.google.com.a.appspot.com/o/Asset%20-%20Drive%20Lockup.png?alt=media"
  var html_body = "<p>This is a test email</p>"
                  + "<br>"
                  + "<p>(Test Document)</p>"
                  + "<br>"
                  + "<a href='" + myURL + "'>"
                  + "<img src='" + gdriveLogo + "' width='130' height='50'>"
                  + "</a>";

  MailApp.sendEmail({
    to: "xxxxxemailxxxxxx", 
    subject: mySubject, 
    htmlBody: html_body
  });
}
Nikko J.
  • 5,319
  • 1
  • 5
  • 14
  • Your example works perfekt. However, it is very difficult for me to understand why Google did not include this feature in the scripting. Because exactly this feature is one of the great advantages of Google. – J. Doe Nov 30 '20 at 19:08