php - Laravel How to attach generated PDF to Email


I'm working on alaravel-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 apdf 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('[email protected]', $application["name"])
    ->subject('New Applicant - ' . $application["name"])
    ->attachData($pdf->output(), "application_" . $application["name"] . ".pdf");

Mail::to('[email protected]')->send(new NewApplication($application->fresh()));

return response()->json('OK', 200););

TheNewApplication-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}")
            '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?


OK, so I tried to do this in myNewApplication.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}")
        ->attach($this->pdf, [
            'as' => 'applicant.pdf', 
            'mime' => 'application/pdf',

    return $build;

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




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',



Try This Example

    $this->validate($request, [
            'name' => 'required',
            'phone' => 'required',
            'email' => 'required|email',
            'message' => 'required|min:20',
            'checkbox' => 'required']);

    $data = array(
            'name' => $request->name,
            'phone' => $request->phone,
            'email' => $request ->email,
            'checkbox' => $request ->checkbox,
            'bodyMessage' => $request->message

     //code to send email to my inbox
    Mail::send('', $data, function($message) use ($data){
        $message->to('[email protected]************');

    //Feedback mail to client
    $pdf = PDF::loadView('your_view_name', $data)->setPaper('a4'); 
    Mail::send('', $data, function($message) use ($data,$pdf){
            $message->from('[email protected]**********');
            $message->subject('Thank you message');
            //Attach PDF doc

    Session::flash('success', 'Hello  '.$data['name'].' Thank You for choosing us. Will reply to your query as soon as possible');

    return redirect()->back();


People are also looking for solutions to the problem: php - TYPO3 QueryRepository returns empty and can not use it as condition


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.