How to send Markdown Mail to single and multiple emails in Laravel
Laravel provides you different way to send Mail , in this article we learn how to send mail using markdown mails . We will use mailtrap for sending mail in this article . Let's see how to use markdown mail in Laravel .
Table of Content :
- Setup Mail Configuration
- Setup your Route
- Generate Markdown Mail
- Call your Mail from controller
Step 1 - Setup Mail Configuration :
The very first step is to set up our mail configuration , first setup your mail configuration on your .env file as per your mail credentials as i am using mailtrap for sending emails , Here is how you can setup your mail configurations shown below .
.env :
MAIL_MAILER=smtp MAIL_HOST=smtp.mailtrap.io MAIL_PORT=2525 MAIL_USERNAME=34d7af640c9818 MAIL_PASSWORD=d07708a6993e38 MAIL_ENCRYPTION=tls MAIL_FROM_ADDRESS=studywithkishan@gmail.com MAIL_FROM_NAME="${APP_NAME}"
Step 2 - Setup Routes :
Now set up your routes as per your requirement .
Route::get('/sendmarkdownmail','CheckController@sendMarkdownMail')->name('send-markdown-mail');
Step 3 - Generate Markdown Mail :
Use the following command to create your markdown mail .
php artisan make:mail WelcomeMail --markdown=Email.welcomeMail
It will create WelcomeMail.php file inside your app/Mail directory and another file named welcomeMail.blade.php inside app/resources/views/Email directory .
Step 4 - Calling Mail from Controller :
Now simply call the Mail from your controller function as shown below .
For sending mail to single Mail user .
public function sendMarkdownMail() { $email = 'youremail@gmail.com'; $data['name'] = 'your name'; $data['subject'] = 'Signup Mail'; $data['from'] = 'studywithkishan@gmail.com'; Mail::to($email)->send(new WelcomeMail($data)); }
For sending mail to multiple Mail users .
public function sendMarkdownMail()
{
$emails = ['mail1@gmail.com','mail2@gmail.com','mail3@gmail.com'];
$data['name'] = 'your name';
$data['subject'] = 'Signup Mail';
$data['from'] = 'studywithkishan@gmail.com';
Mail::to($emails)->send(new WelcomeMail($data));
}
Note :
Do not forget to use Mail class and the mail file you are calling in the controller .
use Mail; use App\Mail\WelcomeMail;
WelcomeMail.php :
<?php namespace App\Mail; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; class WelcomeMail extends Mailable { use Queueable, SerializesModels; protected $data; /** * Create a new message instance. * * @return void */ public function __construct($data) { $this->data = $data; } /** * Build the message. * * @return $this */ public function build() { return $this->markdown('Email.welcomeMail')->from($this->data['from'])->subject($this->data['subject']); } }
welcomeMail.blade.php :
@component('mail::message')
# Thank you for Registering
Thank you for reading this article and for being a part of our family
@component('mail::button', ['url' => ''])
Demo Button
@endcomponent
Thanks,<br>
{{ config('app.name') }}
@endcomponent
That's it , now just simple hit your route and check your Mail , you would have got the mail . This article is only giving you the basics of using Laravel Markdown Mail . You can visit Laravel's official site to make more customize your Email .
Output :
Thank you for reading this article 😊
For any query do not hesitate to comment 💬