Chris
Chris

Reputation: 257

How can I send an email with a PDF with Laravel

I have a PDF coded in base 64, it is an attribute in my database, and I send emails through Laravel but I do not know how can I send the base64 as a PDF.

public function toMail()
    {
      $pdf_decoded = base64_decode($this->table->raw_label);
      
      $message = (new MailMessage)
                  ->subject(env('APP_NAME').' - HI #'. $this->order->id)
                  ->greeting('¡Hi!');
      
      if(env('APP_URL') == 'http://test.test'){
        $message->bcc(['[email protected]']);
      }

      return $message;
    }

I know attach property, but I do not know hot to implement it.

Upvotes: 1

Views: 4455

Answers (1)

Chris Kelker
Chris Kelker

Reputation: 571

You can actually do this through the Mail or Notification class, personally I would use a Notification but it's up to you. Just use the ->attach($pathToFile) method and give it the file path as a parameter.

Here's an example using a Notification, hope this helps!

/**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->line('Please download the PDF.')
                    ->attach(public_path($this->filename), [
                        'as' => 'filename.pdf',
                        'mime' => 'text/pdf',
                    ]);
    }

Upvotes: 8

Related Questions