【发布时间】:2019-10-29 10:16:51
【问题描述】:
我在以下代码的帮助下使用队列发送电子邮件:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Models\Userprofile;
class testNotify extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;
public $data;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($data)
{
$this->data = $data;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('emails.testnotify');
}
}
在控制器部分我使用发送 ->send(new testNotify($data)); 因为我们不需要队列而不是根据 Laravel 5.8 版的文档发送 p>
public function send(Request $request)
{
$data = $request->Text;
Mail::to('anymail@gmail.com')->send(new testNotify($data));
\Session::put('successmessage','Sent');
return Redirect::back();
}
我正在使用视图:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Laravel Send Email Example</title>
</head>
<body>
<h1>Thank you, {{ $data }}</h1>
</body>
</html>
现在,当我尝试发送时,收到的电子邮件中没有内容。
我已经调试了整个 vendor\laravel\framework\src\Illuminate\Mail\Mailer.php 类,发现 renderView 函数无法渲染视图。
protected function renderView($view, $data)
{
return $view instanceof Htmlable
? $view->toHtml()
: $this->views->make($view, $data)->render();
}
我也将视图文档类型更改为 HTML5,但结果相同,没有内容。
渲染视图正在执行 view->toHtml() 而不是 $this->views->make
我使用的视图 HTML 代码是否正确?或者我是否缺少队列中的任何配置。
请注意,当我移除工具 ShouldQueue 时。我得到了内容。
非常感谢任何指导或帮助。
【问题讨论】:
-
您的邮件类的
build函数中有什么?你叫那个视图模板吗? -
在我的构建类中我有: public function build() { return $this->view('emails.testnotify'); }
-
您能否更新您的问题并将整个邮件类放入以便我识别您的问题?
-
另外你确定你的视图文件位于
resources/views/emails/testnotify.blade.php吗? -
已更新...请检查...感谢您的时间和努力
标签: php laravel swiftmailer laravel-queue laravel-mail