@geekmidas/emailkit

中文文档
9.6.1发布于 2026/11/16TypeScriptESM + CJS构建来源已验证近 30 天下载 1.3万

具有 SMTP 支持和反应模板的安全电子邮件客户端

中文使用文档
文档目录13 节
  1. 特征
  2. 安装
  3. 快速启动
  4. API 参考
  5. createEmailClient<T>(config: EmailClientConfig<T>): SMTPCli…
  6. 客户方法
  7. 内建模板示例
  8. 欢迎电子邮件
  9. 密码重置电子邮件
  10. 通知电子邮件
  11. 多个模板示例
  12. 类型脚本支持
  13. 许可证

@geekmidas/电子邮件kit

具有SMTP支持和回放模板的Type-seafe电子邮件客户端.

特征

  • 类型安全模板: 在构建时提供模板, 其模板名称及其相应的道具都有完整的 TypeScript 推论
  • SMTP 支持: 通过节点器配置与任何 SMTP 服务器一起工作
  • 反应模板: 用途 react-dom/server 将 React 组件渲染到 HTML

安装

pnpm add @geekmidas/emailkit

快速启动

import { createEmailClient } from '@geekmidas/emailkit';

// Define your templates with props
const WelcomeEmail = ({ name, confirmationUrl }: { 
  name: string; 
  confirmationUrl?: string; 
}) => (
  <div style={{ fontFamily: 'Arial, sans-serif', maxWidth: '600px' }}>
    <h1>Welcome, {name}!</h1>
    <p>We're excited to have you on board.</p>
    {confirmationUrl && (
      <p>
        <a href={confirmationUrl} style={{ 
          backgroundColor: '#007bff', 
          color: 'white', 
          padding: '10px 20px', 
          textDecoration: 'none',
          borderRadius: '4px' 
        }}>
          Confirm Email
        </a>
      </p>
    )}
  </div>
);

const templates = {
  welcome: WelcomeEmail,
};

// Create client with templates - types are fully inferred
const client = createEmailClient({
  smtp: {
    host: 'smtp.example.com',
    port: 587,
    auth: {
      user: '[email protected]',
      pass: 'password',
    },
  },
  templates,
  defaults: {
    from: '[email protected]',
  },
});

// Send email with full type safety
await client.sendTemplate('welcome', {
  from: '[email protected]',
  to: '[email protected]',
  subject: 'Welcome to our service!',
  props: {
    name: 'John Doe',
    confirmationUrl: 'https://example.com/confirm/123',
  },
});

API 参考

createEmailClient<T>(config: EmailClientConfig<T>): SMTPClient<T>

创建一个有类型安全模板支持的新电子邮件客户端 .

配置

interface EmailClientConfig<T extends TemplateRecord> {
  smtp: SMTPConfig;
  templates: T;
  defaults?: {
    from?: string | Address;
    replyTo?: string | Address;
  };
}

interface SMTPConfig {
  host: string;
  port: number;
  secure?: boolean;
  auth?: {
    user: string;
    pass: string;
  };
  tls?: {
    rejectUnauthorized?: boolean;
    servername?: string;
  };
  pool?: boolean;
  maxConnections?: number;
  maxMessages?: number;
  rateLimit?: number;
  logger?: boolean;
  debug?: boolean;
}

客户方法

send(options: PlainEmailOptions): Promise<SendResult>

发送纯文本或 HTML 电子邮件 .

await client.send({
  from: '[email protected]',
  to: '[email protected]',
  subject: 'Plain email',
  text: 'This is a plain text email',
  html: '<p>This is an HTML email</p>',
});

sendTemplate<K>(template: K, options): Promise<SendResult>

使用带有类型安全道具的React模板发送电子邮件.

await client.sendTemplate('welcome', {
  from: '[email protected]',
  to: '[email protected]',
  subject: 'Welcome!',
  props: { name: 'John', confirmationUrl: 'https://...' },
});

verify(): Promise<boolean>

验证 SMTP 连接 .

close(): Promise<void>

关闭SMTP连接.

getTemplateNames(): string[]

