mega-tj/mailer

A Laravel 11 package for sending emails with flexible email templates (Blade & Markdown) using PHPMailer.

v1.1.2 2025-10-05 19:11 UTC

This package is auto-updated.

Last update: 2025-10-05 19:22:31 UTC


README

Mega Mailer Version: 1.1.2 License: MIT

Mega Mailer is a simple and elegant mailer package for PHP, built on PHPMailer, and designed to integrate seamlessly with the Laravel Mailable pattern. It facilitates sending emails through SMTP with ease, offering robust support for recipients, attachments, and Blade views.

Features SMTP Support: Easily send emails using SMTP configured via Laravel's config/mailer.php.

Full Recipient Control: Dedicated methods for TO, CC, and BCC.

Attachment Support: Attach files directly via the fluent interface or through Mailable objects.

Mailable Integration: Full compatibility with Laravel's standard Mailable, Envelope, and Attachment classes.

Blade View Rendering: Send HTML emails using Laravel Blade views.

Simple API: Set recipients, subjects, and email bodies with a straightforward, fluent interface.

Installation You can install Mega Mailer via Composer. Run the following command in your project directory:

Bash

composer require mega-tj/mailer:dev-main --dev Laravel Integration

  1. Register the Service Provider Ensure your package is installed via Composer. If you are using Laravel 5.5+ and have configured your composer.json for auto-discovery (as shown below), this step is automatic.

JSON

// composer.json "extra": { "laravel": { "providers": [ "Mega\App\Providers\MailerServiceProvider" ] # Mega Mailer

Version: 1.0.0
License: MIT

Mega Mailer is a lightweight PHP mailer package that uses PHPMailer and integrates with Laravel's Mailable pattern. It provides a small, opinionated API for sending SMTP emails, with support for recipients (TO/CC/BCC), attachments, and rendering Blade views.

## Features

- SMTP support via PHPMailer and settings from `config/mailer.php`.
- Fluent API for quick messages and first-class support for Laravel `Mailable` objects.
- TO, CC, and BCC helpers.
- Attach local files or include attachments from a `Mailable`.
- Render Blade views for HTML emails.

## Installation

Require the package with Composer (stable or dev branch):

```bash
composer require mega-tj/mailer
# or for the current development branch
composer require mega-tj/mailer:dev-main --dev
```

## Laravel integration

The package registers the service provider automatically if you rely on Composer auto-discovery. The provider class is:

```
Mega\App\Providers\MailerServiceProvider
```

### Publish configuration

To publish the package configuration to your Laravel app (creates `config/mailer.php`):

```bash
php artisan vendor:publish --provider="Mega\App\Providers\MailerServiceProvider" --tag=config
```

The package reads SMTP settings using `config('mailer.*')`.

## Configuration

The shipped configuration (`src/config/mailer.php`) exposes these keys (and defaults):

- `host` (env: MAIL_HOST) — default: `localhost`
- `port` (env: MAIL_PORT) — default: `25`
- `username` (env: MAIL_USERNAME)
- `password` (env: MAIL_PASSWORD)
- `from_address` (env: MAIL_FROM_ADDRESS) — default: `no-reply@example.com`
- `from_name` (env: MAIL_FROM_NAME) — default: `Mega Mailer`
- `smtp_auth` (env: MAIL_SMTP_AUTH) — default: `false`
- `encryption` (env: MAIL_ENCRYPTION) — `tls`, `ssl`, or `false`
- `auto_tls` (env: MAIL_AUTO_TLS) — default: `false`
- `charset` (env: MAIL_CHARSET) — default: `UTF-8`
- `smtp_options` — array of `smtp_options` passed to PHPMailer (defaults allow self-signed certs)

Set the corresponding environment variables in your `.env` file for production.

## Usage

The primary API surface is the `Mega\App\Mailer` class (PSR-4 autoloaded from `src/`). The class supports a fluent API and can also accept a Laravel `Mailable` in `send()`.

### Option 1 — Send a Laravel Mailable (recommended)

When you pass a `Mailable` instance to `send()`, the Mailer pulls subject, from, recipients, and attachments from the `Mailable`'s `envelope()` and `attachments()` methods.

```php
use Mega\App\Mailer;
use App\Mail\TestMailable;

$mailer = new Mailer();
$response = $mailer->to('recipient@example.com')
                 ->send(new TestMailable());

// $response is a string message or an error message
```

Example `Mailable` (Laravel 10+ style using `Envelope`/`Content`/`Attachment` classes):

```php
namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
use Illuminate\Mail\Mailables\Address;
use Illuminate\Mail\Mailables\Attachment;

class TestMailable extends Mailable
{
    use Queueable, SerializesModels;

    public function envelope(): Envelope
    {
        return new Envelope(
            subject: 'Report',
            from: new Address('support@example.com', 'Support Team'),
            cc: [new Address('manager@example.com')],
            bcc: [new Address('archive@example.com')]
        );
    }

    public function content(): Content
    {
        return new Content(
            view: 'emails.send_report',
            with: ['user' => 'John Doe']
        );
    }

    public function attachments(): array
    {
        return [
            Attachment::fromPath(storage_path('app/report.pdf'))->as('Report.pdf')
        ];
    }
}
```

### Option 2 — Fluent API (simple emails)

For quick one-off messages you can use the fluent methods. You must set the `from()` address (or configure `from_address` in `config/mailer.php`).

```php
use Mega\App\Mailer;

$mailer = new Mailer();
$mailer->from('noreply@mycompany.com', 'System')
       ->to('primary.user@example.com', 'Primary User')
       ->cc('team.lead@example.com')
       ->bcc(['logs@example.com', 'security@example.com'])
       ->subject('Deployment Complete')
       ->message('The deployment finished successfully at '.now())
       ->attach(storage_path('app/logs.zip'))
       ->send();
```

## Behaviour and notes

- The Mailer uses PHPMailer under the hood and configures it from `config('mailer.*')`.
- When rendering a Blade view the Mailer sets the message as HTML. If the view is missing it falls back to a plaintext message.
- Attachments added through a `Mailable` must be instances of `Illuminate\Mail\Mailables\Attachment` (the Mailer checks for this type).

## Contributing

Contributions are welcome. Please open issues or PRs against the upstream repository: https://github.com/wisedev99/mega-mailer

## License

This project is licensed under the MIT License.