105 0 0 0
Last Updated : 2021-03-06 23:57:18
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">المرسل اليه</label>
<input type="email" class="form-control" name="sentToEmail" value="{{ $message->email }}">
</div>
<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>
<div class="form-group col-12 col-sm-12">
<label for="message">الرسالة</label>
<textarea name="message" style="width:100%;" placeholder="نص الرسالة">{{old('message')}}</textarea>
</div>
</div>
</div>
<p class="lead">
<button type="submit" class="btn btn-primary">ارسال</button>
<a class="btn btn-primary" href="{{ route('adminMessages') }}" role="button">رجوع</a>
</p>
</form>
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',
])->validateWithBag('replyErrors');
$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;