Whenever we start building any application. The mailing system does matter a lot because this is what keeps engaging users with the application. So, this is a very important part also. Let’s learn together how to send mail in laravel and with templates also.
Setup A Form
First of all you have to install Laravel (composer create-proeject laravel/laravel sendmail)
. After doing that you have to setup a form when we do install laravel application we always have welcome.blade.php
file in our views folder. So, first of all create a form in that welcome.blade.php
file.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Send Mail</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row py-5 my-5 mx-5 px-5">
<h2>Send Mail</h2>
<form action="{{route('sendmail')}}" class="form" method="POST">
@csrf
<div class="row">
<div class="col-lg-6">
<input type="email" name="email" placeholder="email" class="form-control">
</div>
<div class="col-lg-6">
<input type="text" name="title" class="form-control" placeholder="title">
</div>
<div class="col-lg-12 pt-2">
<textarea name="content" placeholder="write..." class="form-control" cols="30"
rows="10"></textarea>
</div>
<div class="col-lg-12 pt-2">
<button class="btn btn-primary" type="submit">Send</button>
</div>
</div>
</form>
</div>
</div>
</body>
</html>
After doing this we will have a form on our first page that will be the welcome page. And my friend, when you did that then you might have an urge to save this form because without a database form is nothing. okay, But we are not going to save it into the database. Do you know why because we don’t need to save it? We will send mail directly from form to user. Let’s see how?
Setup .env File
In the .env file, we keep our configuration details. Mostly we use database configuration and mail. So, before going forward we have to do with our .env file, and let’s see the code.
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=fbbbf29se90e2d7
MAIL_PASSWORD=36f531se353bc7d
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"
Rather than using my own details, I do prefer to use mailtrap.io It provides you a temporary username and password so that you can test your mail in laravel or in another application as well.
Make Mail
Before going to any other point let’s make mail first because without mail we are not going to do anything. Okay, We will use the command to create mail like this.
php artisan make:mail sendmail --markdown=emails.sendmail
When you will hit this command you will get a folder of emails into your resource > views directory. And also you will get another directory in your App directory (App/Mail) and in this, you will have a mailable file so that you can send mail easily.
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class sendmail extends Mailable
{
use Queueable, SerializesModels;
public $subject;
public $content;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($subject,$content)
{
$this->subject = $subject;
$this->content = $content;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->markdown('emails.sendmail')->from('abc@gmail.com')->subject($this->subject)->with('body',$this->content);
}
}
Make Controller
dude, you know that without a controller how we can move forward. So, let’s make the controller first.
php artisan make:controller mailsend
Now, we have a controller that can’t do anything without a route, In this controller, we will configure mail and send it to the user. Let’s see the controller code and how we will do this.
<?php
namespace App\Http\Controllers;
use App\Mail\sendmail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
class MailController extends Controller
{
public function send(Request $request){
Mail::to($request->email)->send(new sendmail($request->title,$request->content));
return back();
}
}
Make Route
After setting up the controller and mail file. We just need the route to handle all the requests. We will not run so much of requests but we need a route because the route is main part in this process without route we can’t do anything. Let’s make a route to handle form requests.
<?php
use App\Http\Controllers\MailController;
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::post('sendmail',[MailController::class,'send'])->name('sendmail');
Now the setup is done and to use the template we have to use the blade file and in blade we would have a markdown template.
@component('mail::message')
# Introduction
{{$body}}
@component('mail::button', ['url' => ''])
Button Text
@endcomponent
Thanks,<br>
{{ config('app.name') }}
@endcomponent
And after doing this setup you can receive mail in your mail trap account.
Conclusion
I hope you will learn from this tutorial. But I learned a lot of things from this. If you do like these kinds of content and content about web development you can subscribe to my youtube channel also.
Let’s learn together and learn fast
larachamp
The post How to send mail in Laravel with template first appeared on larachamp.com.
The post How to send mail in Laravel with template appeared first on larachamp.com.
Top comments (0)