我正在尝试设置联系表格,一切顺利。使用 - > send()设置我的控制器,一切正常(需要一点时间)。当我将其设置为使用 - > queue()时,似乎工作正常(没有延迟),作业设置,发送时发送邮件。但这次我的邮件模板不包含发送给邮件程序的数据。
我的控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Mail\Contact;
use Illuminate\Support\Facades\Mail;
class PagesController extends Controller
{
public function sendContact(Request $request)
{
Mail::to('webform@email.com')
->queue(new Contact($request));
return redirect('/contact')->with('status', 'Message sent. Thanks!');
}
}
我的邮件程序(App \ Mail \ Contact):
class Contact extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct()
{
}
/**
* Build the message.
*
* @return $this
*/
public function build(Request $request)
{
$subject = 'Web Message from: ' . $request->name;
return $this->from('myemail@email.com')
->subject($subject)
->view('emails.contact-template')
->with([
'name' =>$request->name,
'email' => $request->email,
'message' => $request->message,
'date' => $request->date,
]);
}
}
答案 0 :(得分:0)
问题在于我需要将变量声明为public。以下是最终有效的解决方案:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Http\Request;
use Illuminate\Contracts\Queue\ShouldQueue;
class Contact extends Mailable
{
use Queueable, SerializesModels;
public $request;
public $name;
public $from;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct(Request $request)
{
$this->request = $request->all();
$this->name = $request->name;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
$subject = 'Webform messsage from: ' . $this->name;
$from = 'webform@mail.com';
return $this
->from( $from )
->subject($subject)
->view('emails.contact-template');
}
}