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?

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
//
//					Mar/31/2025
// ---------------------------------------------------------------
'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 Mar/31/2025 AM 08:36', // Subject line
    text: 'Hello world AM 08:36 plaintext', // plaintext body
    html: '<b>Hello world AM 08:36 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
*** 終了 ***

確認したバージョン

$ node --version
v23.9.0

エラーが出た時の暫定対策

エラーメッセージ

Error [ERR_TLS_CERT_ALTNAME_INVALID]: Hostname/IP does not match certificate's altnames:

証明書を無効にします。

const transporter = nodemailer.createTransport({
  host: 'eye.jp', // 使用する SMTP サーバー
  port: 465, // 例: SSL/TLS のポート
  secure: true, // true にすると SSL/TLS を使用
  auth: {
    user: env.usr,
    pass: env.password,
  },
  tls: {
    rejectUnauthorized: false, // 証明書検証を無効にする
  },
})
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?