<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

class MailController extends Controller
{
    public function index()
    {
        return view('mail_form');
    }

    public function send(Request $request)
    {
        $request->validate([
            'email'   => 'required|email',
            'subject' => 'required|string|max:255',
            'body'    => 'required|string',
        ]);

        $to = $request->email;
        $subject = $request->subject;
        $body = $request->body;

        // ✅ Laravel 10 correct mail sending
        Mail::send([], [], function ($message) use ($to, $subject, $body) {
            $message->to($to)
                    ->subject($subject)
                    ->html($body); // 🔥 FIX
        });

        return back()->with('success', 'Email sent successfully!');
    }
}