从0到1掌握Resend Node.js SDK:构建企业级邮件发送平台
从0到1掌握Resend Node.js SDK:构建企业级邮件发送平台
【免费下载链接】resend-nodeResend's Official Node.js SDK项目地址: https://gitcode.com/gh_mirrors/re/resend-node
Resend Node.js SDK是一个功能强大的Node.js库,专为与Resend API交互而设计,帮助开发者轻松构建企业级邮件发送平台。无论是简单的事务性邮件还是复杂的营销邮件,Resend Node.js SDK都能提供可靠、高效的解决方案。
快速入门:Resend Node.js SDK安装指南
要开始使用Resend Node.js SDK,首先需要安装该库。你可以通过npm或yarn包管理器轻松完成安装:
npm install resend # 或者 yarn add resend安装完成后,你就可以在项目中引入Resend模块,开始构建你的邮件发送系统了。
配置Resend:获取API密钥并初始化
使用Resend Node.js SDK之前,你需要先获取一个API密钥。这个密钥可以在Resend Dashboard中找到。获取密钥后,你可以通过以下方式初始化Resend:
import { Resend } from 'resend'; const resend = new Resend('re_xxxx...xxxxxx');如果你不想在代码中硬编码API密钥,Resend Node.js SDK还支持从环境变量中读取。只需设置RESEND_API_KEY环境变量,SDK会自动获取并使用该密钥。
发送第一封邮件:简单示例
使用Resend Node.js SDK发送邮件非常简单。以下是一个基本的示例,展示如何发送一封简单的文本邮件:
const { data } = await resend.emails.send({ from: 'you@example.com', to: 'user@gmail.com', replyTo: 'you@example.com', subject: 'hello world', text: 'it works!', }); console.log(`Email ${data.id} has been sent`);注意:为了能够从你自己的域名发送邮件,你需要先在Resend Dashboard中验证你的域名。
高级功能:发送HTML和React模板邮件
Resend Node.js SDK不仅支持发送纯文本邮件,还可以发送HTML格式的邮件,甚至可以使用React组件作为邮件模板。
发送HTML邮件
以下示例展示如何发送带有自定义HTML内容的邮件:
const { data } = await resend.emails.send({ from: 'you@example.com', to: 'user@gmail.com', replyTo: 'you@example.com', subject: 'hello world', html: '<strong>it works!</strong>', });使用React模板发送邮件
Resend Node.js SDK还支持使用React组件作为邮件模板。首先,创建一个React组件作为邮件模板:
import React from 'react'; export default function EmailTemplate({ firstName, product }) { return ( <div> <h1>Welcome, {firstName}!</h1> <p>Thanks for trying {product}. We’re thrilled to have you on board.</p> </div> ); }然后,在发送邮件时,将React组件作为react属性传递:
import EmailTemplate from '../components/EmailTemplate'; const { data } = await resend.emails.send({ from: 'you@example.com', to: 'user@gmail.com', replyTo: 'you@example.com', subject: 'hello world', react: <EmailTemplate firstName="John" product="MyApp" />, });如果你的文件没有设置JSX转译,你可以使用React的jsx运行时函数:
import { jsx } from 'react/jsx-runtime' import EmailTemplate from '../components/EmailTemplate'; await resend.emails.send({ from: 'you@example.com', to: 'user@gmail.com', replyTo: 'you@example.com', subject: 'hello world', react: jsx(EmailTemplate, { firstName:"John", product:"MyApp" }), });Resend Node.js SDK核心功能概览
Resend Node.js SDK提供了丰富的功能,除了发送邮件外,还包括以下核心模块:
- apiKeys: 管理API密钥
- domains: 管理域名
- templates: 管理邮件模板
- contacts: 管理联系人
- segments: 管理联系人 segments
- webhooks: 管理webhooks
这些模块可以通过Resend实例直接访问,例如:
// 管理域名 const domains = resend.domains; // 管理模板 const templates = resend.templates;框架集成:与主流Node.js框架无缝对接
Resend Node.js SDK可以与各种Node.js框架无缝集成,包括:
- Next.js
- Remix
- Nuxt
- Express
- RedwoodJS
- Hono
- Bun
- Astro
每种框架都有专门的集成指南,可以帮助你快速在项目中集成Resend邮件发送功能。
开始使用Resend Node.js SDK
要开始使用Resend Node.js SDK构建你的企业级邮件发送平台,首先需要克隆仓库:
git clone https://gitcode.com/gh_mirrors/re/resend-node然后按照上述指南进行安装、配置和使用。Resend Node.js SDK的源码结构清晰,主要功能实现位于src/resend.ts文件中,你可以通过阅读源码深入了解其工作原理。
无论你是构建简单的通知系统还是复杂的营销邮件平台,Resend Node.js SDK都能为你提供可靠、高效的邮件发送解决方案。立即开始使用,体验企业级邮件发送的强大功能!
许可证信息
Resend Node.js SDK采用MIT许可证,详情请参见项目根目录下的LICENSE文件。
【免费下载链接】resend-nodeResend's Official Node.js SDK项目地址: https://gitcode.com/gh_mirrors/re/resend-node
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
