LoginSignup
6
8

More than 3 years have passed since last update.

Node.js Gmail送信

Last updated at Posted at 2019-12-28

はじめに

Gmailアカウントでメール送信をしたい場合があります。
gmail-sendを利用すると便利です。

gmail-sendをインストール

npm install --save gmail-send

送信アカウントの設定

const send = require('gmail-send')({
  user: 'xxx@gmail.com',
  pass: 'xxxアカウントのパスワード',
  to:   'taro@yahoo.co.jp',
  subject: 'Gmailからのメール送信確認の件',
});

テキストメールを送信する

send({
  text:    '●●様 \n こちらは2行目です。\n',  
}, (error, result, fullResult) => {
  if (error) console.error(error);
  console.log(result);
})

HTML形式メールを送信する

send({
  html:    '●●様 <br/> こちらは2行目です。<br/>',  
}, (error, result, fullResult) => {
  if (error) console.error(error);
  console.log(result);
})

ファイル添付などの書き方はGithubのサイトで確認できます。
https://github.com/alykoshin/gmail-send

Gmailのセキュリティ変更

Gmailのアカウントはソースからの送信は許可しないと、エラーになります。
image.png

安全性の低いアプリのアクセスにオフの場合はオンにする必要があります。
image.png

これでエラーが回避できますので、パスワードの管理は十分注意が必要です。
またGmail送信アカウントのパスワードを変更した場合、ソース側のパスワードも忘れずに設定する必要があります。

AWSの場合セキュリティグループの設定

Gmailアカウントで送信したい場合、456ポートを許可する必要があります。

参考URL:https://github.com/alykoshin/gmail-send

以上

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