สร้างเทมเพลทอีเมลแบบองค์กร ก่อนส่งด้วย Google Script (เรียบหรูดูแพง)

ในการส่งอีเมลด้วย Google Script นั้นหลายท่านอาจยังไม่ทราบว่าเราสามารถสร้าง Template Email ให้ดูดีมีระดับแบบฉบับองค์กรใช้กันได้เลยนะครับ ไปลุยกันเลยครับ 🙂 🙂 🙂

// Presented by BrilliantPy

/*######################### Editable1 Start #########################*/
let sheetName = "การตอบแบบฟอร์ม 1";
let indexCol = {"ประทับเวลา":0,"ชื่อ - สกุล":1,"อีเมล":2,"send_status":3}
let emailCol = indexCol["อีเมล"];
let statusCol = indexCol["send_status"];
let emailSubject = "แบบฟอร์มใบลาสำหรับพนักงาน";
let logoUrl = "https://brilliantpy.com/wp-content/uploads/2021/07/cropped-logo_circle_v2_whitebg.jpg";
let addressFooterEmail = "72 Phahon Yothin Road, Sam Sen Nai, Phaya Thai, BKK 10400 THA";
/*#########################  Editable1 End  #########################*/

// Init
let sentStatus = "SENT";
let ss,sheet,lastRow,lastCol,range,values,title;
let curData;

async function templateEmail() {
/*######################### Editable2 Start #########################*/
  function getBodyEmail() {
    let result = `คุณ ${curData[indexCol['ชื่อ - สกุล']]} แผนก Dev ได้ยื่นคำร้องขอ ลากิจ ตั้งแต่ 13/09/2022 จนถึง 14/09/2022\nจึงเรียนแจ้งเพื่อขออนุมัติการลา กรุณากดที่ลิงก์เพื่ออนุมัติการลา`;
    return result;
  }
/*#########################  Editable2 End  #########################*/
  await initSpreadSheet();
  for (let i=0;i<lastRow;i++) {
    if (i == 0) {
      continue;
    }
    curData = values[i];
    let curStatus = curData[statusCol];
    let curEmail = curData[emailCol];
    let numRow = i+1;
    if (curStatus == sentStatus) {
      continue;
    }
    // console.log(renderHtmlBody())
    await sendEmail(curEmail,numRow);
  }

  function renderHtmlBody() {
    let result = "";
    result = `<div style="max-width: 600px;text-align: center;margin: auto;color: rgb(34,34,34);">
	<img style="width: 80px;height: 80px;max-width: 80px;max-height: 80px;border-radius: 10px;background-color: white;margin: auto;" class="logo" src="${logoUrl}">
	<div style="font-size: 26px;padding-top: 30px;color: rgb(34,34,34);">${emailSubject}</div>
	<p style="font-size: 16px;color: rgb(34,34,34);">${getBodyEmail()}</p>
	<div style="text-align: center;color: black">
      <a href="#" style="background-color: #4CAF50;
        width: 150px;
        color: white;
        padding: 16px 32px;
        text-decoration: none;
        display: inline-block;
        font-size: 16px;
        margin: 4px 25px;
        cursor: pointer;
        border-radius: 0.25rem;">อนุมัติ</a>
      <a href="#" style="background-color: #f44336;
        width: 150px;
        color: white;
        padding: 16px 32px;
        text-decoration: none;
        display: inline-block;
        font-size: 16px;
        margin: 4px 25px;
        cursor: pointer;
        border-radius: 0.25rem;">ไม่อนุมัติ</a>
    </div>
    <div style="padding-top: 30px;color:rgb(34,34,34);">© 2022 All Right Reserved • Terms and Privacy<br>${addressFooterEmail}</div>
</div>`;
    return result;
  }

  function sendEmail(email,numRow) {
    return new Promise(function(resolve,reject) {
      try {
        MailApp.sendEmail({
          to: email,
          subject: emailSubject,
          htmlBody: renderHtmlBody(),
        });
        sheet.getRange(numRow,toNumCol(statusCol)).setValue(sentStatus);
        resolve();
        console.log('sendEmail completed')
      } catch(e) {
        reject(`[Error]:sendEmail error with email (${email}),${e}`);
      }
    });
  }
}

async function initSpreadSheet() {
  return new Promise(function(resolve) {
    ss = SpreadsheetApp.getActive();
    sheet = ss.getSheetByName(sheetName);
    lastRow = sheet.getLastRow();
    lastCol = sheet.getLastColumn();
    range = sheet.getDataRange();
    values = range.getValues();
    title = values[0];
    resolve();
    console.log('initSpreadSheet completed');
  });
}

function toNumCol(indexCol) {
  return indexCol+1;
}

20 thoughts on “สร้างเทมเพลทอีเมลแบบองค์กร ก่อนส่งด้วย Google Script (เรียบหรูดูแพง)

  1. zmozero teriloren says:

    Thanks for sharing superb informations. Your web site is so cool. I’m impressed by the details that you have on this web site. It reveals how nicely you perceive this subject. Bookmarked this web page, will come back for more articles. You, my pal, ROCK! I found just the info I already searched all over the place and just could not come across. What a perfect site.

ใส่ความเห็น

อีเมลของคุณจะไม่แสดงให้คนอื่นเห็น ช่องข้อมูลจำเป็นถูกทำเครื่องหมาย *