Sending email notification to existing user Laravel 5.8 - laravel

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)

Related

I need to do a function to resend OTP if user has not received it to update phone number functionality in laravel

Maybe I'm losing in a glass of water but I'm not understanding how implements this function, I use OTP code to verify phone when user compile the registration and when he try to update number. I'm using vonage, I put below my update method:
public function updatePhoneNumber(Request $request)
{
$basic = new \Vonage\Client\Credentials\Basic("44bc4bb2", "fYVcLeo0lMhmtjm1");
$client = new \Vonage\Client($basic);
$request->validate([
'telefono' => 'required|unique:users,telefono'
]);
$telefono = $request->input('telefono');
$otp = VerificationCode::create([
'otp' => rand(10000, 99999),
'expire_at' => Carbon::now()->addMinutes(10)
]);
$response = $client->sms()->send(
new SMS($telefono, 'Help4You', 'Il tuo codice di verifica è:'. "\n" . $otp->otp)
);
$message = $response->current();
if ($message->getStatus() == 0) {
echo "The message was sent successfully\n";
} else {
echo "The message failed with status: " . $message->getStatus() . "\n";
}
User::where('id', '=', Auth::user()->id)->update([
'telefono'=>Hash::make($request['telefono']),
]);
}
how can I resend the otp if user has not received it?
You simply need to have 2 separate functions. The logic for creating an OTP and sending the OTP is in a different function. Myself, I create a service for it and store it in a App\Services folder since it is a functionality I commonly use.
So, back to your question, separate the logic for creating an OTP and sending the otp, in your case, the following code should not be part of your function above
$telefono = $request->input('telefono');
$otp = VerificationCode::create([
'otp' => rand(10000, 99999),
'expire_at' => Carbon::now()->addMinutes(10)
]);
$response = $client->sms()->send(
new SMS($telefono, 'Help4You', 'Il tuo codice di verifica è:'. "\n" . $otp-
>otp)
);
}
If you are implementing this is a separate class, remember to return the outcome of the sending of the OTP, it can either be success or error. Also, the only value you need to pass onto the function will be the recipient's cellphone number.
So, first time you send the OTP, you simply call the function or instance of the class, depending how you implemented.
Now, if the OTP was not delivered or a user wants to request another OTP, you call the function again.
You can add additional validation or a field on you table to check how many retries a user does for an OTP since the sms will cost your client money.
Once you separate your logic, you have now made your code to be reusable and is you need to use the same API to send a different SMS, you can do so.
Hope this helps.

How to use a Webhook url with Paystack and send values to Database

I am using https://github.com/iamraphson/vue-paystack in my laravel+vue SPA project. I can receive payments perfectly but now need to hit a webhook url so that I can update database and give value to user. I have set up the route and added the url on my paystack dashboard. What should I do next? How do I get the charge events and other details in my controller method and send to database?
I have tried the following in my controller:
public function payme(Request $request){
//This receives the webhook
//$email = json_decode($input['data']['customer']['email']);
$email = $request->input('data.customer.email');
$bankname = $request->input('data.gateway_response');
//$bankname = json_decode($input['data']['gateway_response']);
$user = User::where('email', $email);
$user->bankname = $bankname;
$user->save();
return response()->json('processed', 200);

How to add more parameter on controller laravel mail

I want to send email using Laravel and success with this code
Mail::to($email)->send(new SendMail($name))
Now I want to send more parameter like subject with this code, but didn't work.
No error message but email not being sent.
Mail::send(new SendMail($name function($message) use ($email){
$message->to($email)->subject('Verify your Account');
}));
Any suggestions? Thanks in advance!
You can use something like that
$template = 'view.email';
$paramets = ['data'=>$data];//Your to value to print in email
$email = 'to#email.com'// to email
$cc = 'cc#email.com' // or null
$toName = 'John Doe' // or null
$subject = 'Verify your Account';
Mail::send($template, $parameters, function($message) use($email,$cc,$toName,$subject) {
$message->from(env('sender'), env('nameSender'));
if($cc) $message->cc($cc);
$message->to($email, $toName)
->subject($subject);
});
I am using a generic method. So i can use this method to all emails that i need to send

Get recipient name on view in laravel mail

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}}
}

When i am trying to send mail from contactUS form getting this error using swiftmailer in Laravel 5.2

when i am trying to send Mail through Contact Us Form receiving this Error
"Address in mailbox given [] does not comply with RFC 2822, 3.6.2."
I try search to find solution but I cannot find one. I edited config/mail.php
public function sendContactInfo(ContactMeRequest $request)
{
$data = $request->only('name', 'email');
$emailto="******#gmail.com";
$data['messageLines'] = explode("\n", $request->get('message'));
Mail::send('publicPages.contactus', $data, function ($message) use ($emailto) {
$message->subject('Contact Us Form: ')
->to(config('blog.contact_email'))
->replyTo($data['email']);
});
return back()
->withSuccess("Thank you for your message. It has been sent.");
}
with configuration file
i am following this tutorial
Laravel Send Mail
use $data['email']
Mail::send('publicPages.contactus', $data, function ($message) use ($emailto,$data['email']) {
$message->subject('Contact Us Form: ')
->to(config('blog.contact_email'))
->replyTo($data['email']);
});

Resources