Google Apps Script - Send Gmail from Google Sheets using Custom HTML Template
Tekan Tombol Ctrl dan + pada Keyboard secara Bersama-sama
Deskripsi [Tampilkan | Sembunyikan]
One of the most commonly asked for Google Apps Script Projects is How to Send custom HTML emails using Apps Script. This lesson will demonstrate how to code in Google Apps Script and create functionality to send custom HTML templated emails to a list of users within your Google Spreadsheet data. Also updating the spreadsheet data.Source Code [Tampilkan | Sembunyikan]
https://github.com/lsvekis/Google-Apps-Script/tree/main/Apps%20Script%20EmailerSource Code Google Apps Script[Tampilkan | Sembunyikan]
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu('send email').addItem('Approve', 'approver').addToUi()
}
function approver() {
const ui = SpreadsheetApp.getUi();
const row = SpreadsheetApp.getActiveSheet().getActiveCell().getRow();
Logger.log(row);
const data = SpreadsheetApp.getActiveSheet().getRange(row, 1, 1, 3).getValues()[0];
const user = {
first: data[0]
, last: data[1]
, email: data[2]
, row: row
};
Logger.log(user);
const res = ui.alert('Send to ' + user.first + '(' + user.email + ')?', ui.ButtonSet.YES_NO);
if (res == ui.Button.YES) {
sendUser(user);
}
Logger.log(res);
}
function sendUser(user) {
//let message = '<h1>Hello World</h1>';
const temp = HtmlService.createTemplateFromFile('temp');
temp.user = user;
const message = temp.evaluate().getContent();
MailApp.sendEmail({
to: user.email
, subject: 'Tester'
, htmlBody: message
});
SpreadsheetApp.getActiveSheet().getRange(user.row, 4).setValue('sent');
}
Source Code HTML [Tampilkan | Sembunyikan]
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<p style="font-family: fantasy;color:red;font-size:3em">Hi, <?= user.first ?></p>
<div>You have been approved</div>
<div> Congrats .... <?= user.first ?> <?= user.last ?></div>
</body>
</html>
Review [Tampilkan | Sembunyikan]
Kelebihan:- Bisa Kirim Gmail dari Google Sheets
- Tanpa AddOn
Kekurangan:
- Hanya bisa satu per satu. Tidak bisa langsung banyak.
Tidak ada komentar