Laravel 12 E-Commerce Project Tutorial

Sending Order Confirmation Email upon Checkout

Hi everyone, welcome back to the Laravel E-Commerce Tutorial. This video guides you through setting up and sending an Order Confirmation Email to the user immediately after an order is successfully placed.

  1. Create the Mailable Class

    Use the Artisan command to generate the `Mailable` class for the order confirmation email:

    php artisan make:mail OrderConfirmationMail
  2. Define the `OrderConfirmationMail` Class

    Open `app/Mail/OrderConfirmationMail.php` and define the constructor to accept the `$order` model, and configure the `envelope` and `content` methods. The subject will be dynamic, and the view will be set to `emails.order_confirmation`:

    use AppModelsOrder; // Assuming the Order model is imported
    class OrderConfirmationMail extends Mailable
    {
    use Queueable, SerializesModels;
    protected $order;
    public function __construct(Order $order)
    {
    $this->order = $order;
    }
    public function envelope(): Envelope
    {
    return new Envelope(
    subject: "Order Confirmation - Order #" . $this->order->id,
    );
    }
    public function content(): Content
    {
    return new Content(
    view: "emails.order_confirmation",
    with: [
    "order" => $this->order,
    ],
    );
    }
    }

  3. Create the Email View (`emails/order_confirmation.blade.php`)

    Create the view file where the email content will be rendered. This template will use the $order variable passed from the Mailable class to display order details, such as items purchased, totals, and shipping address. (The content snippet shows a basic structure, including the order ID and a link to view the order).

  4. Update the `place_order` Function

    In `CartController.php`, import the Mailable class and add the mail sending logic within the `place_order` function, right before redirection. This step also includes cart cleanup after the order is finalized:

    use AppMailOrderConfirmationMail;
    use IlluminateSupportFacadesMail; // Ensure Mail Facade is imported
    use GloudemansShoppingcartFacadesCart; // Ensure Cart Facade is imported
    use IlluminateSupportFacadesSession; // Ensure Session Facade is imported
    // ... inside the place_order function after order creation and transaction save ...
    // Send the confirmation email
    Mail::to($order->email)->send(new OrderConfirmationMail($order)); // Using $order->email or Auth::user()->email
    // Note: The source snippet uses a hardcoded email, which should be replaced with the user'semail dynamically.
    // Mail::to(users: "sudhirmo.co@gmail.com")->send(mailable: new OrderConfirmationMail(order: $order)); // Original source example
    // Clear Cart and Session data
    Cart::instance("cart")->destroy();
    Session::forget(keys: "checkout");
    Session::forget(keys: "coupon");
    Session::forget(keys: "discounts");
    Session::put(key: "order_id", value: $order->id);
    // Redirect to the confirmation page
    return redirect()->route(route: "cart.order.confirmation");
  5. Configure SMTP Settings in `.env`

    Update your `.env` file with the necessary SMTP configuration for sending emails. You will need to replace the placeholder values with your actual mail server credentials (e.g., for Gmail, use an App Password):

    MAIL_MAILER=smtp
    MAIL_SCHEME=null
    MAIL_HOST=smtp.gmail.com
    MAIL_PORT=587
    MAIL_USERNAME=your_email@gmail.com
    MAIL_PASSWORD=your_app_password
    MAIL_FROM_ADDRESS=your_email@gmail.com
    MAIL_FROM_NAME="${APP_NAME}"

Verification and Testing 📧

1. Ensure your `.env` file is properly configured with valid credentials.
2. Place a new order on the frontend.
3. After successful order completion, check the email address associated with the order (or the hardcoded test email) for the new Order Confirmation Email.