【发布时间】:2019-08-13 12:55:07
【问题描述】:
我正在开发一个laravel-应用程序,用户可以在其中填写并提交包含一些用户数据的表单。当用户提交表单时,我会收到一封电子邮件,确认用户已向我发送了包含一些数据的表单。
现在,除此之外,我还添加了使用用户数据生成pdf 的功能,我想将其附加到电子邮件中。到目前为止它工作正常,但我的问题是现在我收到两封电子邮件。一份带有确认信息,一份带有生成的 PDF。
这是我的控制器的样子:
$application = Applicant::create([
'email' => request()->email,
'name' => request()->name,
'avatar' => request()->avatar,
'phone' => request()->phone,
'address' => request()->address,
'zipcode' => request()->zipcode,
'city' => request()->city,
]);
$pdf = PDF::loadView('pdf.application', $application);
Mail::send('pdf.application', $pdf_data, function ($message) use ($pdf_data, $pdf) {
$message->to('my@mail.com', $application["name"])
->subject('New Applicant - ' . $application["name"])
->attachData($pdf->output(), "application_" . $application["name"] . ".pdf");
});
Mail::to('my@mail.com')->send(new NewApplication($application->fresh()));
return response()->json('OK', 200););
NewApplication-methods 构建函数如下所示:
public function build()
{
$build = $this->replyTo($this->application->email, $this->application->name)
->subject("New Application in Database: {$this->application->name}")
->view('emails.application')
->with([
'name' => $this->application->name,
'address' => $this->application->address,
'email' => $this->application->email,
'phone' => $this->application->phone
]);
}
那么,我怎样才能将这两种邮件方式结合起来,让我只收到一个呢?
更新
好的,所以我尝试在我的 NewApplication.php 中执行此操作
public function __construct(SingleApplication $application)
{
$this->application = $application;
$this->pdf = PDF::loadView('pdf.application', $this->application);
}
public function build()
{
$build = $this->replyTo($this->application->email, $this->application->name)
->subject("New application in database: {$this->application->name}")
->view('emails.application')
->attach($this->pdf, [
'as' => 'applicant.pdf',
'mime' => 'application/pdf',
]);
return $build;
}
但这失败并给了我错误basename() expects parameter 1 to be string, object given - 还有其他建议吗?
【问题讨论】:
标签: php laravel dompdf laravel-mail