Reputation: 691
I'm trying to send emails in Laravel 5.4 but I keep getting an error "500 internal server error"
I want to send the email using an AJAX request to a controller and from there I would send the email I want to.
Here is my .env file
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD="My Email Password"
MAIL_ENCRYPTION=tls
My ajax request
$('#form').on('submit', function(e) {
e.preventDefault();
e.stopPropagation();
let data = $('#form').serialize();
let name = $('input[name=name]').val();
let phone = $('input[name=phone]').val();
let message = $('textarea[name=message]').val();
$.post('/create', data, function(data, textStatus, xhr) {
console.log(textStatus);
if (data.lang == '/en') {
$('.notification').css('left', '15px');
window.setTimeout(function() {
$('.notification').css('left', '-350px');
}, 5000);
} else {
$('.notification').css('right', '15px');
window.setTimeout(function() {
$('.notification').css('right', '-350px');
}, 5000);
}
$("input[name=name]").val('');
$("input[name=phone]").val('');
$("textarea[name=message]").val('');
});
})
My web.php
Route::post('create', 'EnquiryController@send');
My EnquiryController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Response;
use \App\Mail\Enquiry;
use Mail;
class EnquiryController extends Controller
{
public function send() {
// I tried this way and didn't work so i tried the one beneath
// Mail::to('[email protected]')->send('emails.enquiry');
Mail::send('emails.enquiry', [], function($message) {
$message->to('[email protected]')->subject('Test');
$message->from('[email protected]', 'Abdul Elah');
});
return response()->json([ 'message' => 'Message Sent Successfully' ]);
}
}
and I have a simple html with <h1> Hello World </h1>
in resources/views/emails/enquiry.blade.php
.
The problem I have is that the browser keeps logging the 500 error without any data that could be useful to debug.
Upvotes: 0
Views: 945
Reputation: 691
The problem simply was that i didn't restart the server, guess i had to do it after editing the .env file
Upvotes: 1