send emails with laravel

43 0 0 0

Last Updated : 2020-12-03 16:11:31

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


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">
<meta charset="utf-8">
<h2>{{ $subject }}</h2> // subject of your message

{{ $message }} // message of your message
{{ config('') }}


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">
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>

@if (\Session::has('msg'))
<div class="col-12 col-sm-12 alert alert-success text-right">
{{ \Session::get('msg') }}

<div class="form-group col-12 col-sm-12">
<label for="sentToEmail">المرسل اليه</label>
<input type="email" class="form-control" name="sentToEmail" value="{{ $message->email }}">

<div class="form-group col-12 col-sm-12">
<label for="subject">عنوان الرد</label>
<input type="text" name="subject" style="width:100%;" placeholder="عنوان الرد" value="{{old('subject')}}">

<div class="form-group col-12 col-sm-12">
<label for="message">الرسالة</label>
<textarea name="message" style="width:100%;" placeholder="نص الرسالة">{{old('message')}}</textarea>


<p class="lead">
<button type="submit" class="btn btn-primary">ارسال</button>
<a class="btn btn-primary" href="{{ route('adminMessages') }}" role="button">رجوع</a>

5-  in your controller create function to receive data from form , validate and send email with your new data like this

 public function sendReply(Request $request) {
// validate
Validator::make($request->all(), [
'subject' => 'required',
'message' => 'required',

$data = [
'email' => $request->input('sentToEmail'),
'subject' => $request->input('subject'),
'message' => $request->input('message'),

Mail::to( $data['email'] )->send( new welcomeMail($data) ); // this is the main syntax for sending emails using laravel

return redirect()->back()->with('msg', 'تم الرد بنجاح');


DON'T FORGET .. to include mail in your controller like this

use App\Mail\welcomeMail ;
use Illuminate\Support\Facades\Mail;


Mahmoud Anwar

Mahmoud Anwar

Back End Developer with a passion for developing innovative web applications that expedite the efficiency and effectiveness of organizational success. Well-versed in technology and writing code to create systems that are reliable and user-friendly. Also has the proven ability to motivate, educate, and collaborate effectively to build web applications and effectively track changes. Confident communicator, strategic thinker, and innovative creator to develop software that is customized to meet a company’s organizational needs, highlight their core competencies, and further their success.