Skip to content

Instantly share code, notes, and snippets.

@totuworld
Last active April 10, 2023 14:46
Show Gist options
  • Save totuworld/7efff378cb3ded0fe7976d8f77098031 to your computer and use it in GitHub Desktop.
Save totuworld/7efff378cb3ded0fe7976d8f77098031 to your computer and use it in GitHub Desktop.
이메일 전송용 apps script
function sendEmail() {
const ss = SpreadsheetApp.getActiveSpreadsheet()
const sheet1 = ss.getSheetByName('list'); // 이메일 보낼 사람들 목록이 들어간 시트명!
const sheet2 = ss.getSheetByName('template'); // 이메일 내용이 들어간 시트명!
const subject = sheet2.getRange(2,1).getValue(); // 이메일 제목 추출
const n = sheet1.getLastRow(); // 이메일 보낼 명단이 전체 몇 명인지 확인
// 2번째 행부터 이메일 주소가 있기때문에 2번째 행부터 시작해서 마지막 줄까지 반복하는 구문
for (let i = 2; i < n+1 ; i++ ) {
// 이메일 주소는 2번째 열에 있으니까 (i,2)
const emailAddress = sheet1.getRange(i,2).getValue();
// 이름은 첫번째 열에 있으니까!! (i, 1)
const name = sheet1.getRange(i,1).getValue();
// 이메일 내용이 될부분 추출
let message = sheet2.getRange(2,2).getValue();
// <name> 이라고 써진 부분을 실제 이름으로 갈아치우는 코드!
message = message.replace("<name>",name);
// 메일 가랏!
MailApp.sendEmail(emailAddress, subject, message);
}
}
function sendEmailWithAttachmentsAndInlineImage() {
const recipient = "이메일 받는 사람의 이메일 주소"; // 이메일 받는 사람의 이메일 주소를 입력하세요
const subject = "이메일 제목"; // 이메일 제목을 입력하세요
const body = "이메일 본문에 <img src='cid:myImage'> 이미지를 넣습니다."; // 이메일 본문을 입력하세요. <img src='cid:myImage'> 부분이 이미지가 삽입될 부분입니다.
const imageFile = DriveApp.getFileById("이미지 파일 ID"); // 구글 드라이브에서 이미지 파일의 ID를 입력하세요
const imageBlob = imageFile.getBlob();
const pdfFile = DriveApp.getFileById("PDF 파일 ID"); // 구글 드라이브에서 PDF 파일의 ID를 입력하세요
const pdfBlob = pdfFile.getBlob();
const inlineImages = {};
inlineImages["myImage"] = imageBlob;
MailApp.sendEmail({
to: recipient,
subject: subject,
htmlBody: body,
attachments: [pdfBlob],
inlineImages: inlineImages
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment