Create a Contact Form in Laravel & send email in your gmail account
- Post by: Public
- on: 01-03-20
How to dynamic contact Form in laravel & send mail to your email accounts.
Making a dynamic contact Form you must setting your email port and smtp (simple mail transfer protocol).Today we will make a contact form by using google mail smtp system.
At first you need to login your gmail account for setting your google email system for this Click on..
settings>Forwarding and POP/IMAP >
Enable POP for all mail
Enable IMAP
Save Changes
Then you need allow less secure apps:on > for this go to your email
settings option > secure & profile > Click and allow Less secure apps: ON
New you need setting your .env file >find it on your project set mail driver option with original gmail account and password
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=write_your_email_password
MAIL_ENCRYPTION=tls
Now you need to Create a route for view and need to create a controller (ContactControlle.php) for making email function.
#web.php
Route::post('/contact/store',[
'uses' =>'ContactController@store',
'as' => 'contact.store'
]);
#App\Htp\ContactControlle.php
public function index(){
return view('contact'); //view for contact from
}
//Email send option
public function store(Request $request ){
$this->validate($request,[
'name'=>'required',
'email'=>'required|email',
'phone'=>'required',
'user_message'=>'min:10',
'a_file'=>'mimes:jpeg,jpg,png,pdf,docx,xls,ppt,gif,svg,doc',
]);
$data=array(
'name'=>$request->name,
'email'=>$request->email,
'phone'=>$request->phone,
'user_message'=>$request->user_message,
'a_file'=>$request->a_file,
);
\Mail::send('mail.contact',$data, function ($message) use ($data) {
$message->to('[email protected]');
$message->subject($data['name']);
$message->from($data['email']);
$message->attach($data['a_file']->getRealPath(), array(
'as' => 'a_file.' . $data['a_file']->getClientOriginalExtension(),
'mime' => $data['a_file']->getMimeType()
));
});
session::flash('success', 'Thanks : Your message send Successfully.');
return redirect()->back();
}
NB: Also use this on header
use Session;
use Mail;
use Image;
use Storage;
use App\Mailfile;
In your view blade form action should follow as blew
<form action="{{route('contact.store')}}" method="POST" enctype="multipart/form-data">
@csrf
// input filed will go through as your design.
</form>
Now we are ready to send a email and check your email account for getting message from your contact us page.
Thanks for reading, please share with your friends who love to learn laravel
Comments