Cannot show data from controller to email blade - php

I want to show $data form controller to view but it shows this error message. Do you know why and how to fix that? Thank you.
Here's my code:
UserController.php
public function forgotPassword(Request $request){
$data['user']['email'] = $request->input('user.email');
Mail::send('vendor.notifications.resetpassword', $data, function($message) use($data){
$message->from('bemsadmin#gmail.com');
$message->to($data ['user']['email']);
$message->subject('Your Email');
});
return response()->json($data);
}
resetpassword.blade.php
<!DOCTYPE html>
<html>
<body>
<p>{{$data['user']['email']}}</p>
</body>
</html>

the error happens beacuse you did not pass a variable called $data to the email view,
you pass a variable like this
Mail::send('vendor.notifications.resetpassword', ['data' => $data], function($message) use($data){
but with your code what get passed is
['user' => ['email' => 'email#examle.com']]
so try in your view to use the following code, it should do the job.
{{$user['email']}}

Related

Display JSON Data store in JSON in blade (Laravel 8)

I have produce notifications through email and manage to send the email.
Now I want to change the template provided by Laravel so I create a new customize blade that will be displayed.
I have a problem on transferring the data to the blade view.
Below is my code:
public function __construct($offerData)
{
$this->offerData = $offerData;
}
public function toMail($notifiable)
{
/*This is the original code with default template */
// return (new MailMessage)
// ->greeting($this->offerData['name'])
// ->line($this->offerData['body'])
// ->action($this->offerData['offerText'], $this->offerData['offerUrl'])
// ->line($this->offerData['thanks']);
/*This is the code use to display the customize template*/
return (new MailMessage)->view(
'email_notification',
['data' => $this->offerData]
);
Below is How I display the code:
<!DOCTYPE html>
<html>
<head>
<title>Hi Awak</title>
</head>
<body>
#foreach($data as $data_)
<p>{{ $data_ }}</p>
#endforeach
</body>
</html>
This is the result:
I cannot do any specific modification for each data as you can see it only repeat the p tag here.
Thank you for help....
I manage to found the solution...
I just store array in array...
public function toMail($notifiable)
{
return (new MailMessage)->view(
'email_notification',
['name' => [$this->offerData['name']],
'body' => [$this->offerData['body']]
]
);
}

Use variable inside another variable in a mail blade view

I'm using the Mail library in Laravel to send html email with custom data passed to a blade view.
The problem born when the mail has to render the html fetched from a row in the database which include a variable that i pass through the view.
This is my build function in my mailable class
public function build()
{
return $this->from('hello#test.it')
->view('view')
->with([
'url' => 'https://google.com',
'text' => $this->parameters->text,
]);
}
Then in the blade view:
<div>
{!! $text !!}
</div>
This is what the $text variable looks like:
<p>
<span>This is my text for the mail</span>
Click here to compile
</p>
The link href shoul contain the url variable value instead of not passing the variable name itself
A simple solution would be formating with php:
public function build()
{
return $this->from('hello#test.it')
->view('view')
->with([
'text' => str_replace('{{ $url }}','https://google.com',$this->parameters->text)
]);
}
I did not try by myself but you could make an attempt with Blade::compileString(), i.e.:
public function build()
{
return $this->from('hello#test.it')
->view('view')
->with([
'url' => 'https://google.com',
'text' => \Blade::compileString($this->parameters->text),
]);
}

how to pass dynamic content(content which is coming from database) to send mail in laravel 5.4

I m new to laravel.i have table email_template and want to send mail to user when user forgot password.i m fetching content dynamically from database but i dont know how to pass it to mail function in laravel.
Mail::send($posts['email_template'], ['USER' =>$post['user] ], function($message)
{
$message->from('test#gmail.com')->subject('Welcome to laravel');
$message->to('test8#gmail.com');
});
where $posts['email_template'] is a content which i want to send and user is a variable which i want to replace in content
Mail::send('emails.template', ['user' => $user, 'data' => $data], function ($message) use ($user, $data) {
$message->from('test#gmail.com', 'Your Application');
$message->to('test8#gmail.com', $user->name)->subject('Welcome to laravel');
});
emails.template is your view - template.blade.php file - /resources/views/emails/template.blade.php
Now, in your view i.e emails.template, you can do:
{{ $user->name }}, {{ $data->address }}
You can Define
ADMIN_EMAIL and CC_EMAIL in the constant file in the config folder
$emailData = array(
'name'=>'toName',
'toEmail'=>$request->email
);
$this->sendEmail($emailData);
Email Function
function sendEmail($emailData){
$this->adminEmail = config('constant.ADMIN_EMAIL');
$this->ccEmail = config('constant.CC_EMAIL');
$this->toEmail = $emailData['toEmail'];
$this->emailTemplate = $emailData['emailTemplate'];
$data['emailInfo'] = array(
'name'=>$emailData['name']
);
Mail::send('emails.yourTemplate', $data, function ($message) {
//$message->attach($pathToFile);
$message->from($this->adminEmail, 'Laravel Email Test');
$message->to($this->toEmail)->cc($this->ccEmail);
});
}

Laravel: Undefined variable content

I have setup a Route:
Route::resource('conferences', 'ConferencesController)
Artisan therefore shows me a route:
POST conferences | conferences.store | ConferencesController#store
When I submit a Form from the create View, I get the error that a variable in my layout file has not been defined.
Undefined variable: content is shown, nothing has been posted.
I opened my form like this:
{{ Form::open(array('url' => '/conferences', 'class' => 'conference-form')) }}
And finally, my store method in ConferencesController looks like this:
public function store()
{
$validator = Validator::make(Input::all(), Conference::$rules);
if($validator->passes()){
$conference = new Conference();
$conference->title = Input::get('title');
$conference->description = Input::get('description');
$conference->location = Input::get('location');
$conference->plannedTime = Input::get('plannedTime');
$conference->save();
Mail::pretend();
Mail::send('emails.conference.create', ['title' => Input::get('title'), 'location' => Input::get('location'), 'plannedTime' => Input::get('plannedTime')], function($message){
$message->to('email')->subject('Een nieuw evenement is gemaakt.');
});
Redirect::to('/conferences')->with('message', 'Nieuw event is aangemaakt!');
} else {
Redirect::to('/')->with('message', 'Iets ging mis');
}
}
How do I fix this error?
** EDIT: Added create method **
public function create(){
$this->layout->content = View::make('conferences.create');
}
This should be really straight forward. In your views directory we usually have a folder called layouts where we put the page structure something like:
// default.blade.php
<html>
<head> </head>
<body>
#yield('content'); // this is where your views will be loaded
</body>
</html>
then in your case you should create a conferences folder and then a file create.blade.php in it.
#extends('layouts.default')
#section('content')
// your forms etc
#stop
And in your create method inside ConferencesController
public function create() {
return View::make('conferences.create');
}
And one last thing, when you try to send the email you should be passing an email address inside the to() function and you are passing a string

Laravel Mail::send how to pass data to mail View

How can i pass data from my Controller to my customized mail View ?
Here's my controller's send mail method :
$data = array($user->pidm, $user->password);
Mail::send('emails.auth.registration', $data , function($message){
$message->to(Input::get('Email'), 'itsFromMe')
->subject('thisIsMySucject');
Here's my emails.auth.registration View
<p>You can login into our system by using login code and password :</p>
<p><b>Your Login Code :</b></p> <!-- I want to put $data value here !-->
<p><b>Your Password :</b></p> <!--I want to put $password value here !-->
<p><b>Click here to login :</b> www.mydomain.com/login</p>
Thanks in advance.
Send data like this.
$data = [
'data' => $user->pidm,
'password' => $user->password
];
You can access it directly as $data and $password in email blade
$data = [
'data' => $user->pidm,
'password' => $user->password
];
second argument of send method passes array $data to view page
Mail::send('emails.auth.registration',["data1"=>$data] , function($message)
Now, in your view page use can use $data as
User name : {{ $data1["data"] }}
password : {{ $data1["password"] }}
for those using the simpleMail this might help :
$message = (new MailMessage)
->subject(Lang::getFromJson('Verify Email Address'))
->line(Lang::getFromJson('Please click the button below to verify your email address.'))
->action(Lang::getFromJson('Verify Email Address'), $verificationUrl)
->line(Lang::getFromJson('If you did not create an account, no further action is required.'));
$message->viewData['data'] = $data;
return $message;
The callback argument can be used to further configure the mail. Checkout the following example:
Mail::send('emails.dept_manager_strategic-objectives', ['email' => $email], function ($m) use ($user) {
$m->from('info#primapluse.com', 'BusinessPluse');
$m->to($user, 'admin')->subject('Your Reminder!');
});

Categories