Skip to content

Latest commit

 

History

History
133 lines (97 loc) · 3.23 KB

README.md

File metadata and controls

133 lines (97 loc) · 3.23 KB

Laravel Transactional Mails

Latest Version on Packagist Build Status Total Downloads

Send your mails after database transaction is committed.

This package prevents for e-mails being sent within a transaction when the transaction fails. It will buffer the emails (or queued emails) and sends (or queues) them after the transaction is committed. Especially sending emails in the background within a transaction and the job picks up the email before the transaction has committed the job might retrieve invalid data.

Installation

You can install the package via composer:

composer require mvanduijker/laravel-transactional-mails

Usage

You only have to extend your mailable with Duijker\LaravelTransactionalMails\TransactionalMailable instead of Illuminate\Mail\Mailable.

<?php

namespace App\Mail;

use App\Order;
use Duijker\LaravelTransactionalMails\TransactionalMailable;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;

class OrderShipped extends TransactionalMailable
{
    use Queueable, SerializesModels;

    /**
     * The order instance.
     *
     * @var Order
     */
    protected $order;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct(Order $order)
    {
        $this->order = $order;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('emails.orders.shipped')
                    ->with([
                        'orderName' => $this->order->name,
                        'orderPrice' => $this->order->price,
                    ]);
    }
}
<?php

namespace App\Http\Controllers;

use App\Order;
use App\Mail\OrderShipped;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Http\Controllers\Controller;

class OrderController extends Controller
{
    /**
     * Ship the given order.
     *
     * @param  Request  $request
     * @param  int  $orderId
     * @return Response
     */
    public function ship(Request $request, $orderId)
    {
        $order = Order::findOrFail($orderId);
        
        DB::transaction(function () use ($order, $request) {
            $order->ship();
            Mail::to($request->user())->send(new OrderShipped($order));
            
            throw new \RuntimeException('Mail won\'t be sent');
        });
        
    }
}

Testing

composer test

Changelog

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

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.