Laravel a Better Way to send emails

creates a file at app/Mail/SendFriend.php

It should look something like this 

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class SendFriend 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->view('view.name');
    }
}

Create a view that will hold your email template and update in app/Mail/SendFriend.php

 

It like to keep my email templates organized in an email folder resources/views/email/friend.blade.php

 

Creatre a new blade file for the email template friend.blade.php and put it in the email email folder

 resources/views/email/friend.blade.php

Add the following "code"

This is an example email template.

 

Now go back and update the build method in app/Mail/SendFriend.php

 

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('email.friend');
    }
}

Let's make a controller to send this email

php artisan make:controller SendController

app/Http/Controllers/SendController.php

 

 

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Mail\SendFriend;

class SendController extends Controller
{
    public function html(Request $request)
    {

        
        Mail::to('someone@example.com')->send(new SendFriend());

        return redirect('/')->with(['status' => 'User has been activated']);

    }
}

be sure to add use Illuminate\Support\Facades\Mail;
use App\Mail\SendFriend;

Amazon Recommended Books