🍣

node.jsで、mail送信する例 nodemailer

2021/04/22に公開

概要:

node.jsで、mail送信のメモとなります


環境

  • node 14
  • npm / nodemailer

関連

https://www.npmjs.com/package/nodemailer

https://qiita.com/kashih/items/59e1c0a8c3b1e483ecbe


参考のコード

https://gist.github.com/kuc-arc-f/8575c5e96a9f48d6a217e6397244f7e6

test4_async_pub.js

"use strict";
const nodemailer = require("nodemailer");

var receiverEmailAddress = 'to123@hoge.com'
var senderEmailAddress = 'from123@hoge.com'

async function smtp_send() {
  let transporter = nodemailer.createTransport({
    host: "smtp.hoge.com",
    port: 465,
    secure: true, // true for 465, false for other ports
    auth: {
      user: "hoge", // generated ethereal user
      pass: "1234", // generated ethereal password
    },
  });
  // send mail with defined transport object
  let info = await transporter.sendMail({
    from: senderEmailAddress, // sender address
    to: receiverEmailAddress, // list of receivers
    subject: "テスト用メールとなります 4", // Subject line
    text: "テスト用メールとなります 4 BODY", // plain text body
    html: "<p>テスト用メールとなります 4 HTML</p>", // html body
  });
  console.log("Message sent: %s", info.messageId);
  console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
}
//
smtp_send().catch(console.error);


  • transporter.createTransport , 設定

auth / smtp user, smtp pass 設定

host, port, secure 設定

  • transporter.sendMail

from : from mail address, To : To mail address

subject: subject , text: mail text

  • text, HTMLメールに対応しているようです

....

Discussion