Laravel send email without Mailable
Introduction
Sometimes I forget how to send email in laravel without having to create a Mailable
, and every time I get that problem, I always do a search on the internet, it’s quite often. finally after I created this blog and one of its functions is to document problems when I work. so I made this article because maybe you guys also have the same problem as me.
How to do that ?
Here’s how to send email in laravel without creating ‘Mailable
‘ first.
1. Send Raw Message (Plain Text)
<?php
$email = '[email protected]';
$subject = 'This is the subject of the email;
Mail::raw('Hello, welcome to Laravel!', function ($message) use($email, $subject) {
$message->to($email)->subject($subject);
});
Refferance : https://github.com/illuminate/mail/blob/v9.24.0/Mailer.php#L205
2. Send Plain Text Message with variable in body
<?php
$email = '[email protected]';
$subject = 'This is the subject of the email';
Mail::plain('Hello {{ $name }}, welcome to Laravel!', ['name' => 'Dani'] function ($message) use($email, $subject) {
$message->to($email)->subject($subject);
});
Refferance : https://github.com/illuminate/mail/blob/v9.24.0/Mailer.php#L218
3. Send Message with Blade Template
<?php
//Send email with data and template from 'resource/views'
$email = '[email protected]';
$subject = 'This is the subject of the email';
$data = [
"name" => "Dani"
];
Mail::send('template.email', $data, function ($message) use($email, $subject) {
$message->to($email)->subject($subject);
});
Refference : https://github.com/illuminate/mail/blob/v9.24.0/Mailer.php#L250
Conclution
Those are some ways to send email in Laravel without having to create a Mailable, you can use it directly in the controller or in Tinker.
To get more knowledge, you can learn it directly from the source code on github or the laravel documentation