Mail Send in Laravel 8 Example Tutorial
Sending email in Laravel or any other project is very required. So, here we implement how to send emails in the Laravel app example easy way step by step. Many times we need to send emails to users which we want to invite or direct send emails using the form. Laravel provides a clean, simple API over the popular SwiftMailer library with drivers for SMTP, Mailgun, Postmark, Amazon SES, and Sendmail, allowing you to quickly get started sending mail through a local or cloud-based service of your choice. Here we use the Mail function to sending emails to users. So, Let’s start with an Email send in Laravel example tutorial with validations messages.
step 1: Install Laravel Project
First Install a Laravel project to run the below command
composer create-project --prefer-dist laravel/laravel Laravel_SendMail
Step 2: Setup Database Credentials
Create a new database custom auth and now open your .env file and add your database credentials
Step 3: Update SMTP Details in .env File
First update your email trap details in your .env file just like below, You can send an email via log. If you have not to email trap details got to this link and create a new account.
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your email
MAIL_PASSWORD='your password'
MAIL_ENCRYPTION=tls
Step 4: Add Routes
Now create two routes in your web.php file one for showing form and another is sending email to the user which you want.
routes\web.php
Route::get('/email', 'EmailController@create');
Route::post('/email', 'EmailController@sendEmail')->name('send.email');
Step 5: Update Controller
You can create a new controller or update these methods in your existing controller. Here we create a new controller you can also create a new after running the below command.
php artisan make:controller EmailController
After successfully create a new controller you can update the below code in your file.
app\Http\Controllers\EmailController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;
class EmailController extends Controller
{
public function create()
{
return view('email');
}
public function sendEmail(Request $request)
{
$request->validate([
'email' => 'required|email',
'subject' => 'required',
'name' => 'required',
'content' => 'required',
]);
$data = [
'subject' => $request->subject,
'name' => $request->name,
'email' => $request->email,
'content' => $request->content
];
Mail::send('email-template', $data, function($message) use ($data) {
$message->to($data['email'])
->subject($data['subject']);
});
return back()->with(['message' => 'Email successfully sent!']);
}
}
Step 6: Create a Blade FIle
Now you need to create a blade file where you show the email send the form in Laravel.
resources\views\email.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Mail Send in Laravel Example</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto|Courgette|Pacifico:400,700">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
<style>
body {
color: #000;
background-image: url("background.jpg");
font-family: "Roboto", sans-serif;
}
.contact-form {
padding: 50px;
margin: 30px auto;
}
.contact-form h1 {
font-size: 42px;
font-family: 'Pacifico', sans-serif;
margin: 0 0 50px;
text-align: center;
}
.contact-form .form-group {
margin-bottom: 20px;
}
.contact-form .form-control, .contact-form .btn {
min-height: 40px;
border-radius: 2px;
}
.contact-form .form-control {
border-color: #e2c705;
}
.contact-form .form-control:focus {
border-color: #d8b012;
box-shadow: 0 0 8px #dcae10;
}
.contact-form .btn-primary, .contact-form .btn-primary:active {
min-width: 250px;
color: #fcda2e;
background: #000 !important;
margin-top: 20px;
border: none;
}
.contact-form .btn-primary:hover {
color: #fff;
}
.contact-form .btn-primary i {
margin-right: 5px;
}
.contact-form label {
opacity: 0.9;
}
.contact-form textarea {
resize: vertical;
}
.bs-example {
margin: 20px;
}
</style>
</head>
<body>
<div class="container-lg">
<div class="row">
<div class="col-md-8 mx-auto">
<div class="contact-form">
<h1>Get in Touch</h1>
@if(session()->has('message'))
<div class="alert alert-success">
{{ session()->get('message') }}
</div>
@endif
<form action="{{ route('send.email') }}" method="post">
@csrf
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label for="inputName">Name</label>
<input type="text" name="name" class="form-control" placeholder="Enter Name">
@error('name')
<span class="text-danger"> {{ $message }} </span>
@enderror
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="inputEmail">Email</label>
<input type="email" name="email" class="form-control" placeholder="Enter Email">
@error('email')
<span class="text-danger"> {{ $message }} </span>
@enderror
</div>
</div>
</div>
<div class="form-group">
<label for="inputSubject">Subject</label>
<input type="text" name="subject" class="form-control" placeholder="Enter subject">
@error('subject')
<span class="text-danger"> {{ $message }} </span>
@enderror
</div>
<div class="form-group">
<label for="inputMessage">Message</label>
<textarea name="content" rows="5" class="form-control" placeholder="Enter Your Message"></textarea>
@error('content')
<span class="text-danger"> {{ $message }} </span>
@enderror
</div>
<div class="text-center">
<button type="submit" class="btn btn-primary"><i class="fa fa-paper-plane"></i> Send</button>
</div>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
Step 6: Email Template
When we send an email then we need a template that shows the message.
resources\views\email-template.blade.php
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Welcome!</div>
<div class="card-body">
@if (session('resent'))
<div class="alert alert-success" role="alert">
{{ __('A fresh mail has been sent to your email address.') }}
</div>
@endif
{!! $content !!}
</div>
</div>
</div>
</div>
</div>
Great! You successfully lean Email Send Example in Laravel. I hope it works for you. If you have any questions don’t hesitate to drop a message below.
3 Comments
404 notfound when hit the post method
ReplyDeleteCannot send message without a sender address
ReplyDeleteCannot send message without a sender address
ReplyDeleteCAN FEEDBACK
Emoji