---
title: "发送邮件 | Tajo"
description: "通过 Brevo API 发送事务性邮件"
url: https://tajo.io/zh-cn/docs/email-messaging/transactional-email/send-email/
markdown: https://tajo.io/zh-cn/docs/email-messaging/transactional-email/send-email.md
lang: zh-CN
author: "Set Noa"
author_url: https://tajo.io/zh-cn/set-noa/
robots: noindex
---

申请抢先体验

请输入您的名字和公司电子邮箱地址。我们会发送确认 Tajo 访问申请的链接。

# 发送邮件

[Set Noa](https://tajo.io/zh-cn/set-noa/)

使用 `/v3/smtp/email` 端点发送单封事务性邮件。

## 端点

```http
POST /v3/smtp/email
```

## 请求体

### 基础邮件

```json
{
  "sender": {
    "name": "Your App",
    "email": "noreply@yourapp.com"
  },
  "to": [
    {
      "email": "user@example.com",
      "name": "John Doe"
    }
  ],
  "subject": "Welcome to our service!",
  "htmlContent": "<html><body><h1>Welcome!</h1><p>Thank you for signing up.</p></body></html>"
}
```

### 进阶邮件

```json
{
  "sender": {
    "name": "Your App",
    "email": "noreply@yourapp.com"
  },
  "to": [
    {
      "email": "user@example.com",
      "name": "John Doe"
    }
  ],
  "cc": [
    {
      "email": "cc@example.com",
      "name": "CC User"
    }
  ],
  "bcc": [
    {
      "email": "bcc@example.com"
    }
  ],
  "subject": "Order Confirmation #{{order_id}}",
  "htmlContent": "<html><body><h1>Order Confirmed</h1><p>Your order {{order_id}} has been confirmed.</p></body></html>",
  "textContent": "Your order {{order_id}} has been confirmed.",
  "params": {
    "order_id": "12345"
  },
  "tags": ["transactional", "order-confirmation"],
  "headers": {
    "X-Custom-Header": "custom-value"
  }
}
```

## 代码示例

### JavaScript/Node.js

```javascript
const brevo = require('@getbrevo/brevo');

const apiInstance = new brevo.TransactionalEmailsApi();
apiInstance.setApiKey(brevo.TransactionalEmailsApiApiKeys.apiKey, process.env.BREVO_API_KEY);

const sendEmail = async () => {
  const sendSmtpEmail = new brevo.SendSmtpEmail();

  sendSmtpEmail.subject = "Welcome to our service!";
  sendSmtpEmail.htmlContent = "<html><body><h1>Welcome!</h1></body></html>";
  sendSmtpEmail.sender = { name: "Your App", email: "noreply@yourapp.com" };
  sendSmtpEmail.to = [{ email: "user@example.com", name: "John Doe" }];

  try {
    const result = await apiInstance.sendTransacEmail(sendSmtpEmail);
    console.log('Email sent:', result);
    return result;
  } catch (error) {
    console.error('Error sending email:', error);
    throw error;
  }
};
```

### Python

```python
import sib_api_v3_sdk
from sib_api_v3_sdk.rest import ApiException

configuration = sib_api_v3_sdk.Configuration()
configuration.api_key['api-key'] = 'YOUR_API_KEY'

api_instance = sib_api_v3_sdk.TransactionalEmailsApi(sib_api_v3_sdk.ApiClient(configuration))

send_smtp_email = sib_api_v3_sdk.SendSmtpEmail(
    to=[{"email": "user@example.com", "name": "John Doe"}],
    sender={"name": "Your App", "email": "noreply@yourapp.com"},
    subject="Welcome to our service!",
    html_content="<html><body><h1>Welcome!</h1></body></html>"
)

try:
    api_response = api_instance.send_transac_email(send_smtp_email)
    print(api_response)
except ApiException as e:
    print("Exception when calling TransactionalEmailsApi->send_transac_email: %s\n" % e)
```

### cURL

```bash
curl -X POST "https://api.brevo.com/v3/smtp/email" \
  -H "Accept: application/json" \
  -H "Content-Type: application/json" \
  -H "api-key: YOUR_API_KEY" \
  -d '{
    "sender": {
      "name": "Your App",
      "email": "noreply@yourapp.com"
    },
    "to": [
      {
        "email": "user@example.com",
        "name": "John Doe"
      }
    ],
    "subject": "Welcome to our service!",
    "htmlContent": "<html><body><h1>Welcome!</h1></body></html>"
  }'
```

## 响应

### 成功响应（201 Created）

```json
{
  "messageId": "<202301151234.12345@smtp-relay.mailin.fr>"
}
```

### 错误响应

**API 密钥无效（401）**

```json
{
  "code": "unauthorized",
  "message": "Invalid API key provided"
}
```

**邮箱格式无效（400）**

```json
{
  "code": "invalid_parameter",
  "message": "Invalid email format in 'to' field"
}
```

**超出速率限制（429）**

```json
{
  "code": "too_many_requests",
  "message": "Rate limit exceeded"
}
```

## 参数

### 必填参数

| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `sender` | object | 发件人信息 |
| `to` | array | 收件人对象数组 |
| `subject` | string | 邮件主题行 |

### 可选参数

| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `htmlContent` | string | 邮件的 HTML 内容 |
| `textContent` | string | 邮件的纯文本内容 |
| `cc` | array | 抄送收件人 |
| `bcc` | array | 密送收件人 |
| `replyTo` | object | 回复地址 |
| `attachments` | array | 文件附件 |
| `params` | object | 模板参数 |
| `tags` | array | 用于追踪的邮件标签 |
| `headers` | object | 自定义请求头 |

## 模板变量

在主题和内容中使用模板变量：

```json
{
  "subject": "Welcome {{firstName}}!",
  "htmlContent": "<h1>Hello {{firstName}} {{lastName}}</h1>",
  "params": {
    "firstName": "John",
    "lastName": "Doe"
  }
}
```

## 附件

添加文件附件：

```json
{
  "attachments": [
    {
      "content": "base64_encoded_content",
      "name": "invoice.pdf"
    },
    {
      "url": "https://example.com/document.pdf",
      "name": "document.pdf"
    }
  ]
}
```

## 最佳实践

1. **同时提供 HTML 和纯文本内容**，可提升送达率
2. **使用模板变量**，不要拼接内容字符串
3. **填写规范的发件人信息**，避免被垃圾邮件过滤器拦截
4. **添加标签**，便于追踪和分析
5. **发送前校验邮箱地址**
6. **优雅处理错误**，配合重试逻辑

## 速率限制

- **API 速率限制**：在每种套餐都适用的通用档位上，此端点允许每秒 1,000 次请求、每小时 3,600,000 次。高级档位（Professional 和 Enterprise）允许每秒 2,000 次，扩展档位（Enterprise）允许每秒 6,000 次。
- **发送量**：你能发送多少封邮件由你的 Brevo 套餐决定，与速率限制分开计算。

请参阅[速率限制](https://tajo.io/zh-cn/docs/rate-limits/)以及 Brevo 的[速率限制页面](https://developers.brevo.com/docs/api-limits)。

## 追踪

通过该端点发送的邮件会自动追踪以下数据：

- 送达状态
- 打开率
- 点击追踪
- 退信处理

在 Brevo 仪表板的 **Statistics** → **Transactional** 中查看详细分析。

## 相关端点

- 邮件模板
- 附件
- [邮件统计](https://tajo.io/zh-cn/docs/analytics-reporting/)
