Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MahmoudMonem's avatar

Problem sending Email to user when purchase a product

I have three tables users | products | product_user

What I have is a call back from payment service provider .. in this call back , I attach the order user to the product

Now everything is working fine, I attach the user to product and also I update Order status .. What I'm trying to do is to send an Email to the user with the details of his purchase or at least just an email that he purchased a new product and he can check it out through the link I give in the mail. something like [website/myproducts]

so In my trial to do that, I tried the following :

I created a NewPurchaseMail.php

 public function build()
    {
        return $this->markdown('emails.new_purchase')
        ->subject('You have purchased new Course 🥳');
    }
}

With a markdown called new_purchase.blade.php

@component('mail::message')

# Hi! You have a new purchase 

@component('mail::button', ['url' => 'https://website/myproducts'])
Checkout your Products
@endcomponent

Team {{ config('app.name') }}

@endcomponent

and in my callback function I tried to do so

if($order = \App\Order::find($orderId)) {
		
        // Update order status
        $order->update([
            'status' => $status
        ]);

        // Get order items
        $items = $order->items()->get()->pluck('item_id')->toArray();

        // Insert user items to product_user table
        $order->user->products()->attach($items);

        Mail::to($order->user->email)->send(new NewPurchaseMail($order));
    } // I am sure this is wrong 

Would appreciate your inputs

0 likes
1 reply
MahmoudMonem's avatar
MahmoudMonem
OP
Best Answer
Level 2

Solved by changing this line

Mail::to($order->user->email)->send(new NewPurchaseMail($order));

and I also forgot to add the facade

use App\Mail\NewPurchaseMail;
use Illuminate\Support\Facades\Mail;

Please or to participate in this conversation.