メール送信画面を、Google Apps ScriptとVue.jsとBootstrapで作る

# index.html

```ruby:index.html
<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <?!= HtmlService.createHtmlOutputFromFile('css').getContent(); ?>
    <?!= HtmlService.createHtmlOutputFromFile('customcss').getContent(); ?>

  </head>
  <body>

  <nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top">
    <a class="navbar-brand" href="#">Navbar</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>

  <div class="collapse navbar-collapse" id="navbarsExampleDefault">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
      </li>
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="#" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</a>
        <div class="dropdown-menu" aria-labelledby="dropdown01">
          <a class="dropdown-item" href="#">Action</a>
          <a class="dropdown-item" href="#">Another action</a>
          <a class="dropdown-item" href="#">Something else here</a>
        </div>
      </li>
    </ul>
    <form class="form-inline my-2 my-lg-0">
      <input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
      <button class="btn btn-secondary my-2 my-sm-0" type="submit">Search</button>
    </form>
  </div>
  </nav>

  <main role="main" class="container">
    <div id="app">

    <div class="starter-template">
      <h1>メール送信</h1>
      <p class="lead">件名と本文と宛先を入力して送信ボタンをクリックすると宛先にメールを送信します。</p>
    </div><!-- /.starter-template -->

    

    <div class="card">
      <div class="card-body">
         <div class="form-group">
           <label for="subject">subject</label>
           <input class="form-control" type="text" id="subject" placeholder="件名を入力してください" v-model.trim="subject"/>
         </div>
         <div class="form-group">
           <label for="body">body</label>
           <textarea class="form-control" id="body" placeholder="本文を入力してください" rows="5" v-model.trim="body"></textarea>
         </div>
         <div class="form-group">
           <label for="email">Email address</label>
           <input type="email" class="form-control" id="email" placeholder="name@example.com" v-model.trim="email">
         </div>
      </div>
    </div>
    
    <br>
    <button type="button" class="btn btn-primary" v-on:click="checkConfirm">送信する</button>
    
    </div><!-- /.vue.el.app -->
  </main><!-- /.container -->

  <?!= HtmlService.createHtmlOutputFromFile('js').getContent(); ?>
  </body>
  <?!= HtmlService.createHtmlOutputFromFile('vue').getContent(); ?>
</html>
```

# vue.html
```ruby:vue.html
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.0"></script>

<script>  
  var app = new Vue({
    el: '#app',
    data: {
      subject:'',
      body:'',
      email:'',
      setAppData:[],
    },
    methods:{
      checkConfirm: function(){
        this.setData();
        google.script.run
          .withSuccessHandler(function(arg){
            alert("データの登録に成功しました。");
          })
          .withFailureHandler(function(arg){
            console.log(arg);
            alert("データの登録に失敗しました。");
          }).sendEmail(this.setAppData);
        this.clearData();  
      },
      setData: function(){
        this.setAppData = [];
        this.setAppData.push(this.subject);
        this.setAppData.push(this.body);
        this.setAppData.push(this.email);
      },
      clearData: function(){
        this.setAppData = [];
        this.subject = '';
        this.body = '';
        this.email = '';
      },       
    },
    created: function(){
    },
  })
</script>
```

# コード.gs
アプリケーションにアクセスしたユーザーのメールアドレスを取得してCCにセットしているため、
メールはアプリ利用ユーザーにもCCとして送信される。

```ruby:コード.gs
function doGet() {
  var html = HtmlService.createTemplateFromFile("index").evaluate().addMetaTag('viewport', 'width=device-width, initial-scale=1, shrink-to-fit=no');
  return html;
}

function sendEmail(setMail){

  var subject = setMail[0]; //件名
  var body = setMail[1]; //本文
  var mailto = setMail[2]; //宛先
  var cc = Session.getActiveUser().getEmail();
    
  /* メールを送信 */
  GmailApp.sendEmail(
    mailto, //toアドレス
    subject,  //表題
    body,　//本文
    {
      cc: cc,
    }
  );  
}

```

# js.html
```ruby:js.html
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
```

# css.html
```ruby:css.html
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
```

# customcss.html
```ruby:customcss.html
<style>
  body {
    padding-top: 5rem;
  }
  .starter-template {
    padding: 3rem 1.5rem;
    text-align: center;
  }     
  .bd-placeholder-img {
    font-size: 1.125rem;
    text-anchor: middle;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
  }
  @media (min-width: 768px) {
    .bd-placeholder-img-lg {
    font-size: 3.5rem;
     }
  }
</style>
```