snowfire / Beautymail

Send beautiful HTML emails with Laravel
1.24k stars 207 forks source link

Passing data to email template #137

Closed msalehipro closed 2 years ago

msalehipro commented 3 years ago

How can i pass data from controller to my email view ? if it's possible, show me some sample code.

aeadedoyin commented 3 years ago

Hi @ghambale

Just call the element of the data directly i.e.

$mailData = [
  'user' => '', // some data,
  'token' => ''
]

$beautymail = app()->make(Beautymail::class);
$beautymail->send('emails.welcome', $mailData, function ($message) {
  $message
  ->from('yourmail@yoursite.com') // or leaving it blank makes use of MAIL_FROM_ADDRESS and MAIL_FROM_NAME in env
  ->to($mailData->user->email, $mailData->user->fullName())
  ->subject('Welcome!');
});

Then in the mail blade file.

<p>Hi, {{$user->first_name}}<p>

See how I used the user property directly instead of $mailData->user->first_name // This will not work