SDK สำหรับ PHP
ติดตั้ง craftintertech/mailcraft ผ่าน Composer แล้วส่งอีเมลจาก PHP 8.1+ โดยไม่ต้องใช้ Guzzle
SDK ทางการสำหรับ PHP 8.1 ขึ้นไป ใช้ ext-curl ที่มากับ PHP เกือบทุกโฮสต์ ไม่ต้องติดตั้ง Guzzle รองรับ PSR-4 ใช้ได้ทั้งกับ Laravel, Symfony หรือ PHP ธรรมดา
ถ้าเว็บของคุณเป็น WordPress ใช้ปลั๊กอิน MailCraft จะง่ายกว่า
ติดตั้ง
composer require craftintertech/mailcraft
ส่งอีเมลฉบับแรก
<?php
require 'vendor/autoload.php';
use CraftIntertech\MailCraft\MailCraft;
$mailcraft = new MailCraft(getenv('MAILCRAFT_API_KEY'));
$res = $mailcraft->send([
'from' => 'ร้านตัวอย่าง <no-reply@mail.example.com>',
'to' => 'customer@example.org',
'subject' => 'ยืนยันคำสั่งซื้อ #10042',
'htmlbody' => '<h1>ขอบคุณที่สั่งซื้อ</h1><p>เรากำลังเตรียมจัดส่งสินค้าให้คุณ</p>',
'textbody' => 'ขอบคุณที่สั่งซื้อ เรากำลังเตรียมจัดส่งสินค้าให้คุณ',
], ['idempotency_key' => 'order-10042-receipt']);
echo $res['message_id'];
ชื่อฟิลด์ตรงกับ REST API ที่อยู่อีเมลเขียนเป็น 'ชื่อ <a@b.com>' หรือ ['address' => ..., 'name' => ...] ก็ได้ ผลลัพธ์ที่ได้กลับมาเป็น associative array
แนบไฟล์
เลือกได้สามแบบ ตามที่สะดวก
'attachments' => [
['path' => '/var/invoices/INV-001.pdf', 'mime_type' => 'application/pdf'], // ให้ SDK อ่านไฟล์เอง
['name' => 'report.csv', 'data' => $csv], // ข้อมูลดิบ SDK เข้ารหัส base64 ให้
['name' => 'logo.png', 'content' => $base64], // เข้ารหัส base64 มาแล้ว
],
เทมเพลต ตั้งเวลาส่ง ส่งเป็นชุด และยกเลิก
$r = $mailcraft->send([
'from' => 'hello@mail.example.com',
'to' => ['address' => 'somchai@example.org', 'name' => 'สมชาย'],
'template_alias' => 'welcome',
'template_data' => ['name' => 'สมชาย'],
'send_at' => new DateTimeImmutable('tomorrow 09:00', new DateTimeZone('Asia/Bangkok')),
'list_unsubscribe' => true,
]);
$mailcraft->cancel($r['message_id']);
$mailcraft->sendBatch([$message1, $message2]);
$status = $mailcraft->get($r['message_id']);
จัดการข้อผิดพลาด
ทุก exception สืบทอดจาก MailCraftException มี property แบบ readonly คือ status, errorCode, field, retryAfter, body
use CraftIntertech\MailCraft\Exception\InsufficientCreditsException;
use CraftIntertech\MailCraft\Exception\MailCraftException;
try {
$mailcraft->send($message);
} catch (InsufficientCreditsException $e) {
error_log("เครดิตเหลือ {$e->getBalance()} แต่ต้องใช้ {$e->getRequired()}");
} catch (MailCraftException $e) {
error_log("{$e->status} {$e->errorCode}: {$e->getMessage()}");
}
ใช้ $e->errorCode เพื่อดูรหัสข้อผิดพลาดแบบข้อความ เพราะ getCode() ของ PHP เป็นตัวเลขเสมอ
คลาสย่อยที่มีให้: ValidationException (400/422), AuthenticationException (401), InsufficientCreditsException (402), PermissionDeniedException (403), NotFoundException (404), ConflictException (409), PayloadTooLargeException (413), RateLimitException (429), ServerException (5xx) และ ConnectionException (เครือข่ายขัดข้องหรือหมดเวลา)
ลองส่งซ้ำอัตโนมัติ
ทำงานแบบเดียวกับ SDK ภาษาอื่น: ลองใหม่สูงสุด 2 ครั้งเมื่อเจอ 429 / 5xx / เครือข่ายขัดข้อง รอตาม Retry-After และแนบ Idempotency-Key ให้อัตโนมัติจึงไม่ส่งซ้ำ
$mailcraft = new MailCraft($apiKey, [
'timeout' => 30, // วินาที
'max_retries' => 2,
'max_retry_delay' => 60,
]);
ตรวจลายเซ็น Webhook
use CraftIntertech\MailCraft\Webhook;
$event = Webhook::parseEvent(
(string) file_get_contents('php://input'),
$_SERVER['HTTP_MAILCRAFT_SIGNATURE'] ?? null,
getenv('MAILCRAFT_WEBHOOK_SECRET'),
);
ดูรายละเอียดและตัวอย่าง Laravel ได้ที่ ตรวจสอบลายเซ็น Webhook