LoginSignup
111
100

More than 5 years have passed since last update.

Node.jsでメールを送信するパッケージ

Last updated at Posted at 2014-04-19

nodemailer npm公式サイト
http://www.nodemailer.com/

nodemailer npmパッケージサイト
https://npmjs.org/package/nodemailer

nodemailer GitHubサイト
https://github.com/andris9/nodemailer

SMTPに使用できるメールサービス

  • DynectEmail
  • Gmail
  • hot.ee
  • Hotmail
  • iCloud
  • mail.ee
  • Mail.Ru
  • Mailgun
  • Mailjet
  • Mandrill
  • Postmark
  • QQ
  • QQex (Tencent Business Email)
  • SendGrid
  • SendCloud
  • SES
  • Yahoo
  • yandex
  • Zoho

インストール

npm install nodemailer

サンプルコード

var mailer = require('nodemailer');

//SMTPの設定
var setting = {
    //SMTPサーバーを使う場合
    host: 'SMTPホスト',
    auth: {
        user: 'ユーザ名',
        pass: 'パスワード',
        port: 'SMTPポート番号'
    }

    /*
    //Webサービスを使う場合
    service: 'サービス名', //'Gmail'、'Hotmail'、'Yahoo Mail'など
    auth: {
        user: 'アカンと名',
        pass: 'パスワード',
        port: 'ポート番号' //'25'など
    }
    */
};

//メールの内容
var mailOptions = {
    from: '送信者のメールアドレス',
    to: '送信先メールアドレス',
    subject: 'メールの件名',
    html: 'メールの内容' //HTMLタグが使えます
};

//SMTPの接続
var smtp = mailer.createTransport('SMTP', setting);

//メールの送信
smtp.sendMail(mailOptions, function(err, res){
    //送信に失敗したとき
    if(err){
        console.log(err);
    //送信に成功したとき
    }else{
        console.log('Message sent: ' + res.message);
    }
    //SMTPの切断
    smtp.close();
});
111
100
1

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
111
100