When building Laravel applications, each type of email
sent by your application is represented as a "mailable" class. First,
you may specify the "from" address on your message's envelope. Within
a mailable class's content method, you may define the view, or which template
should be used when rendering the email's contents. Markdown mailable messages
allow you to take advantage of the pre-built templates. Laravel is able to
render beautiful, responsive HTML templates for the messages while also
automatically generating a plain-text counterpart. So, Let’s start with an Laravel Contact Form Send
Email using Gmail SMTP.
> Link to My Github Repository for Code Download<
Step 1: Install Laravel 10
This step is not required; however, if you have not
created the Laravel app, then you may go ahead and execute the below command:
composer
create-project laravel/laravel example-app
Step 2: Make Gmail Configuration
Make sure you must open your Gmail account then follow
step 1 from 7. Finally copy password (Show Figure-1) for use setup .env
file.
Figure-1
Step 3: Update SMTP Details in .env File
In Third step, you must add .env file
configuration with installed Laravel file. Firstly set ( MAIL_MAILER, MAIL_HOST, MAIL_PORT, MAIL_ENCRYPTION, MAIL_FROM_NAME) are fixed as figure -2 then set MAIL_USERNAME will add Gmail account name when you create at first and
MAIL_PASSWORD will add your
copy password(Show Figure-1). MAIL_FROM_ADDRESS and MAIL_USERNAME will same .
So, you can
simply add as like following.
.env
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=youmailname@gmail.com
MAIL_PASSWORD=lfmgwuousmtnqfas
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS= youmailname@gmail.com
MAIL_FROM_NAME="${APP_NAME}"
Figure-2
Step 4: Add Routes
Now create two routes in your web.php file one for
showing Contact form and another is sending email to the user which you want.
routes\web.php
<?php
use
Illuminate\Support\Facades\Route;
use App\Http\Controllers\MailController;
//
email send
Route::get ('/',[MailController::class,'mailform']);
Route::post ('/send-mail',[MailController::class,'maildata'])->name('send_mail');
Step 5: Create View Contact Form
Now you need to create a blade file where you show the
email send the form in Laravel.
resources/views/mail.blade.php
<!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> Laravel mail Send Syetem </title>
</head>
<body>
<section id="contact" class="contact">
<div class="container" data-aos="fade-up">
<div class="row">
<div class="col-lg-7 mt-5 mt-lg-0 d-flex align-items-stretch">
<form action="{{ route('send_mail') }}" method="POST"
enctype="multipart/form-data">
@csrf
<div class="row">
<div class="form-group col-md-6">
<label for="exampleInputEmail">Your Name</label>
<input type="text" name="name" class="form-control" required>
</div>
<div class="form-group col-md-6">
<label for="exampleInputEmail">Your Email</label>
<input type="email" class="form-control" name="email" required>
</div>
</div>
<div class="form-group">
<label for="exampleInputEmail">Subject</label>
<input type="text" class="form-control" name="sub" required>
</div>
<div class="form-group">
<label for="exampleInputEmail">Message</label>
<textarea class="form-control" name="mess" rows="10"
required></textarea>
</div>
<div class="text-center"><button type="submit">Send Mail</button></div>
</form>
</div>
</div>
</div>
</section><!-- End Contact
Section -->
</body>
</html>
Step 6: Create 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 MailController
After successfully create a new controller you can
update the below code in your file. Now, update code on MailController file.
app/Http/Controllers/MailController.php
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use
Illuminate\Support\Facades\Route;
use App\Mail\SendMail;
use App\Mail\SendMessageToEndUser;
use Mail;
class MailController extends Controller
{
public function mailform()
{
return view('mail');
}
public function maildata(Request $request)
{
$name = $request->name;
$email = $request->email;
$sub = $request->sub;
$mess = $request->mess;
$mailData = [
'url' => 'https://sandroft.com/',
];
$send_mail = "sahincseiu@gmail.com";
Mail::to($send_mail)->send(new SendMail($name, $email, $sub, $mess));
$senderMessage = "thanks for your message , we will reply you in later";
Mail::to( $email)->send(new
SendMessageToEndUser($name,$senderMessage,$mailData));
return "Mail Send Successfully";
}
}
Step 7: Generating Mailable Class with Markdown (Two Part)
Part – 7.1: Send Email to Admin
To generate a mailable with a corresponding Markdown template, you may use the --markdown option of the make:mail Artisan command. So, let's run bellow command.
php artisan make:mail SendMail --markdown=SendMail
now, let's update code on SendMail.php file as bellow:
app/Mail/SendMail.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use
Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use
Illuminate\Mail\Mailables\Content;
use
Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
// use
App\Http\Controllers\MailController;
class SendMail extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*/
public $name, $email, $sub, $mess;
public function __construct($name, $email, $sub, $mess)
{
$this->name = $name;
$this->email = $email;
$this->sub = $sub;
$this->mess = $mess;
}
/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
// subject: 'Sandroft Website Mail',
subject: $this->sub,
);
}
/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
// view: 'sand',
markdown: 'email_to_admin',
);
}
/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [];
}
}
Part - 7.2: Send Reply Email to User
To generate a mailable with a corresponding Markdown
template, you may use the --markdown option of the make:mail Artisan command. So,
let's run bellow command.
php artisan make:mail SendMessageToEndUser --markdown=SendMessageToEndUser
now, let's update code on SendMessageToEndUser.php file as bellow:
app/Mail/ SendMessageToEndUser.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use
Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use
Illuminate\Mail\Mailables\Content;
use
Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
class SendMessageToEndUser extends Mailable
{
use Queueable, SerializesModels;
public $senderMessage = '';
public $name = '';
public $url = '';
public $mailData;
/**
* Create a new message instance.
*/
public function __construct( $name, $senderMessage,$mailData )
{
$this->name = $name;
$this->senderMessage = $senderMessage;
$this->mailData = $mailData;
}
/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
subject: 'Send Message To End User',
);
}
/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
// view: 'enduser-mail',
markdown: 'reply_email',
);
}
/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [];
}
}
Step 8: Make View File for Email Body (Two Part)
In this framework email body content has been get from
view file. We below we have made one
Part - 8.1: Create Blade View for Send Email to Admin
When we send an email then we need a template that
shows the message.
resources/views/email_to_admin.blade.php
@component('mail::message')
# Name: {{ $name }}
#
Email: {{ $email }}<br>
Subject:
{{ $sub }} <br><br>
Message:<br> {{ $mess }}
{{--
@component('mail::button', '$url')
Visit
Our Website
@endcomponent
--}}
Thanks,
{{ config('app.name') }}
@endcomponent
Part - 8.2: Create Blade View for Send Reply Email to User
resources/views/reply_email.blade.php
@component('mail::message')
# Hi {{ $name }},
{{-- #
{{ $senderMessage }} --}}
Receive
your email. I will try quicly answer.
@component('mail::button', ['url' => $mailData['url']])
Visit
Our Website
@endcomponent
Thanks,
{{ config('app.name') }}
@endcomponent
Final Step: Run Laravel App.
All the required steps have been done, now you have to
type the given below command and hit enter to run the Laravel app:
php
artisan serve
Now, go to your web browser, type the given URL and
view the app output:
http://localhost:8000/mail
Output:
End User Send Mail to
Website Owner |
Reply Mail Website Owner |
|
|
Great! You successfully lean Email Send Example in
Laravel 10. I hope it works for you. If you have any questions don’t hesitate
to drop a message below or contact my email sahincseiu@gmail.com.