node.js で取敢えずメール送信

#!/usr/bin/env node
'use strict';

// *********************** //
// メール送信
// body    : 送信データ本文文字列
// mail_to : 送信先
// 戻り値  : 無し
//
// 参考 URL:
// Nodemailer : https://nodemailer.com/
//              https://nodemailer.com/about/
// tls:{rejectUnauthorized: false} : 
//   https://nodemailer.com/smtp#tls-options
// *********************** //
function sendMail(body, mail_to = null) {
  console.log('function sendMail');
  try {
    const nodemailer = require('nodemailer'); // ライブラリ
    // メールサーバ設定
    const transporter = nodemailer.createTransport({  
      host  : 'smtp123.jp',  // メールサーバ
      port  : '587',         // ポート SSL:465  TLS:587
      secure: false,         // true = SSL
      tls   : { rejectUnauthorized: false },
      auth  : {
        user: 'smtp@smtp123.jp', // メールアカウント
        pass: 'smtp_pass',       // メールパスワード
      }, // auth
    }); // nodemailer.createTransport

    let mailOptions = {
      from   : 'smtp@smtp123.jp',  // 送信元アドレス
      to     : mail_to,            // 送信先アドレス
      subject: 'Subject',          // タイトル
      text   : body,               // 本文
      html   : '',                 // html 使用の本文
                                   // e.g. : <b>Html本文</b>
    }; // mailOptions
    mailOptions.attachments = [];  // 添付ファイル初期化

    // 送信実行
    transporter.sendMail(mailOptions, function(error, info) {
      console.log('info:', info);
    }); // transporter.sendMail
  } catch (e) {
    console.error('sendMail', e.message);
  } // try catch
} // sendMail()

// *********************** //
// 実行
// 記述した時点で実行する即時関数
// (function () { 何かの処理 }());
// https://qiita.com/katsukii/items/cfe9fd968ba0db603b1e
// *********************** //
(function () {
    sendMail('123456Test', 'smtp@hoge.jp');
}());

コメント

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です