LoginSignup
0
0

More than 3 years have passed since last update.

Async/Awaitを使ったNodemailerでのメール送信

Posted at

概要

  • 速攻NodeJSで簡単メール送信」を参考に、ES7のasync/awaitを使ったNode.jsでメールを送信するロジックの自分用メモです。
  • 最近はGmailのSMTPサーバを間借りするとエラーとなるようなので、Lolipopのメールサーバで送信できることを確認しました。

前提

  1. Node.jsのプロジェクト作成済みであること
  2. nodemailerのパッケージが追加済みであること
npm install nodemailer

# このサンプルではnodemailerのv6.3.0を使用しています。同じバージョンを指定する場合は下記コマンドで
# npm install nodemailer@6.3.0

ソース

index.js
const nodemailer = require('nodemailer');

(async () => {
    // 宛先アドレス
    const receiverEmailAddress = 'receiver@example.com'
    // 送信者アドレス(smtpサーバのログインアドレス)
    const senderEmailAddress = 'sender@example.com'
    // 送信者のログインパスワード
    const senderEmailPassword = 'SMTP Password'

    const transporter = nodemailer.createTransport({
        // SMTPサーバのホスト
        host: 'smtp.lolipop.jp',
        port: 465,
        secure: true, // SSL
        auth: {
            user: senderEmailAddress,
            pass: senderEmailPassword
        }
    });

    // メールの内容
    const mailOptions1 = {
        from: senderEmailAddress,
        to: receiverEmailAddress,
        subject: '{件名}',
        text: '{本文}'
    };
    try{
        // 送信
        const result = await transporter.sendMail(mailOptions1);
        console.log(result);
    }catch(e){
        console.error(e);
    }
})();
0
0
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
0
0