lukasss93/laravel-extra-mailable

Extra tools for Laravel Mailables

v1.0 2021-05-25 21:35 UTC

This package is auto-updated.

Last update: 2022-07-30 11:52:40 UTC


README

68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f4c61726176656c25323045787472612532304d61696c61626c652e706e673f7468656d653d6461726b267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d6c756b6173737339332532466c61726176656c2d65787472612d6d61696c61626c65267061747465726e3d62616d626f6f267374796c653d7374796c655f31266465736372697074696f6e3d45787472612b746f6f6c732b666f722b4c61726176656c2b4d61696c61626c6573266d643d312673686f7757617465726d61726b3d3026666f6e7453697a653d313235707826696d616765733d6d61696c

Laravel Extra Mailable

Version Downloads PHP Laravel License Build Coverage

Extra tools for Laravel Mailables

🚀 Installation

You can install the package using composer

composer require lukasss93/laravel-extra-mailable

👓 Usage

  1. Add the ExtraMailable Trait in your Mailable class:
<?php

namespace App\Mail;

use Illuminate\Mail\Mailable;
use Lukasss93\ExtraMailable\ExtraMailable;

class MyMail extends Mailable
{
    use ExtraMailable;

    protected int $value;

    public function __construct(int $value = 0)
    {
        $this->value = $value;
    }

    public function build() 
    {
        return $this->markdown('emails.myview', ['myvalue' => $this->value]);
    }
}
  1. How to use the trait:
<?php

use App\Mail\MyMail;

// send mail to recipient (string)
MyMail::create()->sendTo('foo@bar.org');

// send mail to recipients (string with semicolon separator)
MyMail::create()->sendTo('foo@bar.org;bar@foo.org');

// send mail to recipients (array)
MyMail::create()->sendTo(['foo@bar.org','bar@foo.org']);

// send mail to recipients (User)
MyMail::create()->sendTo(User::first());

// send mail to recipients (User collection)
MyMail::create()->sendTo(User::all());

// you can pass parameters in the create method
MyMail::create(69)->sendTo('foo@bar.org');

// send mail to recipients when condition is true
MyMail::create()->sendToWhen(true, 'foo@bar.org');

// execute custom code when there is no recipients
MyMail::create()
    ->onEmptyRecipients(fn() => print('No emails sent! No recipient found.'))
    ->sendTo([]);
    
// execute custom code before sending emails
MyMail::create()
    ->onBeforeSendingMails(fn() => print('This message will be printed before sending emails'))
    ->sendTo('foo@bar.org');

// execute custom code after sending emails
MyMail::create()
    ->onAfterSendingMails(fn() => print('This message will be printed after sending emails'))
    ->sendTo('foo@bar.org');

⚗️ Testing

composer test

📃 Changelog

Please see the CHANGELOG.md for more information on what has changed recently.

🏅 Credits

📖 License

Please see the LICENSE.md file for more information.