@sadikkhan02/adapter-nodemailer
通用电子邮件适配器NotifyKit使用Nodemailer (SMTP, AWS SES, Mailgun, SendGrid, Gmail, Mailtrap)。
这个包将NotifyKit的后台工作者连接到Nodemailer支持的任何SMTP服务器或电子邮件传递提供程序。它符合 EmailProvider 合同并由 @sadikkhan02/test-utils.
安装
npm install @sadikkhan02/notifykit @sadikkhan02/adapter-nodemailer nodemailer
注意:
nodemailer声明为外部对等依赖项,以允许消费应用程序完全控制版本绑定和本机传输扩展。
配置
环境变量
| 变量 | 要求 | 默认的 | 描述 |
|---|---|---|---|
SMTP_HOST | 是 | - - - - - - | SMTP服务器主机名(例如: smtp.mailtrap.io, smtp.sendgrid.net) |
SMTP_PORT | 是 | 587 | SMTP端口(587 STARTTLS, 465 对于SSL, 25 标准) |
SMTP_USER | 是 | - - - - - - | 认证用户名/ API用户 |
SMTP_PASSWORD | 是 | - - - - - - | 认证密码/ API密钥 |
SMTP_SECURE | 否 | false | 设置为 true 如果通过端口连接 465 (SSL) |
SMTP_FROM | 否 | SMTP_USER | 默认发件人电邮地址([email protected]) |
使用
import { createNotifier } from '@sadikkhan02/notifykit';
import { NodemailerEmailProvider } from '@sadikkhan02/adapter-nodemailer';
const emailProvider = new NodemailerEmailProvider({
host: process.env.SMTP_HOST || 'smtp.mailtrap.io',
port: Number(process.env.SMTP_PORT) || 587,
secure: process.env.SMTP_SECURE === 'true' || Number(process.env.SMTP_PORT) === 465,
auth: {
user: process.env.SMTP_USER!,
pass: process.env.SMTP_PASSWORD!,
},
from: process.env.SMTP_FROM || '[email protected]',
});
const notifier = createNotifier({
providers: {
email: {
type: 'nodemailer',
adapter: emailProvider,
from: '[email protected]',
},
},
});
// Register an email template
notifier.registerTemplate(
'account-activation',
'<h1>Activate Your Account</h1><p>Click <a href="{{url}}">here</a> to confirm.</p>',
);
// Enqueue notification (returns immediately with jobId)
const { jobId } = await notifier.notify({
to: { email: '[email protected]' },
channels: ['email'],
template: 'account-activation',
data: { url: 'https://example.com/activate?token=xyz' },
});
// Start processing email queue
notifier.startWorker({ channels: ['email'] });
错误分类和恢复
每个交付尝试都映射到一个标准化的 ProviderResult:
| SMTP错误/响应码 | 类别 | retryable | BullMQ行动 |
|---|---|---|---|
ETIMEDOUT, ECONNRESET, ECONNREFUSED | 网络故障 | true | 以指数回退重试 |
EAI_AGAIN (DNS临时查找失败) | DNS瞬态 | true | 以指数回退重试 |
421 (服务不可用,正在关闭通道) | 服务器繁忙 | true | 以指数回退重试 |
450, 451, 452 (信箱忙/信箱限制) | 率/配额 | true | 以指数回退重试 |
535 (身份验证凭据无效) | 配置缺陷 | false | 直接路由到DLQ(不重试) |
550, 551, 553 (邮箱不存在) | 收件人坏 | false | 直接路由到DLQ(不重试) |
552, 554 (交易失败/被拒绝) | 违反政策 | false | 直接路由到DLQ(不重试) |
凭据卫生处理
Nodemailer返回的所有原始错误消息在附加到错误日志或提供者结果(密码、授权承载令牌和连接凭据匹配)之前都经过处理 pass 或者 password 模式被自动编辑。
许可证
麻省理工学院