
Mail sending process should be clean and simple as much as possible. Laravel Provide a elegant way to send email with SwiftMailer library. Here we will give two example where we can use our custom layout and Laravel Email Layout. Let’s start
Send a Mail With Laravel
Before we start process we need to set some configuration that Laravel will use to send email so you can check your .env file where you will see some variable that have Identical name MAIL_ prefix. For Testing Purpose we will use mailtrap.io credential while development.
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=<mailtrap.io username>
MAIL_PASSWORD=<mailtrap.io password>
MAIL_ENCRYPTION=tls
MAIL_FROM_NAME="${APP_NAME}"
just change <mailtrap.io username> or <mailtrap.io password> with your own username or password that you get from mailtrap.io after signUp.
After setup credential to .env just run below command to make mail File from mailable.
php artisan make:mail SendNewMail --markdown=emails.new_mail
This command will do two things.
- Create a Class File in App\Mail directory (Also make Directory if no exist before)
- Create View file that you added after –markdown= (before . text will convert into folder)
Now let check out SendNewMail.php from App\Mail folder.
App\Mail\SendNewMail.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class SendNewMail extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->markdown('emails.new_mail');
}
}
This is simple class that responsible to clean code.
Basic to Advance Routing in Laravel
now take a look to new_mail.blade.php
that available resources/views/emails
folder.
resources/views/emails/new_mail.blade.php
@component('mail::message')
# Laravel Test
This is New Email
@component('mail::panel')
This is the panel content.
@endcomponent
@component('mail::table')
| Laravel | Table | Example |
| ------------- |:-------------:| --------:|
| Col 2 is | Centered | $10 |
| Col 3 is | Right-Aligned | $20 |
@endcomponent
@component('mail::button', ['url' => ''])
Button Text
@endcomponent
Thanks,<br>
{{ config('app.name') }}
@endcomponent
This is simple markdown mail template that you can edit as per your requirement.
now lets check how we send mail.
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
\Illuminate\Support\Facades\Mail::to('[email protected]')
->send(new \App\Mail\SendNewMail());
\Illuminate\Support\Facades\Mail::to('[email protected]')
->send(new \App\Mail\SendNewMail());
});
now just visit root url of your app and check your mailtrap demo inbox.
Conclusion
You checked that sending mail is very easy then we think. If you ever stuck to sending email just comment below and i will answer your comment with solution. Till that Keep Coding Stay Happy