0

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
MAIL_USERNAME=abdul.elah.js@gmail.com
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('abdul.elah.js@gmail.com')->send('emails.enquiry');

      Mail::send('emails.enquiry', [], function($message) {
        $message->to('abdul.elah.js@gmail.com')->subject('Test');
        $message->from('abdul.elah.js@gmail.com', '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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Abdul-Elah JS
  • 685
  • 1
  • 16
  • 36
  • Did you read laravel log in the storage folder? – NobbyNobbs Jun 27 '18 at 21:24
  • No, i didn't i'm kind of a beginner, could i find the error in there ? @NobbyNobbs – Abdul-Elah JS Jun 27 '18 at 21:24
  • Did you turn on on your Gmail the less secure apps? https://myaccount.google.com/lesssecureapps?pli=1 – Bak87 Jun 27 '18 at 21:27
  • I just did and tried again, still the same error @Bak87 – Abdul-Elah JS Jun 27 '18 at 21:30
  • Probably you could. Look at `./storage/logs/` in the project folder – NobbyNobbs Jun 27 '18 at 21:32
  • @Haboosh Well, then do what NoobbyNoobs tells you, look for the log in storage/logs/ folder and see the laravel.log – Bak87 Jun 27 '18 at 21:33
  • I found this [2018-06-27 21:30:08] local.ERROR: Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required " {"exception":"[object] (Swift_TransportException(code: 530): Expected response code 250 but got code \"530\", with message \"530 5.7.1 Authentication required \" at /Users/Haboosh/Desktop/stars-project/stars-glitter/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php:419) .. @NobbyNobbs – Abdul-Elah JS Jun 27 '18 at 21:35
  • see my last comment @Bak87 – Abdul-Elah JS Jun 27 '18 at 21:36
  • could it have something to do with the _token ? @Bak87 – Abdul-Elah JS Jun 27 '18 at 21:37
  • @Haboosh If you haven't set the csrf token you should do it on every POST request, but that message is from Swift Mailer. https://stackoverflow.com/questions/32515245/how-to-to-send-mail-using-gmail-in-laravel-5-1 – Bak87 Jun 27 '18 at 21:41
  • 1
    I found it the problem was that i had to restart the server in order for changes to take effect. :) .. Thanks for Both of you @Bak87 – Abdul-Elah JS Jun 27 '18 at 21:42

1 Answers1

1

The problem simply was that i didn't restart the server, guess i had to do it after editing the .env file

Abdul-Elah JS
  • 685
  • 1
  • 16
  • 36
  • If you are under php artisan serve, yes, you have to do it. – Bak87 Jun 27 '18 at 21:41
  • Thanks for your help.. @Bak87 but by the way what am i supposed to do in production ? i have the project uploaded to a godaddy web hosting – Abdul-Elah JS Jun 27 '18 at 21:43
  • When modifying .env files, sometimes you don't need to do anything, but if you have those problems you should use in the terminal `php artisan config:clear` and `php artisan cache:clear` – Bak87 Jun 27 '18 at 21:46