Skip to main content

高级 Nodemailer 配置

🌐 Advanced Nodemailer configuration

Page summary:

Nodemailer 提供程序支持 OAuth2 认证、连接池、DKIM 签名和速率限制。每种场景在标准 SMTP 配置的基础上向 providerOptions 添加特定的键。

本页面介绍了可与电子邮件功能一起使用的社区@strapi/provider-email-nodemailer软件包的生产场景。有关基本提供程序安装和SMTP设置,请参阅配置提供程序

🌐 This page covers production scenarios for the community @strapi/provider-email-nodemailer package that can be used with the Email feature. For basic provider installation and SMTP setup, see Configuring providers.

Supported providers

有关支持的 providerOptions 的完整列表,请参阅 provider README on npm

🌐 For the full list of supported providerOptions, refer to the provider README on npm.

OAuth2 认证

🌐 OAuth2 authentication

对于像 Gmail 或 Outlook 这样需要使用 OAuth2 而不是密码的服务:

🌐 For services like Gmail or Outlook that require OAuth2 instead of a password:

/config/plugins.js
module.exports = ({ env }) => ({
email: {
config: {
provider: 'nodemailer',
providerOptions: {
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
type: 'OAuth2',
user: env('SMTP_USER'),
clientId: env('OAUTH_CLIENT_ID'),
clientSecret: env('OAUTH_CLIENT_SECRET'),
refreshToken: env('OAUTH_REFRESH_TOKEN'),
},
},
settings: {
defaultFrom: env('SMTP_USER'),
defaultReplyTo: env('SMTP_USER'),
},
},
},
});

连接池

🌐 Connection pooling

使用连接池重用 SMTP 连接,以在发送大量电子邮件时提高吞吐量:

🌐 Use connection pooling to reuse SMTP connections and improve throughput when sending many emails:

/config/plugins.js
module.exports = ({ env }) => ({
email: {
config: {
provider: 'nodemailer',
providerOptions: {
host: env('SMTP_HOST'),
port: 465,
secure: true,
pool: true,
maxConnections: 5,
maxMessages: 100,
auth: {
user: env('SMTP_USERNAME'),
pass: env('SMTP_PASSWORD'),
},
},
settings: {
defaultFrom: 'hello@example.com',
defaultReplyTo: 'hello@example.com',
},
},
},
});

DKIM 签名

🌐 DKIM signing

添加 DKIM 签名以提高投递率并验证外发邮件:

🌐 Add DKIM signatures to improve deliverability and authenticate outbound mail:

/config/plugins.js
module.exports = ({ env }) => ({
email: {
config: {
provider: 'nodemailer',
providerOptions: {
host: env('SMTP_HOST'),
port: 587,
auth: {
user: env('SMTP_USERNAME'),
pass: env('SMTP_PASSWORD'),
},
dkim: {
domainName: 'example.com',
keySelector: 'mail',
privateKey: env('DKIM_PRIVATE_KEY'),
},
},
settings: {
defaultFrom: 'hello@example.com',
defaultReplyTo: 'hello@example.com',
},
},
},
});

速率限制

🌐 Rate limiting

限制每个时间间隔内发送的消息数量以避免触发垃圾邮件过滤器:

🌐 Limit the number of messages sent per time interval to avoid triggering spam filters:

/config/plugins.js
module.exports = ({ env }) => ({
email: {
config: {
provider: 'nodemailer',
providerOptions: {
host: env('SMTP_HOST'),
port: 465,
secure: true,
pool: true,
rateLimit: 5, // max messages per rateDelta
rateDelta: 1000, // time interval in ms (1 second)
auth: {
user: env('SMTP_USERNAME'),
pass: env('SMTP_PASSWORD'),
},
},
settings: {
defaultFrom: 'hello@example.com',
defaultReplyTo: 'hello@example.com',
},
},
},
});
Rate limiting requires connection pooling

rateLimitrateDelta 只有在 pool: true 也被设置时才会生效。