mcpField
N
nodemailer

nodemailer

github.com/aidotnet開発8595 回インストール39 週間前に更新
リポジトリ

nodemailer について

nodemailerはSMTP経由でメールを送信するNode.jsライブラリーです。OAuth2、HTMLテンプレート、添付ファイル、キュー送信の例があり、Node.jsアプリにメール機能を組み込む開発者向けです。

主な機能

  1. 01SMTP経由のメール送信
  2. 02OAuth2認証
  3. 03HTMLメールの作成
  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:✅

コメント