GCP/Apps Script
Apps Script로 Email 보내기 (HTML 양식)
whistory
2023. 1. 11. 09:58
반응형
이메일을 보내는 기능을 구현한다.
이메일의 경우는, Google Sheets로 관리되는 거래내역이나 급여내역 등을
다수 전송 할 경우 사용 할 수 있을것이다.
템플릿을 만들고, Google Sheets의 데이터들을 붙여넣어 보낼 수 있을것이다.
단 발신자는 AppsScript의 생성자 계정으로 변경 할 수 없다.
Apps Script 의 이메일 전송 함수
/**
* Send Email.
* 이메일을 이용한 Notify.
* @param {string} recipient = 받는사람
* @param {string} subject = 제목
* @param {string} body = 내용
* Ref = <https://spreadsheet.dev/send-html-email-from-google-sheets>
*/
function sendEmail(recipient, subject, body) {
var recipient = "whiseung@naver.com";
var subject = "Apps Script Email Test";
var htmlTemplate = HtmlService.createTemplateFromFile("mail1.html");
htmlTemplate.name = "Whiseung";
htmlTemplate.data = "Apps Script Email Test Body";
const htmlBody = htmlTemplate.evaluate().getContent();
MailApp.sendEmail({
to: recipient,
subject: subject,
htmlBody: htmlBody
});
}
사용할 HTML 생성
<?= parameter ?> 형태로 gs 파일에서 파라미터를 전달받는다.
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<p>
Hello <?= name ?>
</p>
<p><strong>This is <?= data ?></strong></p>
<p>Thanks.</p>
</body>
</html>
이메일 전송 완료
name 과 data만 변경하면, 다른 내용의 메일을 간단히 보낼 수 있다.
/**
* Send Email.
* 이메일을 이용한 Notify.
* @param {string} recipient = 받는사람
* @param {string} subject = 제목
* @param {string} body = 내용
* Ref = <https://spreadsheet.dev/send-html-email-from-google-sheets>
*/
function sendEmail(recipient, subject, body) {
var recipient = "whiseung@naver.com";
var subject = "Apps Script Email Test";
var htmlTemplate = HtmlService.createTemplateFromFile("mail1.html");
htmlTemplate.name = "Whiseung";
htmlTemplate.data = "40,000";
const htmlBody = htmlTemplate.evaluate().getContent();
MailApp.sendEmail({
to: recipient,
subject: subject,
htmlBody: htmlBody
});
}
/**
* Send Email.
* 이메일을 이용한 Notify.
* @param {string} recipient = 받는사람
* @param {string} subject = 제목
* @param {string} body = 내용
* Ref = <https://spreadsheet.dev/send-html-email-from-google-sheets>
*/
function sendEmail(recipient, subject, body) {
var recipient = "whiseung@naver.com";
var subject = "Apps Script Email Test";
var htmlTemplate = HtmlService.createTemplateFromFile("mail1.html");
htmlTemplate.name = "Tommy";
htmlTemplate.data = "1,000";
const htmlBody = htmlTemplate.evaluate().getContent();
MailApp.sendEmail({
to: recipient,
subject: subject,
htmlBody: htmlBody
});
}
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<p>
Hello <?= name ?>
</p>
<p><strong>Your salary this month is <?= data ?>.</strong></p>
<p>Thanks.</p>
</body>
</html>
반응형