获取可用的模板名称 .

内建模板示例

欢迎电子邮件

interface WelcomeEmailProps {
  name: string;
  confirmationUrl?: string;
}

const WelcomeEmail = ({ name, confirmationUrl }: WelcomeEmailProps) => (
  <html>
    <head>
      <meta charSet="utf-8" />
      <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      <title>Welcome!</title>
    </head>
    <body style={{ margin: 0, padding: 0, backgroundColor: '#f4f4f4' }}>
      <div style={{
        fontFamily: '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif',
        lineHeight: 1.6,
        color: '#333',
        maxWidth: '600px',
        margin: '0 auto',
        padding: '20px',
      }}>
        <h1 style={{ color: '#2c3e50', marginBottom: '20px' }}>
          Welcome, {name}!
        </h1>
        <p>
          We're excited to have you on board. Your account has been successfully created.
        </p>
        {confirmationUrl && (
          <>
            <p>Please confirm your email address by clicking the button below:</p>
            <p style={{ textAlign: 'center', margin: '30px 0' }}>
              <a href={confirmationUrl} style={{
                display: 'inline-block',
                padding: '12px 24px',
                backgroundColor: '#3498db',
                color: '#ffffff',
                textDecoration: 'none',
                borderRadius: '4px',
                fontWeight: 'bold',
              }}>
                Confirm Email
              </a>
            </p>
          </>
        )}
        <div style={{
          marginTop: '40px',
          paddingTop: '20px',
          borderTop: '1px solid #eee',
          fontSize: '14px',
          color: '#666',
        }}>
          <p>If you have any questions, feel free to reply to this email.</p>
          <p>Best regards,<br />The Team</p>
        </div>
      </div>
    </body>
  </html>
);

密码重置电子邮件

interface PasswordResetEmailProps {
  name: string;
  resetUrl: string;
  expiresIn: string;
}

const PasswordResetEmail = ({ name, resetUrl, expiresIn }: PasswordResetEmailProps) => (
  <html>
    <head>
      <meta charSet="utf-8" />
      <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      <title>Password Reset Request</title>
    </head>
    <body style={{ margin: 0, padding: 0, backgroundColor: '#f4f4f4' }}>
      <div style={{
        fontFamily: '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif',
        lineHeight: 1.6,
        color: '#333',
        maxWidth: '600px',
        margin: '0 auto',
        padding: '20px',
      }}>
        <h1 style={{ color: '#2c3e50', marginBottom: '20px' }}>
          Password Reset Request
        </h1>
        <p>Hi {name},</p>
        <p>
          We received a request to reset your password. Click the button below to create a new password:
        </p>
        <p style={{ textAlign: 'center', margin: '30px 0' }}>
          <a href={resetUrl} style={{
            display: 'inline-block',
            padding: '12px 24px',
            backgroundColor: '#3498db',
            color: '#ffffff',
            textDecoration: 'none',
            borderRadius: '4px',
            fontWeight: 'bold',
          }}>
            Reset Password
          </a>
        </p>
        <p>
          This link will expire in {expiresIn}. If you didn't request a password reset,
          you can safely ignore this email.
        </p>
        <div style={{
          marginTop: '40px',
          paddingTop: '20px',
          borderTop: '1px solid #eee',
          fontSize: '14px',
          color: '#666',
        }}>
          <p>For security reasons, this link can only be used once.</p>
        </div>
      </div>
    </body>
  </html>
);

通知电子邮件

interface NotificationEmailProps {
  name: string;
  title: string;
  message: string;
  actionUrl?: string;
  actionText?: string;
}

