LoginSignup
1
0

More than 3 years have passed since last update.

nodemailer envelope設定サンプル

Last updated at Posted at 2020-02-19

テスト環境

  • node.js v12
  • nodemailer v6.3

公式ドキュメント

サンプルコード

send-mail.js
var Mailer = require('nodemailer')

//
// ローカルsmtpサーバ
//
const smtp = Mailer.createTransport({
  host: 'localhost',
  port: 25
})

const message = {
// メッセージヘッダとかの設定
// => メール受け取る側から見える情報
  from: 'from@example.com',
  to: ['to@example.com'],
  cc: ['cc@example.com'],
  bcc: ['bcc@example.com'],
  subject: 'HELLO! nodemailer!!',
  text: 'this is a test mail',
  html: '<p>this is a test mail</p>',

// envelope: SMTPサーバー用の設定
//  FROM: (バウンスメールが届くアドレス)
//    envelope.from
//  と
//  RCPT TO: (実際に送信する宛先)
//    envelope.to, envelope.cc, envelope.bcc

// envelopeは "省略可能"
// 省略した場合は以下のように自動付与されると思っていい
  envelope: {
    from: 'from@example.com',
    to: ['to@example.com'],// RCPT TO: に突っ込まれる
    cc: ['cc@example.com'],// RCPT TO: に突っ込まれる
    bcc: ['bcc@example.com'],// RCPT TO: に突っ込まれる
  }
}

smtp.sendMail(message)

メッセージヘッダのfromとバウンスメールの受け取りアドレスを変えたい場合

例えば、MAILER-DAEMONが返ってきてしまった該当メールアドレスは無効化したいので、バウンスメール用のメールアドレスを用意して監視したい時などが利用場面です。

この場合は、envelopeを明示的に設定する必要あります。

envelopeを明示的に設定する場合、message.to, message.cc, message.bccに宛先を書くだけでは、メールは届かないので注意。
envelope.to, envelope.cc, envelope.bcc に宛先を書く必要がある。

const message = {
  from: 'from@example.com',
  to: ['to@example.com'],
  cc: ['cc@example.com'],
  bcc: ['bcc@example.com'],
  subject: 'HELLO! nodemailer!!',
  text: 'bounce-mails are sent to for-bounce@example.com ',
  html: '<p>bounce-mails are sent to for-bounce@example.com</p>',
  envelope: {
    from: 'for-bounce@example.com',
    to: ['to@example.com'],
    cc: ['cc@example.com'],
    bcc: ['bcc@example.com'],
  }
}
1
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
1
0