How to send beautiful HTML emails with Laravel using Beauty mail

Avinash Nethala
justlaravel
Published in
4 min readAug 14, 2018

Hello everyone, welcome back to just laravel. Here am going to show you how you can send clean and beautiful HTML emails in your laravel applications using this Beauty mail package by SnowFire. In one of my previous post I showed you how to send emails using SendGrid mail service provider, here I extended to send a clean and elegant HTML emails rather than plain text emails.

HTML emails using Beautymail in laravel — justlaravel.com

Setting up Beautymail

So first pull this package into our application.

composer require snowfire/beautymail dev-master

After composer finishes its installation, you need to add it to the service providers list in \config\app.php

'providers' => [
.....,
.....,
Snowfire\Beautymail\BeautymailServiceProvider::class,
];

Beautymail allows us to set some configurations for sending emails, where you can set logo to be sent in the email, its dimensions, and any social links to be included in you mail and all such settings.

You can change you settings in \config\beautymail.php.

<?phpreturn ['colors' => ['highlight' => '#004ca3',
'button' => '#004cad',
],'view' => [
'senderName' => null,
'reminder' => null,
'unsubscribe' => null,
'address' => null,
'logo' => [
'path' => '%PUBLIC%/img/logo.png',
'width' => '100',
'height' => '100',
],
'twitter' => 'justLaravel',
'facebook' => 'justLaravel',
],
];

View for entering email

I have made a simple app to show how this works, I place a input box, where one can enter an email address. Then that email address will be validate an will send a mail to that email address with our html email template we designed and customized.

So our view will look like,

HTML emails using Beautymail in laravel — justlaravel.com

It is basically a form with a single input for email adddress and a submit button for sending an email.

<form action="{{ URL::to('/sendMail') }}" method="POST">
{{ csrf_field() }}
<div>Email: <input class="input_email" type="email" name="email" required></div>
<button class="sendmailBtn" type="submit">Send Email</button>
</form>

So when the submit button is pressed it goes to the route sendmail where you can send an email, but before that I will design the view for the email to be sent.

Designing and customizing your email view

Beautymail provides us with few email templates to use, I use of them here and add some custom content to the view it provided. To see all the available templates, you can visit their GitHub repo: https://github.com/Snowfire/Beautymail.

I used ‘ark’ template, here is the example code they provided,

@extends('beautymail::templates.ark')@section('content')@include('beautymail::templates.ark.heading', [
'heading' => 'Hello World!',
'level' => 'h1'
])
@include('beautymail::templates.ark.contentStart')<h4 class="secondary"><strong>Hello World</strong></h4>
<p>This is a test</p>
@include('beautymail::templates.ark.contentEnd')@include('beautymail::templates.ark.heading', [
'heading' => 'Another headline',
'level' => 'h2'
])
@include('beautymail::templates.ark.contentStart')<h4 class="secondary"><strong>Hello World again</strong></h4>
<p>This is another test</p>
@include('beautymail::templates.ark.contentEnd')@stop

Here I removed the default hello world content and added some of post links and images and some text. You can add any content you want in between @include('beautymail::templates.ark.contentStart') and @include('beautymail::templates.ark.contentEnd')

Sending the mail

Firstly, to sen a mail you need to do some settings like setting up mailhost, username password and all. I used sengrid to send mail here. I also have a post covering how to get registered at send grid and send mails using it. Please look at that here.

Recommended: How to use SendGrid email service provider to send emails using laravel

In the form I set the action to \sendMail, so I made a route for it,

Route::post('/sendMail', 'IndexController@sendMail');

In IndexController, I had a new function sendMail

public function sendMail(Request $request){
$this->validate($request, [
'email' => 'bail|required|email',
]);

$beautymail = app()->make(\Snowfire\Beautymail\Beautymail::class);
$beautymail->send('emails.welcome', [], function($message)
{
$email = Input::get('email');
$message
->from('donotreply@justlaravel.com')
->to($email, 'Howdy buddy!')
->subject('Test Mail!');
});
return Redirect::back();
//return view('welcome');
}

In the above code, the following 2 lines sends a mail,

$beautymail = app()->make(\Snowfire\Beautymail\Beautymail::class);
$beautymail->send('emails.welcome', [], function($message)
{
...
...
}

In the function message, you can add from address to address and subject for the mail.

After sending mail you can return to any success view or anything you wish, Here for the example sake, am just redirecting back.

I also added some validation for the email input, if you look at the initial lines of the function, I check the email for required and for a valid email case.

You can show the errors in the view, where ever you want by placing the following code,

@if ($errors->any())
@foreach ($errors->all() as $error)
<div style="color:red; font-size:18px !important;"> {{ $error }}</div>
@endforeach
@endif

The sent mail will look like the following,

HTML emails using Beautymail in laravel — justlaravel.com

Also my custom content I inserted will look like,

HTML emails using Beautymail in laravel — justlaravel.com

Also facebook and twitter icons in the footer,

HTML emails using Beautymail in laravel — justlaravel.com

That’s it for the post. Now you successfully know how to send beautiful emails from your laravel applications.

--

--