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?