const NotificationEmail = ({ 
  name, 
  title, 
  message, 
  actionUrl, 
  actionText = 'View Details' 
}: NotificationEmailProps) => (
  <html>
    <head>
      <meta charSet="utf-8" />
      <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      <title>{title}</title>
    </head>
    <body style={{ margin: 0, padding: 0, backgroundColor: '#f4f4f4' }}>
      <div style={{
        fontFamily: '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif',
        lineHeight: 1.6,
        color: '#333',
        maxWidth: '600px',
        margin: '0 auto',
        padding: '20px',
      }}>
        <h1 style={{ color: '#2c3e50', marginBottom: '20px' }}>
          {title}
        </h1>
        <p>Hi {name},</p>
        <p>{message}</p>
        {actionUrl && (
          <p style={{ textAlign: 'center', margin: '30px 0' }}>
            <a href={actionUrl} style={{
              display: 'inline-block',
              padding: '12px 24px',
              backgroundColor: '#3498db',
              color: '#ffffff',
              textDecoration: 'none',
              borderRadius: '4px',
              fontWeight: 'bold',
            }}>
              {actionText}
            </a>
          </p>
        )}
        <div style={{
          marginTop: '40px',
          paddingTop: '20px',
          borderTop: '1px solid #eee',
          fontSize: '14px',
          color: '#666',
        }}>
          <p>This is an automated notification from our system.</p>
        </div>
      </div>
    </body>
  </html>
);

多个模板示例

import { createEmailClient } from '@geekmidas/emailkit';

const templates = {
  welcome: WelcomeEmail,
  passwordReset: PasswordResetEmail,
  notification: NotificationEmail,
};

const client = createEmailClient({
  smtp: {
    host: 'smtp.example.com',
    port: 587,
    auth: {
      user: '[email protected]',
      pass: 'password',
    },
  },
  templates,
  defaults: {
    from: '[email protected]',
  },
});

// All template names and props are fully type-safe
await client.sendTemplate('welcome', {
  from: '[email protected]',
  to: '[email protected]',
  subject: 'Welcome to our service!',
  props: { name: 'John Doe', confirmationUrl: 'https://example.com/confirm/123' },
});

await client.sendTemplate('passwordReset', {
  from: '[email protected]',
  to: '[email protected]',
  subject: 'Reset your password',
  props: { name: 'John Doe', resetUrl: 'https://example.com/reset/456', expiresIn: '24 hours' },
});

await client.sendTemplate('notification', {
  from: '[email protected]',
  to: '[email protected]',
  subject: 'Important notification',
  props: { 
    name: 'John Doe', 
    title: 'Account Update', 
    message: 'Your account settings have been updated.',
    actionUrl: 'https://example.com/settings',
    actionText: 'View Settings' 
  },
});

类型脚本支持

库提供全部 TypeScript 支持 :

  • 模板名称推论 - 只接受有效的模板名称
  • 道具类型检查 - 根据模板的道具类型验证道具
  • 自动完整支持 - 模板名称和道具的 IDE 自动完成
  • 编译时间安全 - 构建时捕获模板和道具出错

许可证

麻省理工学院

版本与发布记录

@geekmidas/emailkit 共发布 206 个版本,首次发布于 2025/07/13;过去 12 个月发布了 201 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
9.6.1latest2026/11/16今天
10.0.0-alpha.185alpha2026/11/16今天
10.0.0-alpha.1842026/11/16今天
10.0.0-alpha.1832026/11/16今天
10.0.0-alpha.1822026/11/132 天前
10.0.0-alpha.1812026/11/133 天前

关于 @geekmidas/emailkit 的常见问题

@geekmidas/emailkit 是什么?

具有 SMTP 支持和反应模板的安全电子邮件客户端

如何安装 @geekmidas/emailkit?

在项目目录运行 npm install @geekmidas/emailkit,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=18.0.0)、react-dom(>=18.0.0)、@react-email/components(~1.0.1)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@geekmidas/emailkit 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@geekmidas/emailkit 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@geekmidas/emailkit 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@geekmidas/emailkit 可以商用吗?

@geekmidas/emailkit 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@geekmidas/emailkit 的下载量有多少?

根据 npm 官方下载统计,@geekmidas/emailkit 在 2026/10/16 – 2026/11/14 这 30 天内被下载 13,460 次,日均约 449 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@geekmidas/emailkit 还在维护吗?

最新版本 9.6.1 发布于 2026/11/16(今天);至今共发布 206 个版本,其中过去 12 个月发布了 201 个;首次发布于 2025/07/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。