dougang6178 2018-06-27 21:21
浏览 44

发送邮件Laravel 5.4

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.

  • 写回答

1条回答 默认 最新

  • doubi4491 2018-06-27 21:41
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入