nodemailerでNode.jsからメールを送る
またまた使いそうなライブラリのチェック。今回はSMTPによるメールの送信。
さっそくプロジェクトの作成から。
「GitHub - nodemailer/nodemailer: ✉️ Send e-mails with Node.JS – easy as cake!」にはsendmailでの送信サンプルもありました。
$ mkdir my_mail $ cd my_mail $ npm init $ npm install nodemailer --save npm WARN package.json my_mail@0.0.0 No README.md file found! npm http GET https://registry.npmjs.org/nodemailer npm http 200 https://registry.npmjs.org/nodemailer npm http GET https://registry.npmjs.org/nodemailer/-/nodemailer-0.3.22.tgz npm http 200 https://registry.npmjs.org/nodemailer/-/nodemailer-0.3.22.tgz npm http GET https://registry.npmjs.org/mailcomposer npm http GET https://registry.npmjs.org/simplesmtp npm http 200 https://registry.npmjs.org/mailcomposer npm http GET https://registry.npmjs.org/mailcomposer/-/mailcomposer-0.1.15.tgz npm http 200 https://registry.npmjs.org/mailcomposer/-/mailcomposer-0.1.15.tgz npm http 200 https://registry.npmjs.org/simplesmtp npm http GET https://registry.npmjs.org/simplesmtp/-/simplesmtp-0.1.19.tgz npm http 200 https://registry.npmjs.org/simplesmtp/-/simplesmtp-0.1.19.tgz npm http GET https://registry.npmjs.org/rai npm http GET https://registry.npmjs.org/mimelib-noiconv npm http 304 https://registry.npmjs.org/mimelib-noiconv npm http 200 https://registry.npmjs.org/rai npm http GET https://registry.npmjs.org/rai/-/rai-0.1.6.tgz npm http 200 https://registry.npmjs.org/rai/-/rai-0.1.6.tgz nodemailer@0.3.22 node_modules/nodemailer ├── mailcomposer@0.1.15 (mimelib-noiconv@0.1.9) └── simplesmtp@0.1.19 (rai@0.1.6)
mail.js
GmailのSMTPを利用しています。userとpassを埋めてください。
smtpTransport.close();は接続を閉じる場合にコールするようです。
var nodemailer = require("nodemailer"); // create reusable transport method (opens pool of SMTP connections) var smtpTransport = nodemailer.createTransport("SMTP",{ service: "Gmail", auth: { user: "*****", pass: "*****" } }); // setup e-mail data with unicode symbols var mailOptions = { from: "***** <****@gmail.com>", // sender address to: "****@gmail.com, ****@gmail.com", // list of receivers subject: "イーハトーヴォ", // Subject line text: "あのイーハトーヴォのすきとおった風、夏でも底に冷たさをもつ青いそら、うつくしい森で飾られたモリーオ市、郊外のぎらぎらひかる草の波。", // plaintext body html: "<b>あのイーハトーヴォのすきとおった風、夏でも底に冷たさをもつ青いそら、うつくしい森で飾られたモリーオ市、郊外のぎらぎらひかる草の波。</b>" // html body } // send mail with defined transport object smtpTransport.sendMail(mailOptions, function(error, response){ if(error){ console.log(error); }else{ console.log("Message sent: " + response.message); } // if you don't want to use this transport object anymore, uncomment following line smtpTransport.close(); // shut down the connection pool, no more messages });
TLSとかポート番号とか指定しなくて良いのかなーと思ったんですが、serviceで指定しているせいか、試してみたら送れてしまいました。通常のSMTPサーバの場合、以下のように指定するようです。
nodemailer.SMTP = { host: "something.smtp.com", port: 25, ssl: true, use_authentication: true, user: "your mail account", pass: "your mail password" };
また、添付ファイルはmailOptionsの中で以下の様に設定するようです。
// An array of attachments attachments:[ // String attachment { fileName: 'notes.txt', contents: 'Some notes about this e-mail', contentType: 'text/plain' // optional, would be detected from the filename }, // Binary Buffer attachment { fileName: 'image.png', contents: new Buffer('iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD/' + '//+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4U' + 'g9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC', 'base64'), cid: 'note@node' // should be as unique as possible }, // File Stream attachment { fileName: 'nyan cat ✔.gif', filePath: __dirname+"/nyan.gif", cid: 'nyan@node' // should be as unique as possible }
ISO-2022-JPは試していません。