I have this weird issue that I have never came across before ever since I started learning laravel.
I have a contact form: contact.blade.php
{!! Form::open(['url' => '/contact-form', 'id' => 'formContact']) !!}
<div class="col-md-12 col-sm-12 form-group">
{!! Form::label('full_name', 'Full Name:', ['class' => 'fw_400']) !!}
{!! Form::text('full_name', null, ['class' => 'form-control input-sm']) !!}
</div>
<div class="col-md-6 col-sm-6 form-group">
{!! Form::label('email', 'Email:', ['class' => 'fw_400']) !!}
{!! Form::text('email', null, ['class' => 'form-control input-sm']) !!}
</div>
<div class="col-md-6 col-sm-6 form-group">
{!! Form::label('contact', 'Contact Number:', ['class' => 'fw_400']) !!}
{!! Form::text('contact', null, ['class' => 'form-control input-sm']) !!}
</div>
<div class="col-md-12 col-sm-12 form-group">
{!! Form::label('contact_message', 'Contact Message:', ['class' => 'fw_400']) !!}
{!! Form::textarea('contact_message', null, ['class' => 'form-control input-sm', 'rows' => 5]) !!}
</div>
<div class="col-md-12 col-sm-12 form-group">
{!! Form::submit('Submit', ['class' => 'btn btn-primary btn-block btnContactOnContactPage']) !!}
</div>
{!! Form::close() !!}
When I click on the submit button, AJAX call is made.. Here's the ajax handler:
(function() {
$('#formContact').submit(function(e) {
$('.btnContactOnContactPage').prop('disabled', true);
var inputData = $('#formContact').serialize();
$.ajax({
url: $('#formContact').attr('action'),
type: 'POST',
data: inputData,
success: function(m) {
$('.btnContactOnContactPage').prop('disabled', false);
if (m.status === 'success') {
toastr.success(m.msg);
setTimeout(function() {
window.location.reload();
}, 3000)
}
},
error: function(e) {
if (e.status === 422) {
$('.btnContactOnContactPage').prop('disabled', false);
var errors = e.responseJSON;
var errorsHtml = '<div class="alert alert-danger"><ul>';
errorsHtml += '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>';
$.each(errors, function(key, value) {
errorsHtml += '<li class="text-danger">' + value[0] + '</li>';
});
errorsHtml += '</ul></div>';
$('.errors').html(errorsHtml);
}
}
});
return false;
});
})();
And the controller method:
public function postContact(Request $request)
{
$this->validate($request, [
'full_name' => 'required',
'email' => 'required|email',
'contact' => 'required|regex:/^[0-9+\-]+$/',
'contact_message' => 'required'
]);
if ($request->ajax()) {
$prevContactCode = DB::table('contact_messages')->latest()->limit(1)->pluck('cont_code');
if ($prevContactCode == "" || empty($prevContactCode)) {
$request['cont_code'] = 'CON-000001';
}
else {
$request['cont_code'] = ++$prevContactCode;
}
$contactMessage = ContactMessage::create($request->all());
$dataToMail = [
'cont_code' => $contactMessage->cont_code,
'full_name' => $contactMessage->full_name,
'email' => $contactMessage->email,
'contact' => $contactMessage->contact,
'contact_message' => $contactMessage->contact_message
];
$mail = Mail::send('emails.contact', $dataToMail, function ($message) use($dataToMail)
{
$message->from($dataToMail['email'], $dataToMail['full_name']);
$message->to('contact@example.com', 'Example Contact')
->subject('Contact Form Details from example.com (Form No.:' . $dataToMail['cont_code'] . ')')
->cc(['ex@example.net', 'exam@hotmail.com'])
->bcc(['examp@gmail.com', 'esmap@example.net']);
});
var_dump($mail); // displays **int 5** in chrome console
dd($mail); // displays **5** in chrome console
if ($mail) {
return response(['status' => 'success', 'msg' => 'Contact form details has been successfully submitted.']);
}
}
return response(['status' => 'failed', 'msg' => 'Something went wrong. Please try again later.']);
}
The issue is that, the mail is sent thrice instead of just once. I don't know what is happening here. Earlier [maybe 5 months back], it was working correctly, and now, all of a sudden, it started doing this. I am trying to figure out what is the reason behind this, but to no success. I have also updated the framework using composer update
but the issue still exists.
EDIT 1: I was playing trial and error with the Mail
facade, and I guess the issue is in bcc
method, because I tried no bcc
and cc
, it worked as expected. Then again I tried with just cc
and no bcc
, this time also it worked correctly as expected but when added bcc
with cc
, I received the same mail thrice.
Where I have made the mistake ? Kindly help me out.