LoginSignup
2
0

More than 3 years have passed since last update.

node.js + express + nodemailer

Last updated at Posted at 2021-01-03

node.jsでmail機能を実装するには…

node.jsでmail機能を実装するにあたってnpmのnodemailerというパッケージを使っていきます!

$ express app --view=ejs
$ cd app
$ npm install

ルートフォルダーにcontact.jsファイルを作って下記の記述をします

contact.js
"use strict";
const nodemailer = require("nodemailer");
async function main() {

  let transporter = nodemailer.createTransport({
    ignoreTLS:true,
    port: 1025,
    secure: false, // true for 465, false for other ports
  });

  let info = await transporter.sendMail({
    from: '"Fred Foo 👻" <foo@example.com>', // sender address
    to: " s@gmail.com", // list of receivers
    subject: "成功!!!!", // Subject line
    text: "サンプルテキストです", // plain text body
  });

  console.log("送られたメッセージ: %s", info.messageId);

}

main().catch(console.error);


参考)https://nodemailer.com/about/

メールが送信されているかをチェックするためにmailcatcherを使います
mailcatcherを使うためのパッケージをインストールして起動してみます

$ npm install -g maildev
$ maildev

image.png

$ node contact.js

localhostの1080ポートが起動しているのでhttp://127.0.0.1:1080 にアクセスすると

image.png

image.png

ポート1025でメールを受け取ることができました!

あとがき

mail機能を実装するためにはamazon SESでもできるようです
今回nodemailerを使ってみましたが、圧倒的にメールサーバについての理解が乏しいと実感しました(´;ω;`)
mailについて学習し、また記事を書きます!

2
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
2
0