send emails with laravel

1040 0 0 0

Last Updated : 2024-04-26 18:17:16

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">???

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.