Support the ongoing development of Laravel.io →
posted 7 years ago
Mail

In the Laravel documentation for using the Mailable class (https://laravel.com/docs/5.4/mail#sending-mail), it says under the heading "Inline Attachments", "Laravel automatically makes the $message variable available to all of your email templates, so you don't need to worry about passing it in manually". However, I am not able to access the $message variable in my email template that is called from the build method of the Mailable class using the view function. When I do a dd($message) in the email template, the only variable available to the $message variable is the $message->embeddedFiles variable. The other variables like $message->to, $message->from, $message->subject are not available.

Has anyone been able to get this to work?

Here is my code for the subclass that extends the Mailable class:

<!-- Contact.php -->

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Http\Request;

class Contact extends Mailable
{
    use Queueable, SerializesModels;

    private $message;

    public $request;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('emails.contact')
        ->to('dbrower256@cableone.net')
        ->subject('message from texarkanasoftware.com website - ' . $this->request->subject);
    }
}

If anyone figures this out, please help me.

Last updated 2 years ago.
0

Warning: $message could conflict with a reserved variable name, what's the purpose of your "$message" variable?

if you need to pass data you better use with() method (https://laravel.com/api/5.4/Illuminate/Mail/Mailable.html#method_with)

Anyway make it public and it will be available in the scope of building message (https://laravel.com/docs/5.4/mail#view-data)

ie:

public $message;

but you still need to populate the variable... you can do it in the construct

ie:

public function __construct(Request $request, String $message) { $this->request = $request; $this->message = $message; }

Last updated 7 years ago.
0

replace your $massage variable by $msg and it will work.Pass this variable to your construct as a parametere and then define a public attribute like so public $msg

krishnaguragain liked this reply

1

Sign in to participate in this thread!

Eventy

Your banner here too?

Daniel Brower Daniel Brower dbrower256 Joined 22 Jun 2017

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.