ronald2wing/laravel-mailtrap

Laravel mail driver for sending emails through Mailtrap.io Email Sending Service API

Installs: 48

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

pkg:composer/ronald2wing/laravel-mailtrap

v1.1.1 2025-12-08 20:59 UTC

This package is auto-updated.

Last update: 2025-12-08 21:04:03 UTC


README

Latest Version on Packagist Total Downloads PHP Version License

A Laravel mail driver for sending emails through the Mailtrap.io Email Sending Service API.

This package provides seamless integration between Laravel and Mailtrap's sending API, allowing you to send emails through Mailtrap while using Laravel's familiar mail API.

โœจ Features

  • ๐Ÿš€ Easy Integration: Simple setup with Laravel's mail configuration
  • ๐Ÿ“ง Full Feature Support: Supports all Mailtrap API features including categories, attachments, CC/BCC
  • ๐Ÿ”„ Laravel Compatibility: Works with Laravel 10.x, 11.x, and 12.x
  • ๐Ÿ’ป Modern Codebase: Clean, well-documented code with comprehensive tests
  • ๐Ÿ›ก๏ธ Error Handling: Robust error handling and debugging capabilities
  • ๐Ÿ“Š Analytics: Support for Mailtrap categories for email tracking
  • ๐ŸŒ International: Full UTF-8 support for international characters
  • ๐Ÿ”ง Customizable: Configurable API endpoints and HTTP client options

๐Ÿ“‹ Requirements

  • PHP 8.2 or higher
  • Laravel 10.x, 11.x, or 12.x
  • GuzzleHTTP 7.0 or higher

๐Ÿš€ Installation

Install the package via Composer:

composer require ronald2wing/laravel-mailtrap

The package will automatically register itself using Laravel's package auto-discovery.

โš™๏ธ Configuration

Environment Configuration

Add your Mailtrap API configuration to your .env file:

MAIL_MAILER=mailtrap
MAILTRAP_TOKEN=your_mailtrap_api_token_here

Services Configuration

Add your Mailtrap API configuration to config/services.php:

'mailtrap' => [
    'token' => env('MAILTRAP_TOKEN', ''), // Your Mailtrap API token
],

Mail Configuration

Configure Laravel to use the Mailtrap driver in config/mail.php:

'default' => env('MAIL_MAILER', 'mailtrap'),

'mailers' => [
    'mailtrap' => [
        'transport' => 'mailtrap',
    ],
    // ... other mailers
],

Advanced Configuration

For advanced use cases, you can customize the HTTP client configuration:

'mailtrap' => [
    'token' => env('MAILTRAP_TOKEN', ''),
    'guzzle' => [
        'timeout' => 30,
        'connect_timeout' => 10,
        'headers' => [
            'User-Agent' => 'Your-App-Name/1.0',
        ],
    ],
],

๐Ÿ“ Usage

Basic Email Sending

Send emails using Laravel's standard mail API:

use Illuminate\Support\Facades\Mail;

Mail::to('recipient@example.com')
    ->send(new WelcomeEmail());

Using Mailable Classes

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class WelcomeEmail extends Mailable
{
    use Queueable, SerializesModels;

    public function build()
    {
        return $this->subject('Welcome to Our Service')
                    ->view('emails.welcome')
                    ->text('emails.welcome_plain');
    }
}

Using Mailtrap Categories

You can add Mailtrap categories to track email analytics:

use Illuminate\Support\Facades\Mail;

// Using the send method
Mail::send('emails.welcome', $data, function ($message) {
    $message->to('user@example.com')
            ->subject('Welcome to Our Service')
            ->header('X-Mailtrap-Category', 'welcome-emails');
});

// Using a Mailable class
class WelcomeEmail extends Mailable
{
    public function build()
    {
        return $this->view('emails.welcome')
                    ->header('X-Mailtrap-Category', 'welcome-emails');
    }
}

Sending with Attachments

Mail::send('emails.invoice', $data, function ($message) {
    $message->to('customer@example.com')
            ->subject('Your Invoice')
            ->attach('/path/to/invoice.pdf');
});

// Multiple attachments
Mail::send('emails.newsletter', $data, function ($message) {
    $message->to('subscriber@example.com')
            ->subject('Monthly Newsletter')
            ->attach('/path/to/newsletter.pdf')
            ->attach('/path/to/special-offer.jpg');
});

Sending with CC and BCC

Mail::send('emails.announcement', $data, function ($message) {
    $message->to('primary@example.com')
            ->cc(['cc1@example.com', 'cc2@example.com'])
            ->bcc('bcc@example.com')
            ->subject('Important Announcement');
});

Custom Headers and Reply-To

Mail::send('emails.contact', $data, function ($message) {
    $message->to('support@example.com')
            ->replyTo('user@example.com', 'John Doe')
            ->header('X-Priority', '1')
            ->header('X-Custom-ID', '12345')
            ->subject('Support Request');
});

๐Ÿ”‘ API Token

To get your Mailtrap API token:

  1. Log in to your Mailtrap account
  2. Navigate to your sending domain settings
  3. Copy the API token from the integration section
  4. Add it to your .env file as MAILTRAP_TOKEN

๐Ÿงช Testing

The package includes comprehensive tests covering all features. Run them with:

./vendor/bin/phpunit

Test Features Covered

  • โœ… Email sending (text, HTML, multipart)
  • โœ… Recipients (to, cc, bcc)
  • โœ… Attachments (regular, inline)
  • โœ… Headers and metadata
  • โœ… Error handling
  • โœ… International character support
  • โœ… Custom API endpoints

๐ŸŽฏ Code Quality

This package maintains high code quality standards with the following tools:

Static Analysis

Run PHPStan for static analysis:

composer run analyse

Code Formatting

Format code with Laravel Pint:

composer run lint

Full Quality Check

Run both tests and code quality checks:

./vendor/bin/phpunit && composer run analyse && composer run lint

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the project
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Development Setup

git clone https://github.com/ronald2wing/laravel-mailtrap.git
cd laravel-mailtrap
composer install

๐Ÿ“„ License

This package is open-sourced software licensed under the MIT license.

๐Ÿ†˜ Support

If you encounter any issues or have questions:

  1. Check the documentation for common solutions
  2. Search existing issues
  3. Open a new issue with detailed information

๐Ÿ”— Links

Built with โค๏ธ for the Laravel community