快速入门
创建测试 API 密钥并使用模拟收件地址。测试邮件会运行 API 并记录模拟结果,无需实际投递邮件。
模拟器暂不可用。
-
设置工作区
创建账号,确认邮箱地址,然后登录。
-
创建 API 密钥
创建密钥时请选择测试模式。测试密钥的前缀为 mg_test_。
在工作区创建密钥,将密钥的机密值保存在服务器环境中。请勿在浏览器端代码、代码仓库或日志中暴露该值。
-
发送模拟邮件
在 Shell 中设置 MAILGATE_API_KEY,然后执行此请求。模拟器使用此处显示的示例发件人。
将 MAILGATE_API_KEY 保存在终端环境或后端的机密管理工具中。
cURL
POST /api/v1/emailscurl --fail-with-body --request POST https://mailgateapi.com/api/v1/emails \ --header "Authorization: Bearer ${MAILGATE_API_KEY:?Set MAILGATE_API_KEY}" \ --header "Content-Type: application/json" \ --header "Idempotency-Key: first-test-1" \ --data '{ "from": "noreply@simulator.mailgate.test", "to": "delivered@simulator.mailgate.test", "subject": "My first test", "text": "Hello from the MailGateAPI simulator." }'PHP
POST /api/v1/emails<?php $key = getenv('MAILGATE_API_KEY') ?: throw new RuntimeException('Set MAILGATE_API_KEY'); $request = curl_init('https://mailgateapi.com/api/v1/emails'); curl_setopt_array($request, [ CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 20, CURLOPT_HTTPHEADER => [ 'Authorization: Bearer '.$key, 'Content-Type: application/json', 'Idempotency-Key: first-test-1', ], CURLOPT_POSTFIELDS => json_encode([ 'from' => 'noreply@simulator.mailgate.test', 'to' => 'delivered@simulator.mailgate.test', 'subject' => 'My first test', 'text' => 'Hello from the MailGateAPI simulator.', ], JSON_THROW_ON_ERROR), ]); $body = curl_exec($request); if ($body === false) { throw new RuntimeException(curl_error($request)); } echo $body, PHP_EOL; exit(curl_getinfo($request, CURLINFO_RESPONSE_CODE) === 202 ? 0 : 1);Node.js
POST /api/v1/emailsconst key = process.env.MAILGATE_API_KEY; if (!key) throw new Error('Set MAILGATE_API_KEY'); fetch('https://mailgateapi.com/api/v1/emails', { method: 'POST', signal: AbortSignal.timeout(20_000), headers: { Authorization: `Bearer ${key}`, 'Content-Type': 'application/json', 'Idempotency-Key': 'first-test-1', }, body: JSON.stringify({ from: 'noreply@simulator.mailgate.test', to: 'delivered@simulator.mailgate.test', subject: 'My first test', text: 'Hello from the MailGateAPI simulator.', }), }).then(async (response) => { console.log(await response.text()); if (response.status !== 202) process.exitCode = 1; }).catch((error) => { console.error(error.message); process.exitCode = 1; });Python
POST /api/v1/emailsimport json import os from urllib.error import HTTPError from urllib.request import Request, urlopen request = Request( "https://mailgateapi.com/api/v1/emails", method="POST", headers={ "Authorization": "Bearer " + os.environ["MAILGATE_API_KEY"], "Content-Type": "application/json", "Idempotency-Key": "first-test-1", }, data=json.dumps({ "from": "noreply@simulator.mailgate.test", "to": "delivered@simulator.mailgate.test", "subject": "My first test", "text": "Hello from the MailGateAPI simulator.", }).encode("utf-8"), ) try: with urlopen(request, timeout=20) as response: print(response.read().decode("utf-8")) except HTTPError as error: print(error.read().decode("utf-8")) raise SystemExit(1)202 响应包含邮件 ID。控制台的测试活动会显示模拟结果。发送其他邮件时,请使用新的 Idempotency-Key。
-
验证发信域名
服务启用后,使用已验证的发信域名和正式环境的 API 密钥发送真实邮件。
在工作区添加域名。发布设置页面显示的 DNS 记录,等待验证完成。
模拟收件地址
| 收件人 | 结果 |
|---|---|
delivered@simulator.mailgate.test |
模拟送达 |
bounced@simulator.mailgate.test |
模拟退信 |
complained@simulator.mailgate.test |
模拟投诉 |