Last active
February 1, 2024 15:48
-
-
Save susanBuck/1c0280683dafe7d5fadbdc76ef3ffde6 to your computer and use it in GitHub Desktop.
Send emails in Laravel without a Mailable class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
How to send emails in Laravel without using a Mailable class. Includes raw plain text emails, HTML emails, and emails generated from Blade View files. | |
⭐ Video how-to: https://youtu.be/FFFqbvNGptc ⭐ | |
Full reference: https://codewithsusan.com/notes/laravel-email-without-mailable-class | |
*/ | |
namespace App\Http\Controllers; | |
use Illuminate\Support\Facades\Mail; | |
class EmailDemoController extends Controller | |
{ | |
public function index() | |
{ | |
$email = '[email protected]'; | |
$subject = 'This is the subject'; | |
$body = '<h1>This is the body of the email...</h1>'; | |
# EXAMPLE 1) Send the plain text email | |
Mail::raw($body, function ($message) use ($email, $subject) { | |
$message->to($email) | |
->subject($subject . ' plain text'); | |
}); | |
# EXAMPLE 2) Send the HTML email | |
Mail::html($body, function ($message) use ($email, $subject) { | |
$message->to($email) | |
->subject($subject . ' html'); | |
}); | |
# EXAMPLE 3) Send the HTML email using a View | |
$body = view('demo')->render(); | |
Mail::html($body, function ($message) use ($email, $subject) { | |
$message->to($email) | |
->subject($subject . ' html from a View'); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment