如何解决从联系表单发送电子邮件的错误

时间:2018-09-26 10:43:47

标签: php laravel laravel-5

我正在尝试从我的联系表发送邮件。但是我遇到了错误。

contact.blade.php是:

<form method="post" action="{{ URL('send') }}">
  {{csrf_field()}}
    <table align="center" width="400">
   <tr>
     <td><strong>Full Name</strong></td>
    <td><input type="text" name="name" required="required" /></td>
      </tr>
       <tr>
      <td><strong>Contact No.</strong></td>
     <td><input type="text" name="mobno" required="required" /></td>
      </tr>
      <tr>
       <td><strong>Email ID</strong></td>
        <td><input type="text" name="email" required="required" /></td>
        </tr>
      <tr>
        <td><strong>Message</strong></td>
        <td><textarea name="msg" cols="30" rows="3" required="required"></textarea></td>
      </tr>
        <tr>
           <td>&nbsp;</td>
           <td><input type="submit" name="submit" /></td>
         </tr>
       </table>
     </form>

web.php是:

Route::POST('send', 'ContactController@send');

ContactController.php是:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\officeholder;
use App\Mail\SendMail;
use Mail;

class ContactController extends Controller
{

public function send()
{
    Mail::send(new sendMail());
}
}

**我已经使用

创建了SendMail.php
  

php artisan make:mail SendMail

通过我的cmd,然后创建App \ Mail \ SendMail.php。**

SendMail.php是:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Http\request;

class SendMail extends Mailable
{
use Queueable, SerializesModels;


public function __construct()
{
    //
}


public function build(Request $request)
{
    return $this->view('contact',['msg'=>$request->msg])->to('mymail@gmail.com');
}
}

但是在单击表单上的按钮后出现错误。

1/1)FatalErrorException 找不到类“ App \ Http \ Controllers \ sendMail”

在ContactController.php第18行

2 个答案:

答案 0 :(得分:2)

更改代码:

Mail::send(new sendMail()); 

Mail::send(new SendMail());

use App\Mail\SendMail;

use \App\Mail\SendMail;

更新:

尝试一下:

public function build(Request $request)
{
    return $this
             ->view('contact')
             ->with(['msg' => $request->msg])
             ->to('mymail@gmail.com');
}

您需要使用函数with()

设置变量

答案 1 :(得分:0)

这样使用

<?php

    namespace App\Http\Controllers;

    use Illuminate\Http\Request;
    use App\officeholder;
    use Mail;
    use App\Mail\SendMail;

    class ContactController extends Controller
    {

          public function send()
          {
              Mail::send(new SendMail());
          }
    }