邮件系统在现代应用中无处不在,但从多账户中快速检索邮件一直是个挑战。RustMailer 作为一个 自托管 IMAP/SMTP 中间件,不仅提供丰富的 API 支持,还能通过高性能的索引表实现毫秒级的邮件搜索,非常适合 CRM、客户支持系统和销售管理工具。
RustMailer 是 source available ,可以在 GitHub 查看源码。它提供 gRPC 和 RESTful 两种 API 接口,方便开发者无缝集成邮件功能到现有应用中。
🔍 API 目标
RustMailer 的 /unified-search
API 旨在 快速检索本地缓存邮件,支持以下查询条件:
- 邮件地址(from/to/cc)
- 时间范围
- 邮箱账户范围
该 API 避免了每次搜索都连接 IMAP 服务器,通过预构建的索引表实现快速查询,适合需要高响应速度的场景。
🧠 用例示例:在 CRM 中检索客户邮件历史
在 CRM 中,你可能需要:
- 在客户档案页显示最近的邮件互动
- 快速定位某客户在多个邮箱账户中的所有沟通
- 按时间或方向(收/发)过滤邮件
使用 /unified-search
API,可以在不处理底层 IMAP 协议的情况下,统一检索多账户邮件,实现毫秒级响应。
🛠️ API 定义(OpenAPI)
Endpoint:
http
POST /api/v1/unified-search
请求参数(Query):
参数 | 类型 | 描述 |
---|---|---|
page |
u64 |
当前页码(从 1 开始) |
page_size |
u64 |
每页邮件数量 |
desc |
bool |
按时间降序排序(默认升序) |
请求体(JSON):
json
{
"email": "customer@example.com",
"after": 1719859200000,
"before": 1722451200000,
"accounts": [1, 2, 5]
}
after
/before
:可选时间范围(UTC 毫秒)accounts
:可选邮箱账户列表,省略则检索所有可访问账户
✅ 示例请求(curl)
bash
curl -X POST https://your-server/api/v1/unified-search?page=1&page_size=50&desc=true \
-H "Authorization: Bearer <token>" \
-H "Content-Type: application/json" \
-d '{
"email": "alice@example.com",
"after": 1720000000000,
"before": 1720500000000
}'
📦 示例响应(简化)
json
{
"total": 12,
"page": 1,
"page_size": 50,
"items": [
{
"account_id": 3127439432099413,
"mailbox_id": 15004652606885140286,
"mailbox_name": "INBOX",
"uid": 241,
"internal_date": 1752546329000,
"size": 13427,
"flags": [],
"flags_hash": 0,
"bcc": [],
"cc": [],
"date": 1752546326000,
"from": {
"name": "Google",
"address": "no-reply@accounts.google.com"
},
"in_reply_to": null,
"sender": null,
"return_address": "3Frx1aAgTCa8cd-gTeanPRRdjcih.VddVaT.RdbedaanQPhTodWdbPXa.Rdb@identity-reachout.bounces.google.com",
"message_id": "Iigig8Ybeha8HTsMwYTK6w@notifications.google.com",
"subject": "Security alert for rustmailer.git@gmail.com",
"thread_name": "Security alert for rustmailer.git@gmail.com",
"mime_version": "1.0",
"references": [],
"reply_to": [],
"to": [
{
"name": null,
"address": "rustmailer.git@gmail.com"
}
],
"attachments": [],
"body_meta": [
{
"id": "z7tjSOVqMHMg",
"part_type": "Plain",
"path": {
"segments": [1]
},
"params": [
{
"key": "charset",
"value": "UTF-8"
},
{
"key": "delsp",
"value": "yes"
},
{
"key": "format",
"value": "flowed"
}
],
"size": 1350,
"transfer_encoding": "Base64"
},
{
"id": "xIk8OtgQ0bbJ",
"part_type": "Html",
"path": {
"segments": [2]
},
"params": [
{
"key": "charset",
"value": "UTF-8"
}
],
"size": 6441,
"transfer_encoding": "QuotedPrintable"
}
],
"received": {
"from": null,
"by": "mail-qv1-f72.google.com",
"with": "SMTP",
"date": 1752546327000
}
},
...
]
}
⚡ 性能说明:基于索引的快速查询
/unified-search
利用 RustMailer 内部的 邮件索引表,只存储元数据(主题、发件人、收件人、时间戳),不缓存完整邮件内容或附件。- 即便缓存了 数百万封邮件 ,也能实现 亚秒级分页查询。
- 用户点击邮件查看内容时,RustMailer 会通过独立 API 从 IMAP 拉取邮件正文和附件,并本地缓存,避免重复请求,同时自动清理过期数据。
🔗 获取支持与社区交流
如果你有问题或需要帮助,可以通过以下方式联系:
如果你觉得 RustMailer 有用,欢迎 给项目点个 star!