nbbfv's avatar
Level 2

Can not assign varialbes in Mailable Laravel 5.3

Hi, I have this Mailable class,

<?php

namespace MyCompany\Common\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use MyCompany\Common\Models\Player;
use MyCompany\Common\Models\EmailValidationToken;

class ValidateEmail extends Mailable
{
    use Queueable, SerializesModels;

    public $player;

    public $email_validation_token;

    public $subject = 'Email validation request';

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct(Player $player)
    {
        $this->player = $player;
        $player->is_email_verified = false;
        $player->save();
        $this->email_validation_token = EmailValidationToken::create(['email' => $player->mail, 'player_id' => $player->id]);
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->subject($this->subject)
            ->view('mail.validate-email');
    }
}

When I try to use it I always get this error message:

local.ERROR: ErrorException: Undefined variable: email_validation_token in /var/www/html/storage/framework/views/cb5b68b17c989fffdb669af6e4fbfbec9344748d.php:12

The view is very simple:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Validate your email address please</title>
</head>
<body>
    <h1>Validate your email address please</h1>
    <p>Dear {{ $player->name }},
    <br>
    Validate your email address please.<br/>
        Your token is: <b>{{ $email_validation_token->token }}</b>.
        <br/><br/>

     MyCompany Support
    </p>
</body>
</html>

This is how I call it:

public function sendEmailVerification(Request $request, Player $player)
    {
        Mail::to($player->mail)
            ->send(new ValidateEmail($player));
        return response()->json(['success' => true]);
    }
0 likes
7 replies
shakti's avatar

i think in view you are not passing anything thats why you are getting this problem

  public function build()
    {
        return $this->subject($this->subject)
            ->view('mail.validate-email',array('email_validation_token->token'=>$this->email_validation_token));
    }

Please check this it might help you

nbbfv's avatar
Level 2

By default all the public properties are available and $player is okay, I can access it. I tried it with ->with() and passing it as you've suggested as view parameter array, still get same error.

From the documentation:

Via Public Properties

Typically, you will want to pass some data to your view that you can utilize when rendering the email's HTML. There are two ways you may make data available to your view. First, any public property defined on your mailable class will automatically be made available to the view. So, for example, you may pass data into your mailable class' constructor and set that data to public properties defined on the class:
shakti's avatar

can you please tell me what EmailValidationToken::create returns??

nbbfv's avatar
Level 2

@shakti, EmailValidationToken::create() returns an object which represents one single row from the table. Tested it with tinker and works.

nbbfv's avatar
nbbfv
OP
Best Answer
Level 2

@shakti Thank you for your efforts. Finally I've solved the problem. Renamed ValidateEmail class to something other and built email sending process from scratch. Looks like I'm fine now, thank you for your support.

Please or to participate in this conversation.