Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

laravel 9.x passing controller variable to another file

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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@example.com";

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

>Solution :

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@example.com";

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

I hope it’s useful

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading