
GASメール一斉送信ツール
GASでHTMLメール一斉配信するやつの覚書(2022年7月11日時点で有効なコード)
スプレッドシート(以降SS)を用意する
配信用のコードを書く
スプレッドシートのメニューバーに配信用ボタンを配置
1.配信用に配信リストと配信内容を作成

A1~B2までは配信内容、3行目以降は宛先等で使用。
2.配信用のコードを書く
SSメニューバーにて「拡張機能」→「Apps Script」の順に選択しプロジェクト作成画面へ遷移。コード.gsというファイルに直接書き込んでいく。
/*配信用のコード*/
function createEmailsDraft2(){
const spreadsheet = SpreadsheetApp.getActive();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName("シート1"));
const sheet = SpreadsheetApp.getActiveSheet();
const lastRow = sheet.getLastRow();
/*3列目~最終行までを取得*/
/*const内(x,y)はそれぞれx=行、y=列で数字表記*/
for(let i = 4; i <= lastRow; i++){
const company = sheet.getRange(i, 1).getValue();
const cid = sheet.getRange(i, 3).getValue();
const to = sheet.getRange(i, 2).getValue();
const subject = sheet.getRange(1, 2).getValue();
const non = 'nonText'
const message = sheet.getRange(2, 2).getValue()
/*B2セルの{社名}を上記で定義したcompanyに置き換えるcidも同様*/
.replace('{社名}',company)
.replace('{cid}',cid);
/*htmlメールとして指定*/
const options = {"htmlBody":message}
/*送信*/
GmailApp.sendEmail(to, subject, non, options);
}
}
置き換えについては、SSのB2セルでHTML形式に記載した本文中に{社名}などとすることで該当するものに置換。最終行sendEmailについての引数は公式ドキュメント参照が確実。プロジェクト内の「実行」でテストメールが送れるか試してみたらよい。
※初めて扱う場合には、Googleのセキュリティ認証が必要なので翻訳しながらOK!OK!許可!と進む。
3.SSのメニューバーに配信ボタンを追加する
毎回開くのが面倒なだけなので省略可。

/*SSを開いた瞬間に動作するのがonOpen*/
function onOpen() {
const spreadsheet = SpreadsheetApp.getActive();
/*「メール配信」メニュー内の選択肢として「ノーマル」を作成
これを押すと先に作成した「createEmailsDraft2」が動いてメールが配信される*/
const menuItems = [
{name:'ノーマル', functionName:"createEmailsDraft2"},
];
/*「メール配信」というメニューをメニューバーに追加*/
spreadsheet.addMenu('メール配信', menuItems);
}
まとめ 全体構造
上記のコードをまとめるとこんな感じ。
/*コード.js*/
function onOpen() {
const spreadsheet = SpreadsheetApp.getActive();
const menuItems = [
{name:'ノーマル', functionName:"createEmailsDraft2"},
];
spreadsheet.addMenu('メール配信', menuItems);
}
function createEmailsDraft2(){
const spreadsheet = SpreadsheetApp.getActive();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName("シート1"));
const sheet = SpreadsheetApp.getActiveSheet();
const lastRow = sheet.getLastRow();
for(let i = 4; i <= lastRow; i++){
const company = sheet.getRange(i, 1).getValue();
const cid = sheet.getRange(i, 3).getValue();
const to = sheet.getRange(i, 2).getValue();
const subject = sheet.getRange(1, 2).getValue();
const non = 'nonText'
const message = sheet.getRange(2, 2).getValue()
.replace('{社名}',company)
.replace('{cid}',cid);
const options = {"htmlBody":message}
GmailApp.sendEmail(to, subject, non, options);
}
}
function createEmailsDraft2()に続いて、いろいろ書き足していくことが可能。都度、メニューにも追加して名前を付けておくと便利。
おまけ 開封率を取得する方法
UA(Googleアナリティクス)が使えなくなるまでだが開封率の取得が可能。ヒットビルダーで諸々設定し、cidには重複無しの乱数でも突っ込んでおけば開封回数と開封ドメイン数(=社数)が確認できる。
設定すべき項目
t=event
cid=aaaaaa(とかにしておいて後で乱数に置き換え)
以下はAdd PARAMETERにて追加設定
ec=testmail(GAに表示されるイベント名)
ea=open(アクション名称)
以下は無くても良いが、ちゃんと使うと比較解析に便利
dp=%2Ftestmail(ディレクトリ都合、「/」は「%2F」と書く)
cn=testmail(キャンペーン名称、、、だったと思う)
設定が完了したら画面上部のオレンジボタン、VALIDATE HITを押して開通確認。OKなら「v=~~~~~~」を●●●●●●●部分に埋め込みSSのC列に入れておくだけ。
<img src="http://www.google-analytics.com/collect?●●●●●●●">
あとはSS上でcidで仮設定したaaaaaa部分を重複しない英数字に一括で置き換え。GAから開封率を時間ごと、日毎に追えます。