Skip to content

Latest commit

 

History

History
70 lines (45 loc) · 2.38 KB

Mail.md

File metadata and controls

70 lines (45 loc) · 2.38 KB

Mail

⬆️ Go to top ⬅️ Previous (Auth) ➡️ Next (Artisan)

  1. Testing email into laravel.log

  2. Preview Mailables

  3. [Preview Mail without Mailables](#Preview Mail without Mailables)

  4. Default Email Subject in Laravel Notifications

  5. Send Notifications to Anyone

Testing email into laravel.log

If you want to test email contents in your app but unable or unwilling to set up something like Mailgun, use .env parameter MAIL_DRIVER=log and all the email will be saved into storage/logs/laravel.log file, instead of actually being sent.

Preview Mailables

If you use Mailables to send email, you can preview the result without sending, directly in your browser. Just return a Mailable as route result:

Route::get('/mailable', function () {
    $invoice = App\Invoice::find(1);
    return new App\Mail\InvoicePaid($invoice);
});

Preview Mail without Mailables

You can also preview your email without Mailables. For instance, when you are creating notification, you can specify the markdown that may be use for your mail notification.

use Illuminate\Notifications\Messages\MailMessage;
Route::get('/mailable', function () {
    $invoice = App\Invoice::find(1);
    return (new MailMessage)->markdown('emails.invoice-paid', compact('invoice'));
});

You may also use other methods provided by MailMessage object such as view and others.

Tip given by @raditzfarhan

Default Email Subject in Laravel Notifications

If you send Laravel Notification and don't specify subject in toMail(), default subject is your notification class name, CamelCased into Spaces.

So, if you have:

class UserRegistrationEmail extends Notification {
    //
}

Then you will receive an email with subject User Registration Email.

Send Notifications to Anyone

You can send Laravel Notifications not only to a certain user with $user->notify(), but also to anyone you want, via Notification::route(), with so-called "on-demand" notifications:

Notification::route('mail', '[email protected]')
        ->route('nexmo', '5555555555')
        ->route('slack', 'https://hooks.slack.com/services/...')
        ->notify(new InvoicePaid($invoice));