Laravel mail: pass string instead of view

Laravel mail: pass string instead of view

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Laravel mail: pass string instead of view

When building a registration flow or sending transactional emails, it is common to store your HTML templates in a database rather than committing them to the filesystem. This approach simplifies version control and allows non-developers to update copy without touching Git repositories. However, this creates a specific friction point when using Laravel's Mail facade: the standard Mail::send() method expects either a view name or a path to a Blade file, not a raw string of HTML content.

Attempting to pass a simple string directly to the constructor, as shown in the example below, will fail because the underlying Mailable class is looking for a view engine to render the content.

$content = "Hi, welcome user!";

// This will throw an exception: View does not exist
Mail::send($content, $data, function() {}); 

To resolve this without creating dummy files or relying on database lookups inside the mailer logic, you should utilize the Mailable class alongside the view() helper. The key is to separate the data retrieval from the email dispatching.

Using the view() Helper with Raw Data

The most direct solution involves calling the Laravel view() helper function. Unlike Mail::send(), which assumes you are rendering a Blade template, view() can accept an array of options where you specify that the content should be treated as raw HTML rather than a template file path.

You can retrieve your string from the database and pass it into the view() method by setting the second parameter to 'mail' (or any name) and ensuring the third parameter indicates you want to treat the first argument as data, not a view path. However, for passing raw strings specifically, the cleanest pattern is often to use the Mailable class directly with the with() method or by injecting the content into the message() callback.

Here is how you can dispatch an email where the body is stored in a variable:

use Illuminate\Support\Facades\Mail;
use App\Models\User; // Adjust namespace as needed

$emailContent = User::find(1)->email_template_column; // Fetch from DB

Mail::raw($emailContent, function ($message) use ($emailContent) {
    $message->subject('Welcome to our platform');
    $message->view('emails.welcome', [
        'content' => $emailContent
    ]); 
});

Wait, that still uses a view. Let's correct that for the specific requirement of passing a string without a view file entirely if the template is purely static HTML stored in DB.

The most robust approach when you strictly need to bypass Blade rendering (because your DB string might not be valid Blade) is to use Mail::raw(). This method accepts a plain text or HTML string as its first argument. If your database content is HTML, this is the semantic match for your needs.

use Illuminate\Support\Facades\Mail;

// Retrieve the template string from your database
$templateHtml = DB::table('email_templates')
    ->where('id', $templateId)
    ->value('content');

Mail::raw($templateHtml, function ($message) {
    $message->subject('Account Confirmation');
    // You can still attach data to the view if needed for dynamic parts 
    // but raw() treats the first arg as the body.
});

Handling Dynamic Data with Raw Content

A common pitfall when switching to Mail::raw() is that you lose access to Blade directives like {{ $user->name }} or @foreach. If your database string contains static HTML but you need to inject dynamic variables (like the user's name), Mail::raw() alone isn't sufficient because it doesn't render a view context.

In this scenario, you must construct a complete HTML string that includes placeholders or use the Mailable class with the with() method to pass data into a view that renders the string. However, if you want to avoid creating a Blade file for every template variation in your DB, consider using the mail() method on the Mailable object directly.

You can define a custom Mailable class:

// app/Mail/DatabaseTemplateMail.php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class DatabaseTemplateMail extends Mailable
{
    use Queueable, SerializesModels;

    public $subject;
    public $content;

    public function __construct($subject, $content)
    {
        $this->subject = $subject;
        this->content = $content;
    }

    public function build()
    {
        return $this->subject($this->subject)
            ->html($this->content); // Treats content as raw HTML
    }
}

Dispatching the Mailable

Once the class is defined, you can dispatch it from a controller or event listener, fetching the template string on the fly:

$templateContent = DB::table('templates')->first()->content;

Mail::to($user->email)->send(
    new DatabaseTemplateMail(
        'Welcome!', 
        $templateContent
    )
);

This pattern decouples your email logic from the filesystem. You retain the ability to store complex HTML structures in a database while leveraging Laravel's queueing and transport layers. It also ensures that if you need to update a welcome message, you do not need to deploy code changes; a simple database update triggers the change immediately for all subsequent emails.

Be mindful, however, that Mail::raw() or passing raw HTML disables Blade syntax processing. If your stored string relies on Blade directives, you must either store pre-rendered HTML (which defeats the purpose of dynamic data) or ensure your placeholders are compatible with how the Mailable handles data injection. For most confirmation emails where the structure is fixed but the content varies, this approach provides a clean separation between your application code and your email templates.

Our network