简体   繁体   中英

How to define email subject in email template

I have email blade like this:

<?php
$title = 'title';
$text   = 'text';
?>

@include('emails.layouts.general')

In emails.layouts.general

<h1>{{$title}}</h1>
<p>{{$text}}</p>

I send email like :

$subject = 'some subject'

\Illuminate\Support\Facades\Mail::send($template, [],
    function ($m) {
    $m->to('test@test.com', 'test test')->subject($subject);
});

Does anybody have idea how I could define subject in blade :

<?php
$subject = 'subject';
$title   = 'title';
$text    = 'text';
?>

and set this subject when send email ?

EDITED:

I need something like: Set email subject in email blade:

<?php
$title   = 'title';
$text    = 'text';
$subject = 'subject';
Mail::setEmailSubjectForNextEmail($subject);
?>

@include('emails.layouts.general

or in controller :

$subject = somehowGetSubjectVariableFromTemplate($template);

\Illuminate\Support\Facades\Mail::send($template, [],
    function ($m) use ($subject) {
    $m->to('test@test.com', 'test test')->subject($subject);
});

I want set subject variable in blade template, not in controller, because I have more than 50 emails and I want front developers only work(edit) with blade templates not with controllers.

Try use:

@php 
$message->setSubject('New subject');
@endphp

You need to pass the $subject into the callback and into the template. The subject method sets the mail subject line it doesn't pass it into the email template

$subject = 'Your Subject';

Mail::send($template, ['subject'=>$subject,'other_thing'=>'what you like'], //this will pass it into the blade template
   function ($m) use($subject){ //you need to pass into the use clause to make the variable available in the scope of the callback
      $m->to('test@test.com', 'test test')->subject($subject); 
   }
);

You dont need to assign values to your variables at the top of the template either they will be available as long as you put them into the array in the second argument.

blade template:

<div>{{$subject}}</div> <!-- "Your Subject" -->
<span>{{$other_thing}}</span><!-- "what you like" -->

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM