#How to correctly send Mail with attachment?

17 messages · Page 1 of 1 (latest)

sharp heron
#

I have the Route
Route::get('/purchase/send/{id}', 'PurchaseSend')->name('purchase.send');
and my PurchaseSend in my PurchaseController.php


        $purchase = Purchase::with('customer')->where('id', $id)->first();
        Mail::to($customer->email)->send(new SendPdf($id));
    }```
I've imported SendPdf
```namespace App\Http\Controllers\Pos;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Purchase;
use App\Models\Supplier;
use App\Models\Product;
use App\Models\Unit;
use App\Models\Category;
use App\Models\Customer;
use Auth;
use Illuminate\Support\Carbon;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\PurchaseExport;
use App\Models\Payment;
use App\Models\PaymentDetail;
//use App\Imports\Purchase;
use PDF;
use App\Mail\SendPdf;```
SendPdf.php here:
```<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class SendPdf extends Mailable
{
    use Queueable, SerializesModels;

    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

    public function build()
    {
        $path = public_path("purchases/purchase_{$this->id}.pdf");
        return $this->view('emails.pdf')->attach($path, ['as' => "purchase_{$this->id}.pdf"]);
    }
}```
But it just gives me the Error:
```Class "App\Http\Controllers\Pos\Mail" not found

Am i missing something?

hearty basin
#

Mail is a facade in laravel, so you just either need to import it with a use statement at the top, or use the root namespace by changing it to \Mail

#

I would suggest importing the facade cos it's more strict

#

use Illuminate\Support\Facades\Mail; at the top with the use statements.

#

Otherwise it thinks it's a class that exists within the same namespace as your controller

sharp heron
sick basin
#

Hey guys , just wanted to ask a question regarding this thread , I have the same setup (Mail Facade) with a controller(where my pdf gets generated)

Now inside the Mail facade/class there's a method called attachments and I learnt how to send out my mail with an attachment which is stored in my disk.

But in my situation right now , I actually make a pdf then desire to send it out immediately.but I was hoping to not have to save it first before emailing it out

Is this possible,or do i have to save?

I've been trying to find some sources to help with the logic but no help,please advice

glass glen
sick basin
glass glen
#

Can you ping me on code ?

sick basin
#

I @'d you in general

glass glen
#

Temporary storing ?

#

And that’s not the proper way but what if you generateyour file directly in the mail constructor ?

sick basin
#

i could , i would just need to pass the consumer ID and route my button to its controller too?

glass glen
#

Yup

sick basin
#

correction its not a controller but the mail facade/class...doesnt that change thins?

glass glen
#

The controller calls your mail class