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

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

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


 
GS Code

function DesingTeam() {
  var ss=SpreadsheetApp.getActiveSpreadsheet();
  var sht_Guidelines=ss.getSheetByName('Guidelines');
  var sht_DesignTeam=ss.getSheetByName('Design team');
  var lastRow;
  var i;
  var strMailBody;
  var strTask;
  var strDate;
  var strTo;
  var strCC;
  //Deadline Approaching
  var strMailBodyTemp_1=sht_Guidelines.getRange('D2').getValue();
  var strSubject_1=sht_Guidelines.getRange('C2').getValue();
  var image_1=DriveApp.getFileById('1iQYLBJQAWn_6_0rviBuFxjyN4IMBVrJz').getAs('image/jpeg');
  var Eimage_1={'Logo':image_1};
  var Ebody_1=HtmlService.createTemplateFromFile("DeadlineApproaching");
  var name1;
  //Reminder
  var strMailBodyTemp_2=sht_Guidelines.getRange('D3').getValue();
  var strSubject_2=sht_Guidelines.getRange('C3').getValue();
  var image_2=DriveApp.getFileById('1Bq_d-bS4h0ZTJmtLL0QL1gLcGyS7yPDa').getAs('image/jpeg');
  var Eimage_2={'Logo':image_2};
  var Ebody_2=HtmlService.createTemplateFromFile("Reminder");
  var name2;
  //Task Completion
  var strMailBodyTemp_3=sht_Guidelines.getRange('D4').getValue();
  var strSubject_3=sht_Guidelines.getRange('C4').getValue();
  var image_3=DriveApp.getFileById('1_UXHssDP7znIMS3pvfZThVuV47N_EB13').getAs('image/gif');
  var Eimage_3={'Logo':image_3};
  var Ebody_3=HtmlService.createTemplateFromFile("TaskCompletion");
  var name3;
  //Task Pending
  var strMailBodyTemp_4=sht_Guidelines.getRange('D5').getValue();
  var strSubject_4=sht_Guidelines.getRange('C5').getValue();
  var image_4=DriveApp.getFileById('1W0blvSzs_MDHak8rmivk847ouoBx4zi2').getAs('image/jpeg');
  var Eimage_4={'Logo':image_4};
  var Ebody_4=HtmlService.createTemplateFromFile("TaskPending");
  var name4;
  //Pending long time
  var strMailBodyTemp_5=sht_Guidelines.getRange('D6').getValue();
  var strSubject_5=sht_Guidelines.getRange('C6').getValue();
  var image_5=DriveApp.getFileById('1B0c00bDf31kqiTXpcgNEgBwaqnvyxk7o').getAs('image/jpeg');
  var Eimage_5={'Logo':image_5};
  var Ebody_5=HtmlService.createTemplateFromFile("PendingLongTime");
  var name5;
  lastRow=sht_DesignTeam.getRange("A3").getDataRegion().getLastRow();
  if(lastRow>3){
    for(i=3; i<=lastRow;i++){
      if(sht_DesignTeam.getRange(i,2).getValue()!=null){
        sht_DesignTeam.getRange('G1').setFormulaR1C1("=R[" +(i-1)+"]C[-5]-RC[-1]")
        sht_DesignTeam.getRange('H1').setFormulaR1C1("=RC[-2]-R[" +(i-1)+"]C[-6]")
        //******Deadline Approaching**********
        if(sht_DesignTeam.getRange('G1').getValue()==7 &&
        sht_DesignTeam.getRange(i,5).getValue()!='Email Sent' &&
        sht_DesignTeam.getRange(i,7).getValue()!='Completed'){
          strTask=sht_DesignTeam.getRange(i,1).getValue();
          strDate=Utilities.formatDate(sht_DesignTeam.getRange(i,2).getValue(),"GMT+5:30","dd-MMM-YYYY");
          strTo=sht_DesignTeam.getRange(i,3).getValue();
          strCC=sht_DesignTeam.getRange(i,4).getValue();

          name1="X"
          Ebody_1.name=name1;
          Ebody_1.task=strTask;
          Ebody_1.date=strDate;
          strMailBody=Ebody_1.evaluate().getContent();
          //Logger.log(strMailBody);
         
          //strMailBody=strMailBodyTemp_1.replace("{specific task}",strTask).replace("{date}",strDate);
          //GmailApp.sendEmail(strTo,strSubject_1,strMailBody,{cc:strCC});
          //GmailApp.sendEmail({to:strTo,subject:strSubject_1,htmlBody:strMailBody, inlineImages:Eimage_1, cc:strCC}); GmailApp didn't work therefore used MailApp
          MailApp.sendEmail({to:strTo,subject:strSubject_1,htmlBody:strMailBody, inlineImages:Eimage_1, cc:strCC});
          sht_DesignTeam.getRange(i,5).setValue('Email Sent');
        }
        //******Reminder**********
        else if(sht_DesignTeam.getRange('G1').getValue()==2 &&
        sht_DesignTeam.getRange(i,6).getValue()!='Email Sent' &&
        sht_DesignTeam.getRange(i,7).getValue()!='Completed'){
          strTask=sht_DesignTeam.getRange(i,1).getValue();
          strDate=Utilities.formatDate(sht_DesignTeam.getRange(i,2).getValue(),"GMT+5:30","dd-MMM-YYYY");
          strTo=sht_DesignTeam.getRange(i,3).getValue();
          strCC=sht_DesignTeam.getRange(i,4).getValue();

          name2="X"
          Ebody_2.name=name2;
          Ebody_2.task=strTask;
          Ebody_2.date=strDate;
          strMailBody=Ebody_2.evaluate().getContent();

          MailApp.sendEmail({to:strTo,subject:strSubject_2,htmlBody:strMailBody, inlineImages:Eimage_2, cc:strCC});
          sht_DesignTeam.getRange(i,6).setValue('Email Sent');
        }
        //******Task Completition**********
        if(sht_DesignTeam.getRange(i,7).getValue()=='Completed' &&
        sht_DesignTeam.getRange(i,8).getValue()!='Email Sent'){
          strTo=sht_DesignTeam.getRange(i,3).getValue();
          strCC=sht_DesignTeam.getRange(i,4).getValue();

          name3="X"
          Ebody_3.name=name3;
          strMailBody=Ebody_3.evaluate().getContent();

          MailApp.sendEmail({to:strTo,subject:strSubject_3,htmlBody:strMailBody, inlineImages:Eimage_3, cc:strCC});
          sht_DesignTeam.getRange(i,8).setValue('Email Sent');
        }
        //******Task Pending**********
        if(sht_DesignTeam.getRange('H1').getValue()==4 &&
        sht_DesignTeam.getRange(i,7).getValue()!='Completed'){
          strTask=sht_DesignTeam.getRange(i,1).getValue();
          strTo=sht_DesignTeam.getRange(i,3).getValue();
          strCC=sht_DesignTeam.getRange(i,4).getValue();

          name4="X";
          Ebody_4.name=name4;
          Ebody_4.task=strTask;
          strMailBody=Ebody_4.evaluate().getContent();

          MailApp.sendEmail({to:strTo,subject:strSubject_4,htmlBody:strMailBody, inlineImages:Eimage_4, cc:strCC});
          sht_DesignTeam.getRange(i,9).setValue('Email Sent');
        }
        //******Pending long time**********
        else if(sht_DesignTeam.getRange('H1').getValue()>=14 &&
        sht_DesignTeam.getRange(i,7).getValue()!='Completed'){
          strTask=sht_DesignTeam.getRange(i,1).getValue();
          //strDate=Utilities.formatDate(sht_DesignTeam.getRange(i,2).getValue(),"GMT+5:30","dd-MMM-YYYY");
          strTo=sht_DesignTeam.getRange(i,3).getValue();
          strCC=sht_DesignTeam.getRange(i,4).getValue();

          name5="X";
          Ebody_5.name=name5;
          Ebody_5.task=strTask;
          strMailBody=Ebody_5.evaluate().getContent();

          MailApp.sendEmail({to:strTo,subject:strSubject_5,htmlBody:strMailBody, inlineImages:Eimage_5, cc:strCC});
          sht_DesignTeam.getRange(i,10).setValue('Email Sent');
        }
      }
    }
  }
}
HTML Template
<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <p>Dear <b><?=name?></b>,
    <br><br>
    I hope this email finds you well. This is a friendly reminder that the deadline for <b><?=task?></b> is fast approaching, with just 7 days left to complete, which is <b><?=date?></b>.
    <br>
    Please take a moment to review your outstanding tasks and prioritize them accordingly.
    <br><br>
    <img src="cid:Logo" alt="Img: Deadline Approaching, Please Check." style="width:150px;height:125px;">
    <br><br>
    <b>Best Regards</b>
    </p>

  </body>
</html>

Comments

Popular posts from this blog

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

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