---
agent:
  instructions: "Use this search API to find relevant content across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://docs.sendcorex.com/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://docs.sendcorex.com/llms.txt"
---

Source: https://docs.sendcorex.com/th/api-reference/receive-emails/rules/test-rule

รับอีเมล: กฎ

# Test Rule

ดูว่ากฎจะจับอีเมลล่าสุดในโฟลเดอร์ได้กี่ฉบับก่อนบันทึกหรือเปิดใช้

POST `https://g.sendcorex.com/v1` command: test_rule

ทดสอบกับหัวเรื่อง ผู้ส่ง ผู้รับ และตัวอย่างของอีเมลที่เก็บไว้ เงื่อนไข `body` และ `header` ตรวจเต็มที่เฉพาะเมื่อมีอีเมลใหม่มาถึง

## อาร์กิวเมนต์

- `id` string ไม่บังคับ
กฎที่บันทึกแล้ว (ใช้ได้แม้ปิดอยู่)
- `rule` object ไม่บังคับ
หรือกฎที่ยังไม่บันทึก รูปแบบเดียวกับ Create Rule
- `folder` string ไม่บังคับ
ค่าเริ่มต้น INBOX
- `limit` number ไม่บังคับ
จำนวนอีเมลล่าสุดที่จะตรวจ 1 ถึง 500 ค่าเริ่มต้น 200

## เริ่มต้นอย่างรวดเร็ว

```
import axios from 'axios';

const response = await axios.post(
  'https://g.sendcorex.com/v1',
  {
    commands: [
      {
        command: "test_rule",
        args: {
          rule: {
            name: "Invoices",
            conditions: [
              {
                field: "subject",
                op: "contains",
                value: "invoice"
              }
            ],
            actions: [
              {
                type: "label",
                value: "Finance"
              }
            ]
          },
          folder: "INBOX"
        }
      }
    ]
  },
  { headers: { Authorization: process.env.SENDCOREX_TOKEN } },
);

const [result] = response.data.results;
if (result.type !== 'SUCCESS') throw new Error(`${result.code}: ${result.message}`);
console.log(result.data);
```

## การตอบกลับ

```
{
  "type": "SUCCESS",
  "results": [
    {
      "command": "test_rule",
      "id": "0",
      "type": "SUCCESS",
      "data": {
        "folder": "INBOX",
        "scanned": 200,
        "matched": 12,
        "examples": [
          {
            "message_uid": "Vj0xQUJDMTIz",
            "thread_id": "thr_9a1c4e72",
            "subject": "Invoice #123",
            "from_name": "Ana Costa",
            "from_email": "ana@client.com",
            "received_at": "2026-10-24T03:10:00Z"
          }
        ],
        "would_do": {
          "folder": "",
          "labels": [
            "Finance"
          ],
          "matched": [
            "Invoices"
          ]
        }
      }
    }
  ]
}
```

### ฟิลด์การตอบกลับ

- `matched`
จำนวนอีเมลที่สแกนแล้วตรง
- `examples`
สูงสุด 10 รายการ
- `would_do`
สิ่งที่กฎทำเมื่อตรง

## ข้อผิดพลาด

ข้อผิดพลาดระดับ HTTP ส่งกลับ `type`, `code` และ `message` ข้อผิดพลาดที่มีเครื่องหมาย CMD จะกลับมาในผลลัพธ์ของคำสั่ง พร้อม HTTP 200

- 401`TOKEN_EXPIRED`
access token หมดอายุแล้ว เรียก Refresh Token แล้วส่งคำขอซ้ำ
- 401`INVALID_TOKEN`
โทเค็นผิดรูปแบบ ไม่รู้จัก หรือถูกเพิกถอน
- 429`RATE_LIMITED`
เกิน 15 คำขอ/วินาที หรือ 15 คำขอพร้อมกันสำหรับ IP + กล่องจดหมายเดียวกัน รอจำนวนวินาทีใน Retry-After
- CMD`INVALID_ARGS`
อาร์กิวเมนต์ที่จำเป็นหายไปหรือไม่ถูกต้อง
- CMD`INVALID_RULE`
กฎไม่ถูกต้อง ข้อความบอกว่าเงื่อนไขหรือการกระทำใดและเพราะอะไร
- CMD`NOT_FOUND`
ไม่มีข้อความ เธรด หรือโฟลเดอร์นี้สำหรับโทเค็นนี้
- CMD`INTERNAL`
ข้อผิดพลาดของเซิร์ฟเวอร์ รายละเอียดเขียนเฉพาะในล็อกเซิร์ฟเวอร์
