lettr/lettr-php 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

lettr/lettr-php

最新稳定版本:v0.1.0-alpha.1

Composer 安装命令:

composer require lettr/lettr-php

包简介

Lettr PHP SDK - Send emails via Lettr API

README 文档

README

A PHP client library for the Lettr email API.

Requirements

  • PHP 8.4 or higher
  • Guzzle HTTP client

Installation

composer require lettr/lettr-php

Usage

Basic Usage

use Lettr\Lettr;
use Lettr\Dto\SendEmailData;

// Create client with your API key
$lettr = Lettr::client('your-api-key');

// Send an email using DTO
$email = new SendEmailData(
    from: 'sender@example.com',
    to: ['recipient@example.com'],
    subject: 'Hello from Lettr',
    text: 'Plain text body',
    html: '<p>HTML body</p>',
);

$response = $lettr->emails()->send($email);

echo $response->id; // The email ID

Using Array Syntax

You can also create the DTO from an array:

use Lettr\Lettr;
use Lettr\Dto\SendEmailData;

$lettr = Lettr::client('your-api-key');

$email = SendEmailData::from([
    'from' => 'sender@example.com',
    'to' => ['recipient@example.com'],
    'subject' => 'Hello from Lettr',
    'text' => 'Plain text body',
    'html' => '<p>HTML body</p>',
]);

$response = $lettr->emails()->send($email);

Property Access

You can access the emails service via method or property:

// Via method
$response = $lettr->emails()->send($email);

// Via property
$response = $lettr->emails->send($email);

Custom Base URL

If you need to use a different API endpoint:

$lettr = Lettr::client('your-api-key', 'https://custom-api.example.com');

API Reference

SendEmailData

Parameter Type Required Description
from string Yes Sender email address
to array Yes Array of recipient email addresses
subject string Yes Email subject
text string No Plain text body
html string No HTML body

EmailResponse

Property Type Description
id string Unique identifier for the sent email

Error Handling

The client throws exceptions for API errors:

use Lettr\Lettr;
use Lettr\Exceptions\ApiException;
use Lettr\Exceptions\TransporterException;

try {
    $response = $lettr->emails()->send($email);
} catch (ApiException $e) {
    // API returned an error response
    echo $e->getMessage();
} catch (TransporterException $e) {
    // Network or transport error
    echo $e->getMessage();
}

Development

Install Dependencies

composer install

Code Style

This project uses Laravel Pint for code style:

composer lint

Static Analysis

This project uses PHPStan at level 8:

composer analyse

Testing

This project uses Pest for testing:

composer test

License

MIT License. See LICENSE for more information.

统计信息

  • 总下载量: 13
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-11