phplaravellaravel-5sparkpost

Laravel - Mailables Dynamic Subject (using SparkPost)


I have a mailable that I'm not having any issues with really except for dynamically setting the subject. For the life of me, I cannot change the subject line to anything but programmed text (example - Your Account is Ready) but I'd like to be able to say "Your Account is Ready {{$user->name}}".

However, when I do this, I get the following error returned:

"message": "Client error: `POST https://api.sparkpost.com/api/v1/transmissions` resulted in a `422 Unprocessable Entity` response:\n{ \"errors\": [ { \"message\": \"substitution language syntax error in template content\", \"description\": \"Error while compili (truncated...)\n",
    "exception": "GuzzleHttp\\Exception\\ClientException",
    "file": "C:\\xampp\\htdocs\\truckin\\vendor\\guzzlehttp\\guzzle\\src\\Exception\\RequestException.php",
    "line": 113,
    "trace": [

I can pass variables like {{$user->name}} in the body of the email, but I cannot pass any variable to my mailable subject.

At the moment, the mailable has this template:

<?php

namespace App\Mail;

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

use App\Shipment;

class newFreightBill extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public $shipment;

    public function __construct(Shipment $shipment)
    {
        $this->shipment = $shipment;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from('myEmail@email.com')
                    ->subject('New Account Created')
                    ->view('emails.account.created');
    }
}

I'm using Laravel 5.4.


Solution

  • public function build()
        {
            $data['name']     = 'pass your user name here';
            return $this->from('myEmail@email.com')
                        ->subject('New Account Created')
                        ->view('emails.account.created',$data);
        }
    
    1. In Your View page(emails.account.created) you can call like this "Your Account is Ready {{$user->name}}"