What are laravel mail markdown components ? And how is it used ?
Setting Up Mail Configuration
Implied the mail configuration in Laravel, define the following Gmail SMTP details such as username, password inside the .env file.
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=deneme@gmail.com
MAIL_PASSWORD= (third party app password <a href=”https://support.google.com/accounts/answer/185833?hl=en" target=”_blank”>click hear</a>)
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=deneme@gmail.com
MAIL_FROM_NAME=”${APP_NAME}”
If we are sending email from localhost using Gmail SMTP, then it requires you to turn on the “Allow less secure apps” on?
Go to following link.
https://www.google.com/settings/security/lesssecureapps
Next, You need to turn the option “Allow less secure apps” ON.
Open web.php file from /routes folder. Add following into that file.
//…
use Illuminate\Support\Facades\Mail;//…Route::get(‘send-mail’, function () {
$details = [
‘title’ => ‘Sample Title From Mail’,
‘body’ => ‘This is sample content we have added for this test mail’
];
Mail::to(‘send_to_email@gmail.com’)->send(new \App\Mail\MyTestMail($details));
dd(“Email is Sent, please check your inbox.”);
});//…
Open project in terminal and type this artisan command.
php artisan help make:mail php artisan make:mail MyTestMail --markdown=emails.sample-mail
By running the above command, two files are generated:
app/mail/MyTestMail.php
resources/views/emails/sample-mail.blade.php
Open the MyTestMail.php file in our app folder and then write the below code.
Open file MyTestMail.php
<?phpnamespace App\Mail;use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;class MyTestMail extends Mailable
{
use Queueable, SerializesModels;public $details;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($details)
{
$this->details = $details;
}/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->markdown(‘emails.sample-mail’);
}
}
we have created a blade file named sample-mail.blade.php for a laravel email template. This file will be useful to write the design code. We will add the below code into that file.
resources/views/emails/sample-mail.blade.php
Open sample-mail.blade.php
@component(‘mail::message’)
# IntroductionThe body of your message.
<h3>{{ $details[‘title’] }}</h3>
<h3>{{ $details[‘body’] }}</h3>@component(‘mail::button’, [‘url’ => ‘’])
Button Text
@endcomponentThanks,<br>
{{ config(‘app.name’) }}
@endcomponent
Open project to terminal and type the command to start development server
php artisan serve
Open up the URL — http://127.0.0.1/send-mail