How to send email in laravel using markdown

133919 views 5 years ago Laravel

Laravelcode share with you how to send email in laravel using laravel markdown functionality. it is very easy and very simple. so we are write here and share with you very easy way how to send email using email markdown in laravel.

In laravel 5.4 send email functionality something change. in laravel 5.4 provided email markdown functionality for send email

Markdown mailable messages allow you to take advantage of the pre-built templates and components of mail notifications in your mailables. Since the messages are written in Markdown, Laravel is able to render beautiful, responsive HTML templates for the messages while also automatically generating a plain-text counterpart.

Here we are share with you email send in laravel example when user register then after send one confirmation link on use email address

Step : 1 Create new laravel application

Here, we are create new one laravel project by using collowing command


composer create-project --prefer-dist laravel/laravel blog

Step : 2 Create route for send mail

Route::get('sendemail', '[email protected]')->name('sendemail');

Step : 4 Create SendEmailController

Now, we are need to create SendEmailController.php file in app/Http/Controllers folder


<?php
namespace App\Http\Controllers;
use App\Mail\TestMail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Http\Controllers\Controller;
class SendEmailController extends Controller
{
    /**
     * Ship the given order.
     *
     * @param  Request  $request
     * @param  int  $orderId
     * @return Response
     */
    public function ship(Request $request)
    {
        $valueArray = [
        	'name' => 'John',
        ];
        // Test mail...
        try {
            Mail::to('[email protected]')->send(new TestMail($valueArray));
            echo 'Mail send successfully';
        } catch (\Exception $e) {
            echo 'Error - '.$e;
        }
    }
}

Step : 4 Create email markdown controller

Here, we are create new one email markdown controller ans as well as email templete blade using following command


php artisan make:mail TestMail --markdown=emails.testmail

After run above command you can see TestMail.php file automatice created in app/Mail folder and your email templet/blade file testmail.php is created in resources/views/emails folders

Step : 5 Open your TestMail.php file

Now, open your app/Mail/TestMail.php file and make changes look like.


<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class TestMail extends Mailable
{
	use Queueable, SerializesModels;
	/**
	 * Create a new message instance.
	 *
	 * @return void
	 */
	public function __construct($content)
	{
		$this->content = $content;
	}
	/**
	 * Build the message.
	 *
	 * @return $this
	 */
	public function build()
	{
		return $this->markdown('emails.testmail') //pass here your email blade file
	    	->with('content',$this->content);
	}
}

Step : 6 Open your testmail.blade.php file

[ADDCODE]

Now oprn your resources/views/emails/testmail.blade.php file and make changes look like.


@component('mail::message')
# Dear, {{$content['name']}}
You are receiving this email because we received a signup request for your this mail account.
@component('mail::button', ['url' => 'website.com')])
Click Here
@endcomponent
If you did not request a signup , no further action is required.
Thanks,
{{ config('app.name') }}
@endcomponent

Now we are ready to run our example so run bellow command ro quick run:

php artisan serve

Now you can open bellow URL on your browser:

http://localhost:8000/sendemail

I hope it can help you...

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]