12
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Node.jsでGmailを送信

Posted at

Nodemailerを使ってGmailを送信してみる。
Gmailを使う場合は二段階認証がOFFで「安全性の低いアプリの許可」を有効にするのが一番簡単だった。

安全性の低いアプリのアクセス

nodemailerのインストール

npm i -D nodemailer

server.jsの用意

//server.js
var nodemailer = require('nodemailer')

// メッセージ
var message = {
  from    : '送信元アドレス',
  to      : '送信先アドレス',
  subject : 'タイトル',
  text    : '本文'
};

var smtpConfig = {
  host: 'smtp.gmail.com',
  port: 465,
  secure: true, // SSL
  auth: {
    user : 'hogehoge@gmail.com,
    pass : 'パスワード'
  }
};

var transporter = nodemailer.createTransport(smtpConfig);

transporter.sendMail(message, function(err, response) {
  console.log(err || response);
});

server.jsの実行

node server.js
12
8
1

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
12
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?