【Next.js 入门教程系列】08-发送邮件

原文链接

CSDN 的排版/样式可能有问题,去我的博客查看原文系列吧,觉得有用的话, 给我的点个star,关注一下吧

上一篇**【Next.js 入门教程系列】07-身份验证**

发送邮件

Setting Up React Email****

React Email 是一个高效便捷的 Email 库,包含多个组件,包括编写,发送等等功能。使用 npm i react-email @react-email/components 安装

安装好打开 package.json,在 scripts 中添加 "preview-email": "email dev -p 5051" script

复制代码
"scripts": {
  "dev": "next dev -- -p 5050",
  "build": "next build",
  "start": "next start -- -p 5050",
  "lint": "next lint",
  // Add this
  "preview-email": "email dev -p 5051"
},

Careate Email Template****

本章代码链接

在根目录下(app 同级目录)创建 emails 文件夹,在其中创建 WelcomeTemplate.tsx。如下就是一个邮件的模板,会将输入用户的名字添加到其中

TypeScript 复制代码
# emails/WelcomeTemplate.tsx

import React from "react";
import {
  Html,
  Body,
  Container,
  Text,
  Link,
  Preview,
} from "@react-email/components";

const WelcomeTemplate = ({ name }: { name: string }) => {
  return (
    <Html>
      <Preview>Welcome aborad!</Preview>
      <Body>
        <Container>
          <Text>Hello {name}</Text>
          <Link href="http://dino.castamerego.com">
            www.dino.castamerego.com
          </Link>
        </Container>
      </Body>
    </Html>
  );
};
export default WelcomeTemplate;

Preview Email****

本章代码链接

首先在 .gitignore 中添加 .react-email,以防产生的大量文件污染 git。使用 npm preview-email 命令, 打开浏览器,访问对应端口(笔者设置的是 localhost:5051)即可看到

Style Email****

本章代码链接

我们可以直接使用 CSS 来添加样式,也可以直接使用 Tailwind,这里把两种都给出

  • CSS
  • TailWind
TypeScript 复制代码
# emails/WelcomTemplate.tsx

// Use CSS
import React, { CSSProperties } from "react";
import {
  Html,
  Body,
  Container,
  Text,
  Link,
  Preview,
} from "@react-email/components";

const WelcomeTemplate = ({ name }: { name: string }) => {
  return (
    <Html>
      <Preview>Welcome aborad!</Preview>
      <Body style={body}>
        <Container>
          <Text style={heading}>Hello {name}</Text>
          <Link href="http://dino.castamerego.com">
            www.dino.castamerego.com
          </Link>
        </Container>
      </Body>
    </Html>
  );
};

const body: CSSProperties = {
  background: "#fff",
};

const heading: CSSProperties = {
  fontSize: "32px",
};

export default WelcomeTemplate;
TypeScript 复制代码
# emails/WelcomTemplate.tsx

// Use TailWind
import React, { CSSProperties } from "react";
import {
  Html,
  Body,
  Container,
// import Tailwind
  Tailwind,
  Text,
  Link,
  Preview,
} from "@react-email/components";

const WelcomeTemplate = ({ name }: { name: string }) => {
  return (
    <Html>
      <Preview>Welcome aborad!</Preview>
      {/* 将 Body 用 <Tailwind> 包起来*/}
      <Tailwind>
        <Body className="bg-white">
          <Container>
            <Text className="font-bold text-3xl">Hello {name}</Text>
            <Link href="http://dino.castamerego.com">
              www.dino.castamerego.com
            </Link>
          </Container>
        </Body>
      </Tailwind>
    </Html>
  );
};

export default WelcomeTemplate;

Sending Emails****

本章代码链接

使用 npm i resend@1.0.0 安装 resend 用于发送邮件。进入Resend官网,注册账号,获取一个 API Key,并添加到 .env 中,设置 RESEND_API_KEY=... 即可。在 api/ 中添加 send-email/route.tsx,调用 resend.emails.send() 即可

TypeScript 复制代码
# api/send-email/route.tsx

import WelcomeTemplate from "@/emails/WelcomeTemplate";
import { NextResponse } from "next/server";
import { Resend } from "resend";

const resend = new Resend(process.env.RESEND_API_KEY!);

export async function POST() {
  await resend.emails.send({
    from: "...",
    to: "castamere@gmail.com",
    subject: "...",
    react: <WelcomeTemplate name="Castamere" />,
  });

  return NextResponse.json({});
}

下一篇讲优化技巧

下一篇【Next.js 入门教程系列】09-优化技巧

相关推荐
We་ct1 天前
LeetCode 72. 编辑距离:动态规划经典题解
前端·算法·leetcode·typescript·动态规划
你真的快乐吗1 天前
@fuxishi/svg-icon:一个 Vue 3 svg本地图标+iconify图标组件库,让图标管理不再头疼
前端·vue.js·typescript
你好潘先生1 天前
Next.js + Spring Boot 实现 AI 多模型并行对话系统(架构设计与关键实现)
spring boot·向量检索·next.js·pgvector·ai对话·多模型对比·sse流式输出
运维全栈笔记2 天前
Linux安装配置Tomcat保姆级教程:从部署到性能调优
linux·服务器·中间件·tomcat·apache·web
必胜刻2 天前
全面解析 Token:从入门到 JWT 实战
golang·状态模式·web·前后端交互
DevilSeagull2 天前
电脑上安装的服务会自动消失? 推荐项目: localhostSCmanager. 更好管理你的服务!
测试工具·安全·react·vite·localhost·hono·trpc
曲幽2 天前
FastAPI 少有人提的实用技巧:把 Depends 依赖提到路由层,代码少写60%
python·fastapi·web·routes·depends·prefix·apiroute
Wect2 天前
LeetCode 72. 编辑距离:动态规划经典题解
前端·算法·typescript
漫游的渔夫2 天前
前端开发者做 Agent:别只会执行,用 4 类失败策略让 AI 知道怎么停
前端·人工智能·typescript
We་ct2 天前
LeetCode 97. 交错字符串:动态规划详解
前端·算法·leetcode·typescript·动态规划