1237 0 0 0
Last Updated : 2025-04-28 20:34:04
How to send emails with mailable in laravel and how to customize your email content
to create and send emails using laravel you must follow these steps carefully
1- you should use make:mail in artisan to create mailable system in laravel
php artisan make:mail [wanted name of mail.php file] -m [specified view for blade file of the email itself]
// for example
php artisan make:mail [welcomeMail] -m [emails.welcome]
the command above will create a welcomeMail.php in app\Mail folder and welcome.blade.php in resources\views\emails folder
2- now you should modify them to create your own email contents .. so pass $data for construct and build functions in welcomeMail.php like this
<?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;
public $data; // this is important to add , don't forget it
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($data) // assign $data as a parameter
{
$this->email = $data['email'];
$this->subject = $data['subject'];
$this->message = $data['message'];
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->markdown('emails.welcome') // this is the view of your email blade file
-> with ([
'subject' => $this->subject ,
'message' => $this->message ,
]);
}
}
3- modify welcome blade to receive and format content of the email as you want in welcome.blade.php file like his
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
</head>
<body>
<h2>{{ $subject }}</h2> // subject of your message
<div>
<p>
{{ $message }} // message of your message
</p>
Thanks,<br>
{{ config('app.name') }}
</div>
</body>
</html>
4- now create a form to send your emails through like this
<form action="{{ route('sendReply') }}" method="post">
<div class="row">
@if ($errors->any())
<div class="col-12 col-sm-12 alert alert-danger text-right">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
@if (\Session::has('msg'))
<div class="col-12 col-sm-12 alert alert-success text-right">
{{ \Session::get('msg') }}
</div>
@endif
@csrf
<div class="form-group col-12 col-sm-12">
<label for="sentToEmail">???
Read Also