将PDF文件附加到Laravel中的DOMPDF的电子邮件
我正在使用我的肌邮件模型将PDF文件附加到它。邮件随我想要的所有数据发送,并且PDF附加了邮件,但是它是空的,如果我附加,我该如何将布局和数据导入PDF?
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Course;
use Barryvdh\DomPDF\PDF;
class MyEmail extends Mailable {
use Queueable, SerializesModels;
public $data;
public $course;
private $pdf;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($data, Course $course) {
$this->course = $course;
$this->data = $data;
}
/**
* Build the message.
*
* @return $this
*/
public function build() {
return $this->subject('Thanks')->view('emails.my_email')->attachData($this->pdf, 'my_pdf.pdf')->with(['data' => $this->data, 'course' => $this->course]);
}
}
这是向我的控制器发送电子邮件的功能:
public function sendMail(Request $request) {
$data = array(
'first_name' => $request->first_name,
'last_name' => $request->last_name,
'id' => $request->id,
'course_id' => $request->course_id,
);
$resultJson = $this->validateRecaptcha($request);
$course= Course::where('id', $data['course_id'])->first();
if($resultJson->success != true) :
return redirect()->back()->with(['success' => 'success']);
else :
\Mail::to($request->email)
->cc('my@*******')
->queue(
new Mail\MyEmail($data, $course)
);
return redirect()->back()->with(['success' => 'Success!']);
endif;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PDF还需要一个视图,这样的视图:
在此视图看起来像这样:
因此,我建议先创建PDF,然后将其附加到电子邮件中,
请记住这对我们自己的用例非常具体,您将需要修改此。
The PDF also requires a view, like so:
Where the view looks like this:
So i recommend first creating the PDF, and only then attaching it to the email
Keep in mind this is very specific to our own use case and you will need to modify this.
如果您使用 barryvdh/larryvdh/laravel-dompdf 软件包,您可以简单地使用 loadview()函数,它会
.blade> .blade
file and you想要的数据。在该刀片内部,您将生成PDF布局并注入变量。简单的示例就是这样:您现在可以在pdf .blade文件中使用此
$ name
变量:在使用所需的布局和数据生成PDF后,您可以将其附加到邮件类上:
If you are using barryvdh/laravel-dompdf package, you can simply use loadView() function, that takes your
.blade
file and the data you want. Inside that blade, you will generate PDF layout and inject your variables. Simple example would be something like this:You can now use this
$name
variable in your pdf .blade file:After you generate the pdf with desired layout and data, you can attach it to Mail class like this: