通过 API 发送
本领域的主题:
API 概述
服务器地址、必填参数、JSON 响应
API 详情
字符集、内容类型、可选参数、JSON 响应
API 示例
PHP 和 cURL 示例
带附件的 API 示例
带附件的 PHP 和 cURL 示例
本领域的主题:
服务器地址、必填参数、JSON 响应
字符集、内容类型、可选参数、JSON 响应
PHP 和 cURL 示例
带附件的 PHP 和 cURL 示例
RealSender 允许您通过 API(应用程序接口)发送电子邮件。
这样,您就可以直接从应用程序发送电子邮件,无需通过 SMTP(简单邮件传输协议)。目前我们仅支持POST 请求。
服务器地址:
https://rsXXX-api.realsender.com/mail/send
必填参数:
| apiuser | 用户名 |
| apipass | 认证密码 |
| 来自 | 发件人电子邮件地址 |
| 到 | 收件人电子邮件地址 |
| 主题 | 邮件主题 |
| 文本 | 纯文本的电子邮件正文 |
| html | HTML格式的邮件正文 |
如果一切正常,消息将被发送,您将收到一条成功的 JSON 响应:
{"success":true}
如果出现错误,你会看到类似以下的内容:
{"success":false,"errorMsgs":["Please provide the 'subject' value."]}
内容必须使用UTF-8 国际字符集发送。
要进行测试,请在主题中添加“€uro”并提交。如果字符集不正确,您将收到以下 JSON 警告:
{"success":false,"errorMsgs":["The 'subject' value is not correctly encoded. It must be UTF-8 encoded."]}
根据您是否填写了“text”和“html”其中一个或两个字段,消息将使用以下其中一种“Content-Type”进行发送:
| 文本 | 纯文本(仅文本) |
| html | text/html(仅限HTML) |
| 文本+HTML | multipart/alternative(同时包含文本和HTML) 具体显示哪一部分取决于电子邮件客户端的设置 |
非必需/可选参数:
| fromname | 发件人描述 |
| toname | 收件人描述 |
| 回复 | 用于接收回复的电子邮件地址 |
| 返回路径 | 用于接收退信的电子邮件地址 ,该地址必须包含在 RealSender 的授权发件人列表中 |
| 抄送 | 抄送邮箱地址 |
| ccname | 副本描述 |
| 密件抄送 | 密件副本(BCC)邮箱地址 |
| bccname | 密件副本说明 |
| 附加 | 待附加的文件——可在表单中出现多次——最大大小为 3MB 文件内容必须作为多部分 HTTP POST 请求的一部分 对于 INPUT TYPE=FILE,必须设置 enctype="multipart/form-data" |
“收件人”、“抄送”和“密送”字段可以包含单个电子邮件地址,也可以包含以逗号分隔的电子邮件地址列表。
!! 在 RealSender 中,每封电子邮件的收件人总数限制为 25 个(最多可增加至 100 个)。
服务器的响应采用 JSON(JavaScript 对象表示法)格式:
| 邮件已发送 | {"success":true} |
| 电子邮件未发送 | {"success":false,"errorMsgs":["..."]} |
向
发送POST 请求
使用 PHP 实现(不使用 CURL)
<?php
$url = 'https://rsXXX-api.realsender.com/mail/send';
$data = array('apiuser' => 'the one we provided you', 'apipass' => 'the one we provided you', 'from' => 'sender@example.com', 'to' => 'recipient@example.com', 'subject' => 'subject of the message', 'text' => 'email body in plain text', 'html' => 'email body in HTML format');
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
?>POST 请求
CURL 方法
curl -d 'apiuser=我们提供的用户名&apipass=我们提供的密码 you&from=sender@example.com&to=recipient@example.com&subject=邮件主题&text=纯文本格式的邮件正文&html=HTML格式的邮件正文'https://rsXXX-api.realsender.com/mail/send带有附件的 POST 请求(最多 5 个:attach1、attach2、…)
使用 PHP 的非 CURL 方法
<?php
require_once 'HTTP/Request2.php';
$config = array('use_brackets' => false,
);
$request = new HTTP_Request2('https://rsXXX-api.realsender.com/mail/send',
HTTP_Request2::METHOD_POST,
$config);
$data = array('apiuser' => 'the one we provided you',
'apipass' => 'the one we provided you',
'from' => 'sender@example.com',
'to' => 'recipient@example.com',
'subject' => 'subject of the message',
'text' => 'email body in plain text',
'html' => 'email body in HTML format');
foreach ($data as $k => $d) {
$request->addPostParameter($k, $d);
};
$request->addUpload('attach1', './sample.pdf', 'sample.pdf', 'application/pdf');
$request->addUpload('attach2', './sample.txt', 'sample.txt', 'text/plain');
$result = $request->send();
var_dump($result);
?>带附件的 POST 请求
CURL 方法
curl -F 'apiuser=我们提供的用户名' \
-F 'apipass=我们提供的密码' \
-F 'from=sender@example.com' \
-F 'to=recipient@example.com' \
-F 'subject=邮件主题' \
-F 'text=纯文本格式的邮件正文' \
-F 'html=HTML格式的邮件正文' \
-F 'attach=@sample.pdf;type=application/pdf' \
-F 'attach=@sample.txt;type=text/plain' \
https://rsXXX-api.realsender.com/mail/send