速攻NodeJSで簡単メール送信

NodeJSでメール送信

環境

  • モジュールはnodemailerを使用します。
  • SMTPはGメールを使用します。

ソース

mail.js
var receiverEmailAddress = '{宛先アドレス}@*****.com'
var senderEmailAddress = '{作成したGメール}@gmail.com'
var senderEmailPassword = '{作成したGメールアカウントのパスワード}'

var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
  host: 'smtp.gmail.com',
  port: 465,
  secure: true, // SSL
  auth: {
    user: senderEmailAddress,
    pass: senderEmailPassword
  }
});
var mailOptions1 = {
  from: senderEmailAddress,
  to: receiverEmailAddress,
  subject: '{件名}',
  text: '{本文}'
};
transporter.sendMail(mailOptions1, function (error, info) {
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent: ' + info.response);
  }
});

使用方法

  1. モジュールのインストールnpm install nodemailer
  2. スクリプト実行node mail.js

結果

Email sent: 250 2.0.0 OK ******* f11sm5251981pfd.27 - gsmtp

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account