6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Googleフォームの自動返信メールのサンプルスクリプト

Last updated at Posted at 2017-02-09

Googleフォームの自動返信スクリプトのサンプルです。

function sendMailFromForm() {
    var title = "******";    //メールタイトル
    var admin_name = "******"; //送信者名
    //------------------------------------------------------------
    // 設定エリアここから
    //------------------------------------------------------------
    // 件名、本文、フッター
    var subject = "" + title + "】控えメール";
    // メール送信先
    var admin = "";    // 管理者メールアドレス(必須)
    var cc    = "";    // Ccに入れるメールアドレス
    var bcc   = ""; // Bccに入れるメールアドレス:
    var reply = ""; // Reply-Toに入れるメールアドレス:
    var to    = "";    // To: (入力者のアドレスが自動で入ります)

    //------------------------------------------------------------
    // 設定エリアここまで
    //------------------------------------------------------------

    try{
        // スプレッドシートの操作
        var sh   = SpreadsheetApp.getActiveSheet();
        var rows = sh.getLastRow();
        var cols = sh.getLastColumn();
        var rg   = sh.getDataRange();

        var dataList = {};

        // メール件名・本文作成と送信先メールアドレス取得
        for (var j = 1; j <= cols; j++ ) {
            var col_name  = rg.getCell(1, j).getValue();    // カラム名
            var col_value = rg.getCell(rows, j).getValue(); // 入力値
            dataList[col_name] = col_value;
        }
        to = dataList["メールアドレス"];
        var sei = dataList["お名前(姓)"];

        var body
        = sei + "さん\n"

    body+= "\n"
        + "ここに本文を入力します。\n"
        + "ここに本文を入力します。\n"
        + "ここに本文を入力します。\n"
        + "===================================\n"
        + admin_name + "\n"
        + "\n"
        + "";

        // 送信先オプション
        var options = {};
        if ( cc )    options.cc      = cc;
        if ( bcc )   options.bcc     = bcc;
        if ( reply ) options.replyTo = reply;

        // メール送信
        if ( to ) {
            MailApp.sendEmail(to, subject, body, options);
        }else{
            MailApp.sendEmail(admin, "【失敗】Googleフォームにメールアドレスが指定されていません", body);
        }
    }catch(e){
        MailApp.sendEmail(admin, "【失敗】Googleフォームからメール送信中にエラーが発生", e.message);
    } 
}
6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?