Home > Enterprise >  laravel 9.x passing controller variable to another file
laravel 9.x passing controller variable to another file

Time:04-09

I'm trying to pass form values on my PagesController.php to HelloMail.php After passing variables it will send mail. I'm new to laravel tried to make some research about it and read the older questions but none of them contains proper answer for my question.

Problem: Undefined variable on HelloEmail.php => $emailSender - $emailSubject - $emailBody

Do I need to change my route for this? or is it makeable on controllers?

web.php

Route::post('/message',[App\Http\Controllers\PagesController::class,'getData'])->name('message');
Route::view('message','message');

Route::get('/send-email', [App\Http\Controllers\EmailController::class, 'sendEmail']);

PagesController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PagesController extends Controller
{
    public function getData(Request $Req)
    {
        $emailSender = $Req->input('emailSender');
        $emailSubject = $Req->input('emailSubject');
        $emailBody = $Req->input('emailBody');
       
    }
}

HelloEmail.php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use app\Http\Controllers\PagesController;

class HelloEmail extends Mailable
{
    use Queueable, SerializesModels;
    public function __construct()
    {
        //
    }
    public function build()
    {
        return $this->from($emailSender)
                    ->body('$emailSubject')
                    ->subject('$emailBody');
        }
}

EmailController.php

namespace App\Http\Controllers;

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

class EmailController extends Controller
{
    public function sendEmail()
    {
        $toAddress= "[email protected]";

        Mail::to($toAddress)->send(new HelloEmail);
        dd("Email is sent successfully.");
    }
}

CodePudding user response:

not sure what you looking for from sending emails from separate requests, but you can pass variables to HelloEmail class from the constructor

Example

HelloEmail

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use app\Http\Controllers\PagesController;

class HelloEmail extends Mailable
{
    use Queueable, SerializesModels;
    
    private $emailSender;
    private $emailSubject;
    private $emailBody;
    
    
    public function __construct($emailSender, $emailSubject, $emailBody)
    {
        $this->emailSender = $emailSender;
        $this->emailSubject = $emailSubject;
        $this->emailBody = $emailBody;
    }
    public function build()
    {
        return $this->from($this->emailSender)
            ->body($this->emailBody)
            ->subject($this->emailSubject);
    }
}

and in EmailController

namespace App\Http\Controllers;

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

class EmailController extends Controller
{
    public function sendEmail()
    {
        $toAddress= "[email protected]";

        Mail::to($toAddress)->send(new HelloEmail('[email protected]', 'email subject !', 'the body'));
        dd("Email is sent successfully.");
    }
}

I hope it's useful

  • Related