Get recipient name on view in laravel mail - laravel-5

I have a set of recipients. I am able to send mail to all of them. But how to get their name on the view. To be specific how to get $user value in my view(emails.test).
Mail::send('emails.test', ['data' => $data], function ($message) use ($data) {
foreach($data['users'] as $user) {
$message->to($user->email, $name = $user->firstName . ' ' . $user->lastName);
}
$message->subject('test');
});
Is there any way to access $user value in my view? I can access $data in my view. $data['users'] is an array of users. I need particular/current User's name in the view.
My view(emails.test)
<div>Dear {{$user->firstName}},</div>
How are you?....
But user is undefined here.
Thanks in advance.
Debabrata

from the docs
The send method accepts three arguments. First, the name of a view
that contains the e-mail message. Secondly, an array of data you wish
to pass to the view. Lastly, a Closure callback which receives a
message instance, allowing you to customize the recipients, subject,
and other aspects of the mail message
as you can see the second arguments its the data you send to the view
so in your view you can use the $data array just like you did inside the closure:
#foreach($data['users'] as $user) {
{{$user->username}}
}

Related

passing query data to send in mail laravel

i have a query to return all the the data in visits table
$visits = Visit::get();
i want to pass the returned data in $data so that i can send it on mail and display the data in the email body
$data = array();
Mail::send('mails.mail', $data, function ($message) use ($host_email, $to_name) {
$message->from('', '');
$message->to($host_email);
$message->subject('Visitor arrived');
});
how do i do that
You can use Laravel structure, You can create a mail class with this command:
php artisan make:mail OrderShipped
it will generate a class in App\Mail path sample named OrderShipped in build method you can call a blade view for this mail and send data to it like:
public function build()
{
return $this->view('emails.orders.shipped')
->with([
'orderName' => $this->order->name,
'orderPrice' => $this->order->price,
]);
}
and finally use this to send email to user:
Mail::to($request->user())->send(new OrderShipped($order));
look here for full documentation:
laravel mail

Send Email to admin when user register?

I tried to to send email to admin when user register to my application
I tried these code :
$admin = Admin::where('is_admin', 3)->get();
Mail::send('emails.notfyNewUser', $data , function ($message) use($admin) {
$message->from('test#test.org','test');
$message->to($admin->email);
$message->subject('test');
});
And got this error :
Property [email] does not exist on this collection instance.
And this is my collection result via dd for $admin :
Image
The problem is that you have 2 admins inside your collection, and the property admin is within each of those models.
You can solve it by changing your query, for something like this:
$admin = Admin::where('is_admin', 3)->first();
Or, if you want to send an email for both admins, use a foreach loop:
foreach($admin as $a) {
Mail::send('emails.notfyNewUser', $data , function ($message) use($a) {
$message->from('test#test.org','test');
$message->to($a->email);
$message->subject('test');
});
}
Hope it helps.

Sending email notification to existing user Laravel 5.8

I am trying to send email to all existing admins once a user sends a request. I am using a Notifiable class to send emails but I am getting an error of :-
Call to a member function notify() on string
The code for me to notify all admins:-
$all_admins = User::whereHas('roles', function ($query) {
$query->where('admin', '1');
})->paginate(100);
foreach($all_admins as $admin){
if ($type == 1){
$subject = $username.' made a lawyer request.';
$message = "User: ". $user;
$message2 = "Please handle the request in the admin portal under the request tab.";
}
$user = $admin->email;
//$admin->email has a string of an email
$user->notify(new EmailAdmin($subject, $message, $message2)); <--
***The above code is the code that is marked when laravel is returning the error to me.
}
You wrote it yourself. $admin->email is a string. The error says you're calling a function on a string. You're doing it wrong.
The documentation says you're supposed to call notify() on a User model that has the trait.
(answered in the comments by #IGP)

Laravel 5.5 - Send Email - Can't Get Property On Non-Object

I am trying to send email from a Laravel 5.5 controller like this...
$user = User::find(1)->toArray();
Mail::send('emails.invite', $user, function($message) use ($user) {
$message->to($user->email);
$message->from('me#example.com');
$message->subject('Test Subject');
});
This fails with the error...
"message": "Trying to get property of non-object",
If I echo the array into the subject I can see that I do have the correct $user available to me, but for some reason it doesn't like it when I try and extract $user->email
Anyone any ideas?
You're calling ->toArray() on the User object and so it is no longer an object!
$user = User::find(1); // Assuming you are already protecting yourself from potentially not finding a user
$viewData = $user->toArray(); // Or better still don't expose the underlying structure
Mail::send('emails.invite', $viewData, function($message) use ($user) {
$message->to($user->email);
$message->from('me#example.com');
$message->subject('Test Subject');
});
Your $user is type ofarray not object as you expected, because of toArray() called on returned model. Either change that line and remove said toArray() to keep it unchanged object, or make this line:
$message->to($user->email);
look more like:
$message->to($user['email']);
with proper array element reference.

Undefined variable inside Laravel view when using Mail

While trying to send verification email using Laravel 5.2, I get an error:
Undefined variable: confirmation_code (View:
C:\xampp\htdocs\laravel\resources\views\email\verify.blade.php)
My code looks like this:
Controller.php:
public function postSignup(Request $request){
$this->validate($request,[
'email'=>'required|unique:users|email',
'name'=>'required|max:50|min:3',
'password'=>'required|min:6',
'con-password'=>'required|same:password',
]);
$confirmation_code=['code'=>str_random(20)];
$name = $request->input('name');
Mail::send('email.verify',$confirmation_code,function($message)
use($request,$name){
$message->to($request->input('email'),$name)
->subject('Verify Your Email Address');
});
User::create([
'email'=>$request->input('email'),
'name'=>$request->input('name'),
'password'=>bcrypt($request->input('password'))
]);
return redirect()->back()->with('info','Congratulation you have been successfully registered.Please check your email for verification');
}
Mail.verify.blade.php:
<h2>Verify Your Email Address</h2>
<div>
Thanks for creating an account with the verification demo app.
Please follow the link below to verify your email address
{{ URL::to('register/verify/'.$confirmation_code) }}.<br/>
</div>
</body>
Try this:
Mail::send('email.verify', compact('confirmation_code'), function ($message) use($request, $name) {
$message->to($request->input('email'),$name)
->subject('Verify Your Email Address');
});
The reason why it fails is that Laravel views accept an associative array as their data, so that it can turn them into variables using keys as variables names and match them to their corresponding values.
What compact does is turn your variable into an associative array, with the name of the variable as its key (sort of the opposite of what the Laravel view will do).

Resources