Carts & Orders
Email Notifications
When you install Cargo, it will automatically create a mailable for you, resulting in a class in app/Mail
and a view in resources/views/emails
.
Cargo will also configure an event listener in your AppServiceProvider
, trigger the email to send whenever Cargo's OrderPaymentReceived
event is dispatched.
// app/Providers/AppServiceProvider.php
use App\Mail\OrderConfirmation;
use DuncanMcClean\Cargo\Events\OrderPaymentReceived;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Mail;
Event::listen(OrderPaymentReceived::class, function ($event) {
Mail::to($event->order->customer())
->locale($event->order->site()->shortLocale())
->send(new OrderConfirmation($event->order));
});
To change the content of the email, all you need to do is edit the view or mailable class in your app.
Sending your own emails
If you want to send any other emails, you can create your own mailable, then configure an event listener to trigger it based on one of Cargo's events:
php artisan make:mail OrderOnTheWay
// app/Providers/AppServiceProvider.php
use App\Mail\OrderOnTheWay;
use DuncanMcClean\Cargo\Events\OrderShipped;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Mail;
Event::listen(OrderShipped::class, function ($event) {
Mail::to($event->order->customer())
->locale($event->order->site()->shortLocale())
->send(new OrderOnTheWay($event->order));
});
For more information on sending emails, please consult the Laravel documentation.
Previewing emails in the browser
You can preview Mailables by returning them from a route, like this:
// routes/web.php
Route::get('/order-confirmation', function () {
$order = Order::query()->orderByDesc('date')->first();
return new OrderConfirmation($order);
});
You may want to wrap the route in a if (! app()->isProduction())
conditional to ensure the email isn't accessible in production.