tosend/tosend-laravel
Composer 安装命令:
composer require tosend/tosend-laravel
包简介
Official Laravel SDK for the ToSend email API
README 文档
README
Official Laravel SDK for the ToSend email API.
Requirements
- PHP 8.1 or higher
- Laravel 10.x, 11.x, or 12.x
Installation
composer require tosend/tosend-laravel
Configuration
Add your API key to your .env file:
TOSEND_API_KEY=tsend_your_api_key
Optionally publish the config file:
php artisan vendor:publish --tag=tosend-config
Configuration Options
// config/tosend.php return [ 'api_key' => env('TOSEND_API_KEY'), 'api_url' => env('TOSEND_API_URL', 'https://api.tosend.com'), 'from' => [ 'address' => env('TOSEND_FROM_ADDRESS'), 'name' => env('TOSEND_FROM_NAME'), ], 'timeout' => env('TOSEND_TIMEOUT', 30), ];
Usage
Using the Facade
use ToSend\Laravel\Facades\ToSend; $response = ToSend::send([ 'from' => ['email' => 'hello@yourdomain.com', 'name' => 'Your App'], 'to' => [['email' => 'user@example.com']], 'subject' => 'Welcome!', 'html' => '<h1>Hello World</h1>', ]); echo $response->messageId;
Using Dependency Injection
use ToSend\Laravel\Contracts\ToSendClient; class EmailController extends Controller { public function send(ToSendClient $tosend) { $response = $tosend->send([ 'from' => ['email' => 'hello@yourdomain.com'], 'to' => [['email' => 'user@example.com']], 'subject' => 'Hello!', 'html' => '<p>Welcome to our app!</p>', ]); return $response->messageId; } }
Using the Email Builder
use ToSend\Laravel\Facades\ToSend; use ToSend\Laravel\Data\Email; use ToSend\Laravel\Data\Attachment; $email = Email::make( from: ['email' => 'hello@yourdomain.com', 'name' => 'Your App'], subject: 'Your Invoice' ) ->to(['email' => 'user@example.com', 'name' => 'John Doe']) ->to('another@example.com') ->cc('manager@example.com') ->bcc(['email' => 'archive@example.com']) ->html('<h1>Invoice Attached</h1>') ->text('Invoice Attached') ->attach(Attachment::fromPath('/path/to/invoice.pdf')); $response = ToSend::send($email);
Batch Sending
use ToSend\Laravel\Facades\ToSend; $response = ToSend::batch([ [ 'from' => ['email' => 'hello@yourdomain.com'], 'to' => [['email' => 'user1@example.com']], 'subject' => 'Hello User 1', 'html' => '<p>Welcome!</p>', ], [ 'from' => ['email' => 'hello@yourdomain.com'], 'to' => [['email' => 'user2@example.com']], 'subject' => 'Hello User 2', 'html' => '<p>Welcome!</p>', ], ]); // Check results echo "Sent: " . $response->successCount(); echo "Failed: " . $response->failedCount(); foreach ($response->results as $result) { if ($result->isSuccess()) { echo "Sent: " . $result->messageId; } else { echo "Failed: " . $result->message; } }
Account Information
use ToSend\Laravel\Facades\ToSend; $info = ToSend::getAccountInfo(); echo $info->title; echo $info->emailsUsageThisMonth; echo $info->emailsSentLast24Hours; foreach ($info->domains as $domain) { echo $domain->domainName . ': ' . $domain->verificationStatus; } // Get only verified domains $verified = $info->verifiedDomains();
Laravel Mail Integration
Use ToSend as your Laravel mail driver:
Configure Mail Driver
MAIL_MAILER=tosend TOSEND_API_KEY=tsend_your_api_key TOSEND_FROM_ADDRESS=hello@yourdomain.com TOSEND_FROM_NAME="Your App"
// config/mail.php 'mailers' => [ 'tosend' => [ 'transport' => 'tosend', ], ],
Send with Laravel Mail
use Illuminate\Support\Facades\Mail; // Using a Mailable Mail::to('user@example.com')->send(new WelcomeEmail()); // Using the mail facade directly Mail::mailer('tosend') ->to('user@example.com') ->send(new WelcomeEmail());
Create a Mailable
namespace App\Mail; use Illuminate\Mail\Mailable; use Illuminate\Mail\Mailables\Content; use Illuminate\Mail\Mailables\Envelope; use Illuminate\Mail\Mailables\Attachment; class WelcomeEmail extends Mailable { public function envelope(): Envelope { return new Envelope( from: new \Illuminate\Mail\Mailables\Address('hello@yourdomain.com', 'Your App'), subject: 'Welcome to Our App', ); } public function content(): Content { return new Content( view: 'emails.welcome', ); } public function attachments(): array { return [ Attachment::fromPath('/path/to/file.pdf'), ]; } }
Attachments
From File Path
use ToSend\Laravel\Data\Attachment; $attachment = Attachment::fromPath('/path/to/document.pdf'); // With custom name and type $attachment = Attachment::fromPath( path: '/path/to/document.pdf', name: 'custom-name.pdf', type: 'application/pdf' );
From Content
$attachment = Attachment::fromContent( content: $pdfContent, name: 'report.pdf', type: 'application/pdf' );
From Base64
$attachment = Attachment::fromBase64( base64Content: $base64String, name: 'image.png', type: 'image/png' );
Error Handling
use ToSend\Laravel\Facades\ToSend; use ToSend\Laravel\Exceptions\ToSendException; try { $response = ToSend::send([ 'from' => ['email' => 'hello@yourdomain.com'], 'to' => [['email' => 'user@example.com']], 'subject' => 'Hello', 'html' => '<p>Hello</p>', ]); } catch (ToSendException $e) { // Get error message echo $e->getMessage(); // Get HTTP status code echo $e->getCode(); // Get validation errors $errors = $e->getErrors(); // Check error type if ($e->isValidationError()) { // Handle validation error (422) } if ($e->isAuthenticationError()) { // Handle auth error (401/403) } if ($e->isRateLimitError()) { // Handle rate limit (429) } }
Testing
For testing, you can mock the ToSend client:
use ToSend\Laravel\Contracts\ToSendClient; use ToSend\Laravel\Data\EmailResponse; public function test_sends_welcome_email() { $mock = $this->mock(ToSendClient::class); $mock->shouldReceive('send') ->once() ->andReturn(new EmailResponse(messageId: 'test-message-id')); // Your test code... }
Or use a custom base URL for testing:
# .env.testing TOSEND_API_URL=http://localhost:8080
License
MIT
tosend/tosend-laravel 适用场景与选型建议
tosend/tosend-laravel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 835 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「mail」 「api」 「email」 「laravel」 「transactional」 「tosend」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tosend/tosend-laravel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tosend/tosend-laravel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tosend/tosend-laravel 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Courier offers a convenient and painless solution for creating emails tailored for your Kirby website.
A PSR-7 compatible library for making CRUD API endpoints
Mail libraries used by Zimbra Api
Extensible library for building notifications and sending them via different delivery channels.
Email+ extends Kirby's email capabilities by adding support for multiple email services using the same Kirby email API.
Laravel contact us form package to send email and save to database
统计信息
- 总下载量: 835
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-24