I am new to laravel and worked through the laravel mailable doc which ended me with a mailable
that looks like this:
ContactMail.php
:
<?php
namespace App\Mail;
use http\Env\Request;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class ContactMail extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;
public function __construct()
{
//
}
public function build()
{
return $this->markdown('emails.contact');
}
public function store(Request $request)
{
$contactVar = 'hello world!';
Mail::to($request->user())->send(new contact($contactVar));
}
}
I want the store
function of this mailable
to be called from my ContactController.php
:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
class ContactController extends Controller
{
public function submit(Request $request) {
$this->validate($request, [
'name' => 'required',
'email' => 'required | email',
'message' => 'required'
]);
// store(...)
return response()->json(null, 200);
}
}
How would I call the function and is this good practice? Sorry if it's obvious, I'm trying to get the hang of Laravel/PHP
.
This is the corresponding email template file
(contact.blade.php
):
@component('mail::message')
# Introduction
The body of your message.
@component('mail::button', ['url' => ''])
Button Text
@endcomponent
Thanks,<br>
{{ config('app.name') }}
@endcomponent