0
0

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 3 years have passed since last update.

Node.js: Starttls でメールの送信

Last updated at Posted at 2020-07-05

こちらと同じことを Node.js で行いました。
Python3: Starttls でメールの送信
hi-ho.ne.jp で試しました。

必要なライブラリーのインストール

sudo npm install -g dotenv
sudo npm install -g nodemailer
hi-ho.js
#! /usr/bin/node
//
//	hi-ho.js
//
// ---------------------------------------------------------------
'use strict'

const dotenv = require('dotenv')
const nodemailer = require('nodemailer')

console.error ("*** 開始 ***")

dotenv.config()

const env = {
    server: `${process.env.SERVER}`,
    port: `${process.env.PORT}`,
    usr: `${process.env.USR}`,
    password: `${process.env.PASSWORD}`,
    from: `${process.env.FROM}`,
    to: `${process.env.TO}`,
}

console.log(env.from)
console.log(env.to)

// create reusable transporter object using the default SMTP transport
const url = 'smtps://' + env.usr + ':' + env.password + '@' + env.server 
var transporter = nodemailer.createTransport(url)

// setup e-mail data with unicode symbols
var mailOptions = {
    from: env.from, // sender address
    to: env.to, // list of receivers
    subject: 'Hello Jul/05/2020 PM 14:26', // Subject line
    text: 'Hello world PM 14:26 plaintext', // plaintext body
    html: '<b>Hello world PM 14:26 html</b>' // html body
}

// send mail with defined transport object

transporter.sendMail(mailOptions, function(error, info){
    if(error){
        return console.log(error)
    }
	console.log('Message sent: ' + info.response)

	console.error ("*** 終了 ***")
})


// ---------------------------------------------------------------
.env
SERVER = 'hi-ho.mose-mail.jp'
PORT = 587
USR = '****@hi-ho.ne.jp'
PASSWORD = '****'
FROM = '****@hi-ho.ne.jp'
TO = 'sample@example.com'

実行結果

$ ./hi-ho.js
*** 開始 ***
****@hi-ho.ne.jp
sample@example.com
Message sent: 250 2.0.0 0622YNNb088216 Message accepted for delivery
*** 終了 ***
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?