Environment Setting ==> .env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=myemail@gmail.com
MAIL_PASSWORD=GmailPassword
MAIL_ENCRYPTION=ssl
or use mailtrap.io for testing purpose
Host: smtp.mailtrap.io
Port: 25 or 465 or 2525
Username: 123456789random
Password: 13256465random
Auth: PLAIN, LOGIN and CRAM-MD5
TLS: Optional
Note: When goes on live server, then it may not works. Because respected server smtp must be used in MAIL_HOST and MAIL_FROM_ADDRESS
Route Setting ==> web.php
Route::post('contact/sendemail/{contact_email}','Front\PageController@sendemail')->name('contact.sendemail');
Controller ==> PageController.php
Please note following facades must include on top before the class begins
use Illuminate\Support\Facades\Mail;
public function sendemail(Request $request,$contact_email)
{
$data = $request->validate([
'name' => 'required|max:30|min:2',
'email' => 'required|email|max:50|min:2',
'phone' => 'required|numeric|max:999999999999|min:11111111',
'subject' => 'required|max:30|min:2',
'message' => 'required|max:400|min:2',
],[
'phone.numeric' => 'Phone number must be the number'
]);
$name = $data['name'];
$email = $data['email'];
$phone = $data['phone'];
$subject = $data['subject'];
$message = $data['message'];
$sender_name = $data['name'];
$sender_email = $data['email'];
$subject = "BENERA PTY. LTD. ==>".$data['subject'];
$sub = $data['subject'];
$receiver_email = $contact_email;
$receiver_name = 'Benera Pty. Ltd.';
$data2 = array(
'name' => $name,
'email' => $email,
'phone' => $phone,
'subject' => $subject,
'msg' => $message,
'sub' => $sub,
);
Mail::send('email.template',$data2,function($message) use (
$sender_name,
$sender_email,
$subject,
$receiver_email,
$receiver_name
){
$message->to($receiver_email,$receiver_name)->subject($subject);
$message->from($sender_email,$sender_name);
// $message->cc('cc@gmail.com');
// $message->bcc('bcc@gmail.com');
// $message->attachment('path to attach','name');
});
return redirect()->back()->withSuccess('Email is sent successfully');
}
Template ==> Contact Form
<!DOCTYPE html>
<html>
<head>
<title>Email Received</title>
</head>
<body>
<h4 style="text-align: center"> ======= CONTACT FORM ======= </h4>
<h1 style="text-align: center">Benera Pty. Ltd.</h1>
<h4 style="text-align: center"> ======= CONTACT FORM ======= </h4>
<h3 style="text-align: center;color: green">Subject: {{$sub}}</h3><br>
<div class="row">
<div style="font-size: 19px;" class="col-md-12">
<label>...................................................... </label><br>
<label>From: {{$name}} </label><br>
<label>Phone: {{$phone}} </label><br>
<label>Email: {{$email}} </label><br>
</div>
<div class="col-md-12">
<label>...................................................... </label><br>
<label style="font-size: 16px">{{$msg}} </label><br><br>
<label> ************************* </label><br>
<label style="font-style: italic;"> This is end of message </label><br>
<label style="font-style: italic;"> - Benera Pty. Ltd. </label><br>
<label> ************************* </label><br>
</div>
</div>
</body>
</html>