nodemailer は SMTP プロトコルを使用してメールを送信する汎用ライブラリです。sendgrid と sparkpost は、それぞれ特定のクラウドメール配信サービスと連携するための API クライアントです。これらはサーバーサイドでの通知、パスワードリセット、トランザクションメールの送信に使用されます。
Node.js アプリケーションでメールを送信する場合、主に 2 つのアプローチがあります。一つは SMTP サーバーに直接接続する方法、もう一つはクラウドサービスの API を使用する方法です。nodemailer、sendgrid、sparkpost はそれぞれの代表格ですが、現状の保守状況と仕組みには大きな違いがあります。
nodemailer は SMTP プロトコルを使用します。
// nodemailer: SMTP トランスポートの設定
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
host: 'smtp.example.com',
port: 587,
auth: {
user: 'user@example.com',
pass: 'password'
}
});
sendgrid は SendGrid 社の REST API を使用します。
sendgrid は非推奨です。現在は @sendgrid/mail が公式です。// sendgrid: API キーによる認証(@sendgrid/mail の場合)
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
// 旧パッケージ 'sendgrid' は非推奨のため使用しないでください
sparkpost は SparkPost 社の REST API を使用します。
// sparkpost: API クライアントの初期化
const SparkPost = require('sparkpost');
const client = new SparkPost('YOUR_API_KEY');
// 保守状況を確認せずに本番環境で使用するのは危険です
実際のメール送信コードもパッケージごとに異なります。
nodemailer は sendMail 関数を使用します。
// nodemailer: メール送信
await transporter.sendMail({
from: 'sender@example.com',
to: 'receiver@example.com',
subject: 'Hello',
text: 'Hello world',
html: '<b>Hello world</b>'
});
sendgrid は send 関数を使用します。
personalizations 配列で宛先を管理します。// sendgrid: メール送信(@sendgrid/mail)
await sgMail.send({
to: 'receiver@example.com',
from: 'sender@example.com',
subject: 'Hello',
content: [{ type: 'text/plain', value: 'Hello world' }]
});
sparkpost は transmissions.send を使用します。
recipients 配列で定義します。// sparkpost: メール送信
client.transmissions.send({
content: {
from: 'sender@example.com',
subject: 'Hello',
text: 'Hello world'
},
recipients: [{ address: 'receiver@example.com' }]
});
この比較において最も重要な点は、パッケージの現状です。
nodemailer は活発に保守されており、コミュニティでの信頼が厚いです。新規プロジェクトでも安心して使用できます。sendgrid パッケージは非推奨です。公式ドキュメントでは @sendgrid/mail への移行を強く推奨しています。旧パッケージにはセキュリティ更新が届かない可能性があります。sparkpost パッケージは、サービスの買収により事実上の終了状態にあります。新規プロジェクトで採用するのはリスクが高すぎます。| 特徴 | nodemailer | sendgrid | sparkpost |
|---|---|---|---|
| プロトコル | SMTP | REST API | REST API |
| 保守状況 | ✅ 活発 | ⚠️ パッケージ名変更必要 | ❌ 不明確/非推奨 |
| 依存関係 | なし(汎用) | SendGrid 社のみ | SparkPost 社のみ |
| 設定の手間 | SMTP 設定が必要 | API キーのみ | API キーのみ |
| 配信分析 | サーバー依存 | 詳細なダッシュボード | 詳細なダッシュボード |
nodemailer は、インフラを自分で管理したいチーム — 例えば AWS SES や自社 SMTP を使う場合 — にとって最良の選択です。ライブラリ自体が軽量で、特定のサービスに縛られないため、長期的な保守性が最も高いです。
sendgrid (実際には @sendgrid/mail)は、配信到達率や分析機能を重視する場合に適しています。ただし、必ず公式が推奨する最新パッケージを使用してください。旧パッケージ sendgrid は使用してはいけません。
sparkpost は、現時点では新規プロジェクトでの使用を避けるべきです。保守されていないライブラリに依存することは、セキュリティと安定性のリスクになります。
最終的なアドバイス:メール送信機能を実装する際は、ライブラリの保守状況を確認してください。機能だけでなく、その背後にあるチームが活動しているかどうかが、プロジェクトの寿命を決めます。
SMTP サーバーを既に持っている場合や、特定のサービスに依存したくない場合に選択します。自社インフラや AWS SES などを直接使用する際に最適です。保守状況が良く、長期的な安定性があります。
高い配信到達率と詳細な分析が必要な場合に選択します。ただし、npm パッケージ sendgrid は非推奨であり、@sendgrid/mail へ移行しています。新規プロジェクトでは公式の最新パッケージを使用してください。
過去には有力な選択肢でしたが、現在は MessageBird に買収されており、パッケージの保守状況が不明確です。新規プロジェクトでの使用は推奨されません。代替サービスの検討が必要です。
Send emails from Node.js – easy as cake! 🍰✉️
See nodemailer.com for documentation and terms.
[!TIP] Check out EmailEngine – a self-hosted email gateway that allows making REST requests against IMAP and SMTP servers. EmailEngine also sends webhooks whenever something changes on the registered accounts.
Using the email accounts registered with EmailEngine, you can receive and send emails. EmailEngine supports OAuth2, delayed sends, opens and clicks tracking, bounce detection, etc. All on top of regular email accounts without an external MTA service.
Documentation for Nodemailer can be found at nodemailer.com.
You are using an older Node.js version than v6.0. Upgrade Node.js to get support for the spread operator. Nodemailer supports all Node.js versions starting from Node.js@v6.0.0.
Gmail either works well, or it does not work at all. It is probably easier to switch to an alternative service instead of fixing issues with Gmail. If Gmail does not work for you, then don't use it. Read more about it here.
Check your firewall settings. Timeout usually occurs when you try to open a connection to a firewalled port either on the server or on your machine. Some ISPs also block email ports to prevent spamming.
It's either a firewall issue, or your SMTP server blocks authentication attempts from some servers.
secure option. This should be set to true only for port 465. For every other port, it should be false. Setting it to false does not mean that Nodemailer would not use TLS. Nodemailer would still try to upgrade the connection to use TLS if the server supports it.false to skip chain verification or upgrade your Node versionlet configOptions = {
host: 'smtp.example.com',
port: 587,
tls: {
rejectUnauthorized: true,
minVersion: 'TLSv1.2'
}
};
Node.js uses c-ares to resolve domain names, not the DNS library provided by the system, so if you have some custom DNS routing set up, it might be ignored. Nodemailer runs dns.resolve4() and dns.resolve6() to resolve hostname into an IP address. If both calls fail, then Nodemailer will fall back to dns.lookup(). If this does not work for you, you can hard code the IP address into the configuration like shown below. In that case, Nodemailer would not perform any DNS lookups.
let configOptions = {
host: '1.2.3.4',
port: 465,
secure: true,
tls: {
// must provide server name, otherwise TLS certificate check will fail
servername: 'example.com'
}
};
Nodemailer has official support for Node.js only. For anything related to TypeScript, you need to directly contact the authors of the type definitions.
If you are having issues with Nodemailer, then the best way to find help would be Stack Overflow or revisit the docs.
Nodemailer is licensed under the MIT No Attribution license
The Nodemailer logo was designed by Sven Kristjansen.