drpfu51608120170 2019-08-13 12:55
浏览 130

Laravel如何将生成的PDF附加到电子邮件

I'm working on a laravel-application where users can fill out and submit a form with some user-data. When the user has submitted the form I receive an email with a confirmation that a user has send me the form with some data. Now, in addition to that I have added the feature that a pdf is generated too with the user-data and I want to attach it to the email. So far it works ok, but my issue is that right now I'm getting two emails. One with the confirmation and one with the generated PDF.

This is what my controller looks like:

$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););

The NewApplication-methods build funtion looks like this:

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
        ]);
}

So, how can I combine these two mail methods so that I only receive one?

UPDATE

OK, so I tried to do this in my 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;
}

But this fails and gives me the error basename() expects parameter 1 to be string, object given - any other suggestions?

  • 写回答

2条回答 默认 最新

  • drw85135 2019-08-13 13:03
    关注

    The Laravel way to attach a document (be it pdf or anything) as per the documentation is in the build method of your Mail class

    ->attach($pdf->output(), [
          'as' => "application_" . $application["name"] . ".pdf",
          'mime' => 'application/pdf',
      ]);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看