Send Email with Image/s in Mail Body Using GAS-2

Note:
GmailApp - Respect cells formatting (line break but not bold text).

MailApp - Don't respect cell formatting. It works better with HTML template.


 function SendEmailUsingGAS() {

  var subj='Test Email';
  var mailBody='Test Email';
  var img=DriveApp.getFileById('1fAalmjqjhjPpuDrcTBuHlt5NKCpe7Kvo').getAs('image/png');
  var Emg={'Logo':img};
  MailApp.sendEmail({to:'ExcelSujeet@gmail.com',subject:subj,htmlBody:mailBody+"<br><br>"+"<img src='cid:Logo' style='width:100px;height:100px'>",inlineImages:Emg});
}

URL's https://developers.google.com/apps-script/reference/mail/mail-app https://developers.google.com/apps-script/reference/mail/mail-app#sendemailrecipient,-subject,-body,-options
Explanation

This code is a Google Apps Script (GAS) function that sends an email using Gmail services and includes an inline image. Let's break down the code:

  1. function SendEmailUsingGAS(): This line declares a function named SendEmailUsingGAS. When this function is called, it will execute the code within its curly braces.

  2. var subj = 'Test Email';: This line declares a variable subj and assigns it the string value 'Test Email'. This will be used as the subject of the email.

  3. var mailBody = 'Test Email';: This line declares a variable mailBody and assigns it the string value 'Test Email'. This will be used as the body of the email.

  4. var img = DriveApp.getFileById('1fAalmjqjhjPpuDrcTBuHlt5NKCpe7Kvo').getAs('image/png');: This line fetches an image file from Google Drive using its file ID ('1fAalmjqjhjPpuDrcTBuHlt5NKCpe7Kvo'). The getAs('image/png') method ensures that the file is treated as a PNG image.

  5. var Emg = {'Logo': img};: This line creates an object named Emg with a property named 'Logo' whose value is the image fetched in the previous step. This object will be used to associate the image with its content ID.

  6. MailApp.sendEmail({...});: This line sends an email using the MailApp service in Google Apps Script. It takes an object as an argument with various parameters:

    • to: The recipient email address ('ExcelSujeet@gmail.com' in this case).
    • subject: The subject of the email (set to the value of the subj variable).
    • htmlBody: The HTML body of the email. It combines the mailBody variable with additional HTML code to include the image in the email body. The image is referenced using a Content-ID (cid) reference, which is the key 'Logo' defined earlier in the Emg object.
    • inlineImages: An object containing inline images to be included in the email. In this case, it contains the Emg object, which associates the 'Logo' content ID with the image.

So, this function essentially sends an email with the subject 'Test Email', body 'Test Email', and includes an inline image fetched from Google Drive with the specified file ID.

Comments

Popular posts from this blog

Copy Paste Data, Remove Filter, Apply Filter, Delete Filtered Data

Send Email with Image/s in Mail Body Using GAS-1