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() {
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:
function SendEmailUsingGAS(): This line declares a function named
SendEmailUsingGAS. When this function is called, it will execute the code within its curly braces.var subj = 'Test Email';: This line declares a variable
subjand assigns it the string value'Test Email'. This will be used as the subject of the email.var mailBody = 'Test Email';: This line declares a variable
mailBodyand assigns it the string value'Test Email'. This will be used as the body of the email.var img = DriveApp.getFileById('1fAalmjqjhjPpuDrcTBuHlt5NKCpe7Kvo').getAs('image/png');: This line fetches an image file from Google Drive using its file ID (
'1fAalmjqjhjPpuDrcTBuHlt5NKCpe7Kvo'). ThegetAs('image/png')method ensures that the file is treated as a PNG image.var Emg = {'Logo': img};: This line creates an object named
Emgwith 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.MailApp.sendEmail({...});: This line sends an email using the
MailAppservice 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
subjvariable). - htmlBody: The HTML body of the email. It combines the
mailBodyvariable 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 theEmgobject. - inlineImages: An object containing inline images to be included in the email. In this case, it contains the
Emgobject, which associates the'Logo'content ID with the image.
- to: The recipient email address (
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
Post a Comment