SENT
A MAIL IN LARAVEL USIG SMTP SERVER
In this blog I am going to explain the methods of
sending a g mail using smtp server in Laravel
First create a mailer class like this
php artisan make:mail ContactFormMail
We access the this mailer class in a way: App/Mail/ContacFormMail.php
In this file we gave code like this
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class ContactFormMail extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public $contact_data;
public function __construct($contact_data)
{
$this->contact_data = $contact_data;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->subject('Contact Form Mail')->view('emails.contact_mail');
}
}
In this file we create a constructor $contact_data to
the function __construct
After that create a controller for our programs to
implement
php artisan make:controller MailController
App\Http\Controller\MailController
In a above path the controller will be created.
There we create a function like this,
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Mail\ContactFormMail;
class MailController extends Controller
{
//
public function sendContactMail(Request $request){
$contact_data = [ ];
$contact_data['name'] = $request->input('name');
$contact_data['email'] = $request->input('email');
$contact_data['message'] = $request->input('message');
Mail::to('xyz@gmail.com')->send(new ContactFormMail($contact_data));
return redirect()->back()->withSuccess('Email has been
sent!');
}
}
$contact_data
is a array variable is used to store the data which is became a message content
in a mail.
Here I use xyz@gmail.com
for a example instead of that you have to provide a email id which one is
optimized to access a “Less Secure Apps”
Using a $request method we got a values of a array
variable to got this values as a input through a form. we create a html and css
form as a view file in a name of contact_form.blade.php
At the Resources/views folder
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<!------ Include the above in your HEAD tag ---------->
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="well well-sm">
<form class="form-horizontal" action="{{route('send.contact_mail')}}" method="post">
@csrf
@if(session()->has('success'))
<div class="alert
alert-success">
{{ session()->get('success') }}
</div>
@endif
<fieldset>
<legend class="text-center">Contact us</legend>
<!-- Name input-->
<div class="form-group">
<label class="col-md-3
control-label" for="name">Name</label>
<div class="col-md-9">
<input id="name" name="name" type="text" placeholder="Your
name" class="form-control">
</div>
</div>
<!-- Email input-->
<div class="form-group">
<label class="col-md-3
control-label" for="email">Your E-mail</label>
<div class="col-md-9">
<input id="email" name="email" type="text" placeholder="Your
email" class="form-control">
</div>
</div>
<!-- Message body -->
<div class="form-group">
<label class="col-md-3
control-label" for="message">Your message</label>
<div class="col-md-9">
<textarea class="form-control" id="message" name="message" placeholder="Please enter
your message here..." rows="5"></textarea>
</div>
</div>
<!-- Form actions -->
<div class="form-group">
<div class="col-md-12
text-right">
<button type="submit" class="btn
btn-primary btn-lg">Submit</button>
</div>
</div>
</fieldset>
</form>
</div>
</div>
</div>
</div>
In this there was a three input is required they are
@if(session()->has('success'))
{{ session()->get('success') }}
@endif
The above content was used to send a acknowledgemt to
the user whether find mail is sent or not. but it doesn’t provide any error
messages.
Just only works when the emails sent successfully
@csrf
This above content was generating a csrf token for
this post form
<input name="name" type="text" placeholder="Your
name">
<input name="email" type="text" placeholder="Your
email">
<textarea name="message" placeholder="Please enter
your message here" <textarea>
<button type="submit">Submit</button>
These are the
important input fields which has a variable references for the corresponding
function values
Then change the configurtions in a .env file for sending a mail using a
smtp server like this
MAIL_MAILER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
MAIL_USERNAME=Your Username
MAIL_PASSWORD=Your Password
MAIL_ENCRYPTION=ssl
MAIL_FROM_ADDRESS=example@gmail.com
MAIL_FROM_NAME="${APP_NAME}"
In the above content fill your user name and password
Before that you have to turn on “Low secure app
access” using the following steps
Open your google account manage page
And then navigate to the security tab
And Scroll down and turn on the less secure app access
Comments