Skip to content

Instantly share code, notes, and snippets.

@susanBuck
Last active February 1, 2024 15:48
Show Gist options
  • Save susanBuck/1c0280683dafe7d5fadbdc76ef3ffde6 to your computer and use it in GitHub Desktop.
Save susanBuck/1c0280683dafe7d5fadbdc76ef3ffde6 to your computer and use it in GitHub Desktop.
Send emails in Laravel without a Mailable class
<?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 = 'test@gmail.com';
$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