@emailjs/nodejs

中文文档
5.0.2发布于 2024/10/22BSD-3-ClauseTypeScriptESM + CJS零依赖近 30 天下载 3.2万

Official EmailJS SDK for NodeJS

原始使用文档
文档目录8 节
  1. Disclaimer
  2. Links
  3. Intro
  4. Usage
  5. FAQ
  6. Examples
  7. ECMAScript modules
  8. Configuration

Official EmailJS SDK for Node.js

SDK for EmailJS.com customers.
Use your EmailJS account for sending emails.

codecov npm version

Disclaimer

This is a Node.js platform, otherwise use

Official SDK Docs

Intro

EmailJS helps you send emails directly from code with one command. No large knowledge is required – just connect EmailJS to one of the supported email services, create an email template, and use our SDK to trigger an email.

Usage

Install EmailJS SDK using npm:

$ npm install @emailjs/nodejs

Note: By default, API requests are disabled for non-browser applications. You need to activate them through Account:Security.

FAQ

API calls are disabled for non-browser applications

You need to activate API requests through Account:Security.

Examples

ECMAScript modules

Send the email using the customized send method

import emailjs from '@emailjs/nodejs';

const templateParams = {
  name: 'James',
  notes: 'Check this out!',
};

emailjs
  .send('YOUR_SERVICE_ID', 'YOUR_TEMPLATE_ID', templateParams, {
    publicKey: 'YOUR_PUBLIC_KEY',
    privateKey: 'YOUR_PRIVATE_KEY', // optional, highly recommended for security reasons
  })
  .then(
    (response) => {
      console.log('SUCCESS!', response.status, response.text);
    },
    (err) => {
      console.log('FAILED...', err);
    },
  );

init (optional)

import emailjs from '@emailjs/nodejs';

// set Public Key as global settings
emailjs.init({
  publicKey: 'YOUR_PUBLIC_KEY',
  privateKey: 'YOUR_PRIVATE_KEY', // optional, highly recommended for security reasons
});

emailjs.send('YOUR_SERVICE_ID', 'YOUR_TEMPLATE_ID').then(
  (response) => {
    console.log('SUCCESS!', response.status, response.text);
  },
  (err) => {
    console.log('FAILED...', err);
  },
);

await/async with EmailJS error handler

import emailjs, { EmailJSResponseStatus } from '@emailjs/nodejs';

try {
  await emailjs.send(
    'YOUR_SERVICE_ID',
    'YOUR_TEMPLATE_ID',
    {},
    {
      publicKey: 'YOUR_PUBLIC_KEY',
      privateKey: 'YOUR_PRIVATE_KEY', // optional, highly recommended for security reasons
    },
  );
  console.log('SUCCESS!');
} catch (err) {
  if (err instanceof EmailJSResponseStatus) {
    console.log('EMAILJS FAILED...', err);
    return;
  }

  console.log('ERROR', err);
}

Configuration

Options

Options can be declared globally using the init method or locally as the fourth parameter of a function.
The local parameter will have higher priority than the global one.

NameTypeDefaultDescription
publicKeyStringThe public key is required to invoke the method.
blockListBlockListBlock list settings.
limitRateLimitRateLimit rate configuration.
storageProviderStorageProviderProvider for a custom key-value storage.

BlockList

Allows to ignore a method call if the watched variable contains a value from the block list.
The method will return the error 403 if the request is blocked.

NameTypeDescription
listString[]The array of strings contains values for blocking.
watchVariableStringA name of the variable to be watched.

LimitRate

Allows to set the limit rate for calling a method.
If the request hits the limit rate, the method will return the error 429.

NameTypeDefaultDescription
idStringpage pathThe limit rate is per page by default. To override the behavior, set the ID. It can be a custom ID for each page, group, or application.
throttleNumber(ms) After how many milliseconds a next request is allowed.

StorageProvider

Allows to provide a custom key value storage. By default, localStorage is used if available.
The custom provider must match the interface.

interface StorageProvider {
  get: (key: string) => Promise<string | null | undefined>;
  set: (key: string, value: string) => Promise<void>;
  remove: (key: string) => Promise<void>;
}

Declare global settings

import emailjs from '@emailjs/nodejs';

emailjs.init({
  publicKey: 'YOUR_PUBLIC_KEY',
  privateKey: 'YOUR_PRIVATE_KEY', // optional, highly recommended for security reasons
  blockList: {
    list: ['[email protected]', '[email protected]'],
  },
  limitRate: {
    throttle: 10000, // 10s
  },
});

Overwrite settings locally

import emailjs from '@emailjs/nodejs';

const templateParams = {
  name: 'James',
  notes: 'Check this out!',
};

emailjs
  .send('YOUR_SERVICE_ID', 'YOUR_TEMPLATE_ID', templateParams, {
    publicKey: 'YOUR_PUBLIC_KEY',
    privateKey: 'YOUR_PRIVATE_KEY', // optional, highly recommended for security reasons
    blockList: {
      watchVariable: 'userEmail',
    },
    limitRate: {
      throttle: 0, // turn off the limit rate for these requests
    },
  })
  .then(
    (response) => {
      console.log('SUCCESS!', response.status, response.text);
    },
    (err) => {
      console.log('FAILED...', err);
    },
  );

版本与发布记录

@emailjs/nodejs 共发布 13 个版本,首次发布于 2022/11/02;过去 12 个月发布了 0 个版本。

版本发布日期距今
5.0.2latest2024/10/222 年前
5.0.12024/10/212 年前
5.0.02024/10/212 年前
4.1.02024/10/022 年前
4.0.42024/07/122 年前
4.0.32024/07/122 年前

关于 @emailjs/nodejs 的常见问题

@emailjs/nodejs 是什么?

Official EmailJS SDK for NodeJS

如何安装 @emailjs/nodejs?

在项目目录运行 npm install @emailjs/nodejs,或使用 pnpm add / yarn add / bun add。

@emailjs/nodejs 支持 TypeScript 吗?

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

@emailjs/nodejs 支持 ESM 还是 CommonJS?

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

@emailjs/nodejs 需要什么版本的 Node.js?

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

@emailjs/nodejs 可以商用吗?

@emailjs/nodejs 使用 BSD-3-Clause 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。另外不得用原作者名义为衍生产品背书。以上为概括,不构成法律意见,请以许可原文为准。

@emailjs/nodejs 的下载量有多少?

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

@emailjs/nodejs 还在维护吗?

最新版本 5.0.2 发布于 2024/10/22(2 年前);至今共发布 13 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/11/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。