Using external HTML templates for scheduled Mailgun emails in Laravel

I’m working on a Laravel project where I need to send scheduled emails using Mailgun’s API. I’ve got the timing part working fine, but I’m having trouble with the HTML content.

Right now my code looks like this:

$mailgunClient->messages()->send($domainName, [
    'from' => 'Support <[email protected]>',
    'to' => '[email protected]',
    'subject' => __('messages.welcome_subject'),
    'html' => '<div>Simple HTML content</div>',
    'o:deliverytime' => Carbon::now()->addHours(3)->toRfc2822String()
]);

The issue is that for complex emails with lots of HTML (like 100+ lines), putting everything directly in the ‘html’ field makes the code messy and hard to maintain. I want to use a separate template file instead.

Is there a way to reference an external Blade template file for the HTML content? Something like:

$mailgunClient->messages()->send($domainName, [
    'from' => 'Support <[email protected]>',
    'to' => '[email protected]', 
    'subject' => __('messages.welcome_subject'),
    'html' => 'emails.welcome-template.blade.php',
    'o:deliverytime' => Carbon::now()->addHours(3)->toRfc2822String()
]);

How can I properly include external HTML templates when sending delayed emails through Mailgun?

Honestly, just use View::make() if you want another approach. Works great for Mailgun and it’s simpler:

$html = View::make('emails.welcome', compact('user'))->render();

Just make sure your template’s in resources/views/emails/. I’ve been using this for months without any issues.

You need to render the Blade template into HTML first before passing it to Mailgun. Laravel’s view system handles this perfectly. Don’t reference the template file directly - use Laravel’s view() helper to compile it:

$htmlContent = view('emails.welcome-template', ['user' => $user, 'data' => $someData])->render();

$mailgunClient->messages()->send($domainName, [
    'from' => 'Support <[email protected]>',
    'to' => '[email protected]',
    'subject' => __('messages.welcome_subject'),
    'html' => $htmlContent,
    'o:deliverytime' => Carbon::now()->addHours(3)->toRfc2822String()
]);

The render() method compiles your Blade template into plain HTML that Mailgun can process. Pass variables to the template through view()'s second parameter. This keeps your code clean and lets you use all Blade features like loops, conditionals, and partials in your email templates.

I’ve had good luck creating a dedicated service class for handling Mailgun template rendering. Way better organization and you can reuse it everywhere. php class MailgunTemplateService { public function renderTemplate($templateName, $data = []) { return view($templateName, $data)->render(); } } Then in your scheduling code: php $templateService = new MailgunTemplateService(); $htmlContent = $templateService->renderTemplate('emails.welcome-template', [ 'userName' => $user->name, 'activationLink' => $activationUrl ]); $mailgunClient->messages()->send($domainName, [ 'from' => 'Support <[email protected]>', 'to' => '[email protected]', 'subject' => __('messages.welcome_subject'), 'html' => $htmlContent, 'o:deliverytime' => Carbon::now()->addHours(3)->toRfc2822String() ]); This saves tons of time with multiple email templates and makes debugging way easier. You can throw in error handling and template caching later if you need it.