如何邮寄表单值而不是contact.blade.php

时间:2018-09-26 12:21:27

标签: 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');
}
}

但是我的contact.blade.php的屏幕截图正在发送给我的邮件,而不是表单的值,并且也无法在联系页面上重定向。

1 个答案:

答案 0 :(得分:2)

在构造器中传递数据

class ContactController extends Controller
{

public function send()
{
    Mail::send(new SendMail(request()));
    redirect()->to('url');
}
}

在您的邮件中

<?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;
private $request;

public function __construct( Request $request )
{
     $this->request =  $request;
}


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

希望这会有所帮助