GS: To send a HTML formatted email from Gmail

If using <Google Labs>, simple image can be inserted to the body of mail, but we can't additional html code because it is basically supported plain text. Above all, if you use Gmail for business purposes and want to send DM for marketing purposes, it is necessary to inserting HTML.

It is simple google script that is notify by email to editors of spreadsheet on google drive.

[sourcecode language="javascript"]

var sheet = SpreadsheetApp.getActiveSheet();
var spread = SpreadsheetApp.getActiveSpreadsheet();

[/sourcecode]

The spreadsheet docs and sheet you want to edit are declared as the global variable.

[sourcecode language="javascript"]

function notification(){
var allUser = spread.getEditors();
//var allUser = spread.getViewers(); <- they have only view-permission.
var spreadURL = spread.getUrl();
var sheetName = sheet.getName();
var spreadName = SpreadsheetApp.getActiveSpreadsheet().getName();

MailApp.sendEmail(
allUser, //Editors. Of course, you can input email-address with format([email protected]).
"[Google Docs] "+spreadName+" has been updated." , // the title of mail
"",
{
htmlBody: // the body of mail. it must be written by HTML.
spreadName +"'s"+ sheetName+" has been updated. Please check at the address below. <br><br> ▶ "+spreadURL

}
);
}
[/sourcecode]

If the sheet of spreadsheet is updated, run this method at [Menu]>[Tools]>[Script Manager]. Then, all user will be receive notification.

[sourcecode language="javascript"]
{
htmlBody:
// insert html source
}
[/sourcecode]

PS. If you want to send DM, you need to use macro, so use [for(;;)] with sorted contents of sheet. (ex-name, mail, contents..)

[GoogleScript] HTML 형식으로 Gmail보내기 

메일 중 갑은 Gmail 이라는 말이 있다. (역시 구글신...이라지)

대화형식보기, 온갖 커스텀 기능들(이라고 쓰고 구글실험실이라고 읽는다)도 제공한다.

물론 구글실험실에서는 <본문에 이미지 삽입> 이라는 기능도 있긴 하지만, HTML 로 디테일하게 코딩해서 보내는건 제공하지 않는다.

gmail 본문에서는 plainText만 지원하기 때문인데, 업무용으로 gmail을 주로 사용하거나, 마케팅 목적으로 DM등을 발송하거나 할 때는 좀 짜증나는 일이 아니다.

아래 코드는 메일 노티피를 보내는 간단한 소스.

[sourcecode language="javascript"]

var sheet = SpreadsheetApp.getActiveSheet();
var spread = SpreadsheetApp.getActiveSpreadsheet();

[/sourcecode]

해당 스프레드시트의 문서와 활성화 시트를 전역변수로 선언해주자. 코드가 길어지거나 하면 쟤네들은 수시로 호출하게 된다.

[sourcecode language="javascript"]

function notification(){
var allUser = spread.getEditors();
//var allUser = spread.getViewers(); <- they have only view-permission.
var spreadURL = spread.getUrl();
var sheetName = sheet.getName();
var spreadName = SpreadsheetApp.getActiveSpreadsheet().getName();

MailApp.sendEmail(
allUser, //Editors. Of course, you can input email-address with format([email protected]).
"[Google Docs] "+spreadName+" has been updated." , // the title of mail
"",
{
htmlBody: // the body of mail. it must be written by HTML.
spreadName +"'s"+ sheetName+" has been updated. Please check at the address below. <br><br> ▶ "+spreadURL

}
);
}
[/sourcecode]

메일 노티피케이션 소스이기 때문에 문서의 시트가 업데이트 되었을 때, 해당 메소드를 실행해주면 보기/수정 권한이 있는 모든 공유자들에게 메일이 발송된다.

[sourcecode language="javascript"]
{
htmlBody:
// insert html source
}
[/sourcecode]

이 부분에는 html 테이블 등을 짜서, 소스만 캡쳐해서 넣어주면 HTML로 아름답게 꾸며진 메일로 깔끔하게 보낼 수 있다.

PS. 물론 DM발송에는 메일 주소 등을 매크로로 돌려야 할테고, 그건 for문을 이용해서 시트를 읽어서 주루룩~ 넣어주면 편리하게 처리할 수 있다.

적게 일하고 많이 버는 법을 늘 고민합니다. 일이 되게 하는 것에 간혹 목숨을 겁니다. 지금은 우아한형제들과 함께 일하고 있어요.