mcpField
N
nodemailer

nodemailer

github.com/aidotnet开发8595 次安装39 周前更新
代码仓库

关于 nodemailer

nodemailer 是用于通过 SMTP 发送邮件的 Node.js 库,示例涵盖 OAuth2、HTML 模板、附件和队列发送,适合在 Node.js 应用中集成邮件功能的开发者。

主要功能

  1. 01通过 SMTP 发送邮件
  2. 02使用 OAuth2 进行身份验证
  3. 03创建 HTML 邮件内容
  4. 04为邮件添加附件
  5. 05以速率限制队列发送邮件

适用场景

  1. 01发送应用通知
  2. 02发送模板化欢迎邮件
  3. 03通过邮件发送带附件的报告
  4. 04按速率限制排队发送邮件

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/aidotnet/moyucode --skill nodemailer

安装后在 Claude Code 中输入/nodemailer确认已加载。

详细介绍

Nodemailer 工具

描述

通过 SMTP、OAuth2、附件和 HTML 模板,从 Node.js 发送电子邮件。

来源

安装

npm install nodemailer

用法示例

基本电子邮件
import nodemailer from 'nodemailer';

const transporter = nodemailer.createTransport({
  host: 'smtp.gmail.com',
  port: 587,
  secure: false,
  auth: {
    user: process.env.EMAIL_USER,
    pass: process.env.EMAIL_PASS,
  },
});

async function sendEmail() {
  const info = await transporter.sendMail({
    from: '"My App" <[email protected]>',
    to: '[email protected]',
    subject: 'Welcome to My App',
    text: 'Hello, welcome to our platform!',
    html: '<h1>Hello</h1><p>Welcome to our platform!</p>',
  });
  
  console.log('Message sent:', info.messageId);
}
使用模板的 HTML 电子邮件
async function sendWelcomeEmail(user: { name: string; email: string }) {
  const html = `
    <!DOCTYPE html>
    <html>
      <head>
        <style>
          .container { max-width: 600px; margin: 0 auto; font-family: Arial; }
          .header { background: #4F46E5; color: white; padding: 20px; }
          .content { padding: 20px; }
          .button { background: #4F46E5; color: white; padding: 12px 24px; 
                    text-decoration: none; border-radius: 4px; }
        </style>
      </head>
      <body>
        <div class="container">
          <div class="header">
            <h1>Welcome, ${user.name}!</h1>
          </div>
          <div class="content">
            <p>Thank you for joining our platform.</p>
            <a href="https://myapp.com/dashboard" class="button">Get Started</a>
          </div>
        </div>
      </body>
    </html>
  `;
  
  await transporter.sendMail({
    from: '"My App" <[email protected]>',
    to: user.email,
    subject: `Welcome, ${user.name}!`,
    html,
  });
}
带附件的电子邮件
async function sendEmailWithAttachment() {
  await transporter.sendMail({
    from: '"Reports" <[email protected]>',
    to: '[email protected]',
    subject: 'Monthly Report',
    text: 'Please find the monthly report attached.',
    attachments: [
      {
        filename: 'report.pdf',
        path: './reports/monthly-report.pdf',
      },
      {
        filename: 'data.xlsx',
        content: Buffer.from('...'), // Buffer content
      },
      {
        filename: 'logo.png',
        path: './assets/logo.png',
        cid: 'logo@myapp', // For embedding in HTML
      },
    ],
    html: '<img src="cid:logo@myapp" /><p>See attached report.</p>',
  });
}
OAuth2 身份验证(Gmail)
const transporter = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    type: 'OAuth2',
    user: process.env.GMAIL_USER,
    clientId: process.env.GOOGLE_CLIENT_ID,
    clientSecret: process.env.GOOGLE_CLIENT_SECRET,
    refreshToken: process.env.GOOGLE_REFRESH_TOKEN,
  },
});
带速率限制的电子邮件队列
class EmailQueue {
  private queue: Array<() => Promise<void>> = [];
  private processing = false;
  
  async add(emailFn: () => Promise<void>) {
    this.queue.push(emailFn);
    this.process();
  }
  
  private async process() {
    if (this.processing) return;
    this.processing = true;
    
    while (this.queue.length > 0) {
      const emailFn = this.queue.shift()!;
      await emailFn();
      await new Promise(r => setTimeout(r, 1000)); // Rate limit
    }
    
    this.processing = false;
  }
}

标签

email、smtp、notification、communication、automation

兼容性

  • Codex:✅
  • Claude Code:✅

评论

支持 Markdown