如何使用 JavaScript 发送电子邮件:完整指南(使用 SendLayer API)
Source: Dev.to
前置条件
- 在你的机器上已安装 Node.js。前往此处下载最新版本。
- 代码编辑器(例如 Visual Studio Code)。
- 基本的 HTML 与 JavaScript 知识。
- 一个 SendLayer 账户。你可以使用免费试用账户,免费发送最多 200 封邮件。
在 SendLayer 开始免费试用
创建 SendLayer 账户后,请确保授权你的域名。此步骤对于提升站点的邮件投递率至关重要。
通过 SendLayer API 发送邮件(推荐)
使用 API 发送邮件比使用 SMTP 更快且更安全。像 SendLayer 这样的事务性邮件服务提供用于编程发送邮件的 API,并内置高级功能。
本指南将使用 SendLayer 的 SDK。该 SDK 开箱即用地提供了邮件验证、错误处理和文件附件编码等功能。
获取 API 密钥
要使用 SendLayer SDK,你需要一个有效的 API 密钥。登录你的SendLayer 账户,然后进入 Settings » API Keys。

复制你的 API 密钥——我们将在后续步骤中使用它。
安装 SendLayer SDK
npm install sendlayer
发送你的第一封邮件
创建一个新的 JavaScript 文件(例如 emailService.js),并添加以下代码:
import { SendLayer } from 'sendlayer';
const apiKey = 'your-sendlayer-api-key';
// Initialize the SendLayer package with your API key
const sendlayer = new SendLayer(apiKey);
const sendEmail = async () => {
// Email data
const params = {
from: 'paulie@example.com',
to: 'recipient@example.com',
subject: 'Test Email via SendLayer API',
text: 'This is a test email sent via the SendLayer API.'
};
// email sending function with error handling
try {
const response = await sendlayer.Emails.send(params);
console.log('Email sent successfully:', response);
} catch (error) {
console.error('Error sending email:', error.message);
}
};
sendEmail();
代码解析
from– 发件人邮箱地址(必须属于已授权的域名)。to– 收件人邮箱地址或收件人列表。subject– 邮件主题行。text或html– 邮件内容(纯文本或 HTML)。
sendlayer.Emails.send() 方法用于发送邮件,try...catch 块用于捕获并处理错误。
运行代码
node emailService.js
如果成功,你会看到类似以下的确认信息:
{
"MessageID": "62e09048-039f-4fce-a744-b096981e4990"
}

发送 HTML 邮件
若要发送 HTML 格式的邮件,请使用 html 参数而不是 text:
const params = {
from: 'paulie@example.com',
to: 'recipient@example.com',
subject: 'Test HTML Email',
html: '
## Hello!
This is an **HTML email** sent via the SendLayer API.
'
};
发送给多个收件人
为多个地址提供一个收件人对象数组:
const params = {
from: { name: "Sender Name", email: "sender@example.com" },
to: [
{ name: "Recipient 1", email: "recipient1@example.com" },
{ name: "Recipient 2", email: "recipient2@example.com" }
],
subject: "Test Email to Multiple Recipients",
html: "
This email is sent to multiple recipients.
"
};
你也可以加入 CC 与 BCC 收件人:
const params = {
from: { name: "Sender Name", email: "sender@example.com" },
to: [{ name: "Recipient 1", email: "recipient1@example.com" }],
subject: "Test Email",
html: "
This email includes CC and BCC recipients.
",
cc: ["cc@example.com"],
bcc: [{ name: "BCC", email: "bcc@example.com" }]
};
注意: 每个邮件请求的收件人数有限制。详情请参阅开发者文档。
发送带附件的邮件
在 params 中添加 attachments 字段。SDK 会自动处理编码。
const params = {
from: { name: 'Sender Name', email: 'sender@example.com' },
to: [{ name: 'Recipient Name', email: 'recipient@example.com' }],
subject: 'Test Email with Attachment',
html: '
This email includes an attachment.
',
attachments: [
{
path: 'path/to/file.pdf',
type: 'application/pdf'
}
]
};
const response = await sendlayer.Emails.send(params);
你可以一次附加多个文件,包括远程 URL:
attachments: [
{
path: 'path/to/file.pdf',
type: 'application/pdf'
},
{
path: 'https://example.com/image.png',
type: 'image/png'
}
]
专业提示: type 字段应与文件的 MIME 类型相匹配,以便邮件客户端正确处理。