申请抢先体验

请填写名字,以及邮箱或手机号。我们会与您联系,提供 Tajo 访问详情。

发送邮件

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

端点

POST /v3/smtp/email

请求体

基础邮件

{
"sender": {
"name": "Your App",
"email": "[email protected]"
},
"to": [
{
"email": "[email protected]",
"name": "John Doe"
}
],
"subject": "Welcome to our service!",
"htmlContent": "<html><body><h1>Welcome!</h1><p>Thank you for signing up.</p></body></html>"
}

进阶邮件

{
"sender": {
"name": "Your App",
"email": "[email protected]"
},
"to": [
{
"email": "[email protected]",
"name": "John Doe"
}
],
"cc": [
{
"email": "[email protected]",
"name": "CC User"
}
],
"bcc": [
{
"email": "[email protected]"
}
],
"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

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: "[email protected]" };
sendSmtpEmail.to = [{ email: "[email protected]", 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

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": "[email protected]", "name": "John Doe"}],
sender={"name": "Your App", "email": "[email protected]"},
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

Terminal window
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": "[email protected]"
},
"to": [
{
"email": "[email protected]",
"name": "John Doe"
}
],
"subject": "Welcome to our service!",
"htmlContent": "<html><body><h1>Welcome!</h1></body></html>"
}'

响应

成功响应(201 Created)

{
"messageId": "<[email protected]>"
}

错误响应

API 密钥无效(401)

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

邮箱格式无效(400)

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

超出速率限制(429)

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

参数

必填参数

参数类型说明
senderobject发件人信息
toarray收件人对象数组
subjectstring邮件主题行

可选参数

参数类型说明
htmlContentstring邮件的 HTML 内容
textContentstring邮件的纯文本内容
ccarray抄送收件人
bccarray密送收件人
replyToobject回复地址
attachmentsarray文件附件
paramsobject模板参数
tagsarray用于追踪的邮件标签
headersobject自定义请求头

模板变量

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

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

附件

添加文件附件:

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

最佳实践

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

速率限制

  • 免费账号:每天 300 封邮件
  • 付费账号:取决于你的套餐
  • 突发上限:每分钟 100 封邮件

追踪

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

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

在 Brevo 仪表板的 StatisticsTransactional 中查看详细分析。

相关端点

申请抢先体验

请填写名字,以及邮箱或手机号。我们会与您联系,提供 Tajo 访问详情。

自动识别
AI 助手

你好!关于文档有任何问题都可以问我。