I tried to set rate limit for my notifications queue (called email)
Here is the documentation I checked here. I use laravel 5.6.
Here is the code I tried
class MyController extends Controller{
function myControllerFunction(){
Redis::throttle('email')->allow(1)->every(120)->then(function () use ($user, $userReport) {
$user->notify(new ReportStandard($userReport));
}, function ($error) {
Log::error($error);
return $this->release(120);
});
}
}
I got an error on $this->release(120) because the $this in the closure refer to the MyController class instead of a Job class.
I do not know how I can rate limit my email queue used by notifications?
Notes: ReportStandard is defined this way
class ReportStandard extends Notification implements ShouldQueue
{
use Queueable;
public $userReport;
/**
* Create a new notification instance.
*
* #return void
*/
public function __construct($userReport)
{
$this->queue = "email";
$this->userReport = $userReport;
}
/**
* Get the notification's delivery channels.
*
* #param mixed $notifiable
* #return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* #param mixed $notifiable
* #return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
Log::info(now() . " report-standard");
return (new MailMessage)
->subject('Reçu pour votre paiement à Autorigin')
->view('front.mails.cross-sell.report-standard', [
'notifiable' => $notifiable,
'userReport' => $this->userReport
]);
}
/**
* Get the array representation of the notification.
*
* #param mixed $notifiable
* #return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
Related
I want tp apply queue for my Notifications, so I implemented that at my Notification which ResetPassword:
class ResetPassword extends Notification implements ShouldQueue
Then I ran php artisan queue:table and migrate it so the table jobs created successfully at the DB.
And also change the QUEUE_CONNECTION to database at .env file and re-run php artisan serve.
But when I test this and clicked on reset password link, a new table row must be added to jobs table but it does not.
And instead of that, this error returns:
ErrorException Undefined property:
App\Notifications\ResetPassword::$queue
...\notification\vendor\laravel\framework\src\Illuminate\Notifications\NotificationSender.php:195
So what is going wrong here ? How can I fix this issue ?
I would really appreciate any idea or suggestion from you guys...
Thanks in advance.
UPDATE #1:
ResetPassword.php:
<?php
namespace App\Notifications;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Lang;
class ResetPassword extends Notification implements ShouldQueue
{
/**
* The password reset token.
*
* #var string
*/
public $token;
/**
* The callback that should be used to build the mail message.
*
* #var \Closure|null
*/
public static $toMailCallback;
/**
* Create a notification instance.
*
* #param string $token
* #return void
*/
public function __construct($token)
{
$this->token = $token;
}
/**
* Get the notification's channels.
*
* #param mixed $notifiable
* #return array|string
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Build the mail representation of the notification.
*
* #param mixed $notifiable
* #return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
if (static::$toMailCallback) {
return call_user_func(static::$toMailCallback, $notifiable, $this->token);
}
return (new MailMessage)
->subject('subject goes here')
->line('This email is sent to you')
->action(Lang::get('Reset Password'), url(config('app.url').route('password.reset', ['token' => $this->token, 'email' => $notifiable->getEmailForPasswordReset()], false)))
->line(Lang::get('Until the next 60 minutes you can use this link', ['count' => config('auth.passwords.'.config('auth.defaults.passwords').'.expire')]))
->line(Lang::get('If you did not request a password reset, no further action is required.'));
}
/**
* Set a callback that should be used when building the notification mail message.
*
* #param \Closure $callback
* #return void
*/
public static function toMailUsing($callback)
{
static::$toMailCallback = $callback;
}
}
Look Like you have forgot to use Queueable Trait in your notification
use Illuminate\Bus\Queueable;
class ResetPassword extends Notification implements ShouldQueue
{
use Queueable;
Queueable trait has propety $queue
Ref:https://laravel.com/docs/8.x/notifications#queued-notifications-and-database-transactions
I would like to send a mail from a button on Laravel Nova with an action (which I think is the most appropriate).
I already have a template of the mail saved in the mailable, I have made my resource but I don't know what to put inside since I need to retrieve information from this resource like for example the name, a price or the date when the line was created (in my table related to my resource).
My resource code:
<?php
namespace App\Nova;
use App\Image;
use Gloudemans\Shoppingcart\Cart;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Laravel\Nova\Fields\Currency;
use Laravel\Nova\Fields\Date;
use Laravel\Nova\Fields\Heading;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;
use Techouse\IntlDateTime\IntlDateTime;
class Order extends Resource
{
/**
* The model the resource corresponds to.
*
* #var string
*/
public static $model = \App\Order::class;
public static $group = 'Paramètres';
public static function label()
{
return __('Commandes');
}
public static function singularLabel()
{
return __('Commande');
}
/**
* The single value that should be used to represent the resource when being displayed.
*
* #var string
*/
public static $title = 'id';
/**
* The columns that should be searched.
*
* #var array
*/
public static $search = [
'id',
];
/**
* Get the fields displayed by the resource.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function fields(Request $request)
{
return [
ID::make()->sortable(),
Text::make('Nom du client', 'name'),
IntlDateTime::make('Passée le', 'created_at')
->locale('fr')
->readonly(),
Currency::make('Prix', 'total')->currency('EUR'),
Text::make('Mode de paiement', 'gateway')
->readonly(),
Text::make('Numéro de téléphone', 'phone'),
Heading::make('Adresse de livraison'),
Text::make('Adresse', 'address')
->hideFromIndex(),
Text::make('Code postal', 'postal_code')
->hideFromIndex(),
Text::make('Ville', 'city')
->hideFromIndex(),
Text::make('Pays', 'country')
->hideFromIndex(),
];
}
/**
* Get the cards available for the request.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function cards(Request $request)
{
return [];
}
/**
* Get the filters available for the resource.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function filters(Request $request)
{
return [];
}
/**
* Get the lenses available for the resource.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function lenses(Request $request)
{
return [];
}
/**
* Get the actions available for the resource.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function actions(Request $request)
{
return [];
}
}
My mailable code:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class ResendOrder extends Mailable
{
use Queueable, SerializesModels;
/**
* Elements de contact
* #var array
*/
public $contact;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct(Array $contact)
{
$this->contact = $contact;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->view('emails.orderconfirmation')
->subject("Confirmation de commande");
}
}
And finally my Action code:
<?php
namespace App\Nova\Actions;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Collection;
use Laravel\Nova\Actions\Action;
use Laravel\Nova\Fields\ActionFields;
class EmailOrderConfirmation extends Action
{
use InteractsWithQueue, Queueable;
/**
* Perform the action on the given models.
*
* #param \Laravel\Nova\Fields\ActionFields $fields
* #param \Illuminate\Support\Collection $models
* #return mixed
*/
public $name = 'Renvoyer le mail de confirmation de commande';
public function handle(ActionFields $fields, Collection $models)
{
return Action::message('Mail envoyé');
}
/**
* Get the fields available on the action.
*
* #return array
*/
public function fields()
{
return [];
}
}
I don't really know where to go and what to use, if you could enlighten me on that I would be very grateful, thank you!
// in EmailOrderConfirmation --nova action
// declare what you are using
// use Illuminate\Support\Facades\Mail;
// use App\Mail\ResendOrder;
public function handle(ActionFields $fields, Collection $models)
{
//loop over the orders that have been selected in nova
foreach ($models as $order) {
$contact = $order->contract; //however you are getting contract data
//assuming you have a $order->user order belongs to user relationship
//send mail to the user, with the order/contract details to create your email
Mail::to($order->user->email)->send(new ResendOrder($contact));
}
//return a message to nova
return Action::message('Mail envoyé');
}
// in Order /Nova resource
// declare what you are using
// use App\Nova\Actions\EmailOrderConfirmation;
public function actions(Request $request)
{
return [new EmailOrderConfirmation];
}
I created a localized copy of ResetPassword.php, redefined the sendPasswordResetNotification () method in app\user. In general, I did everything according to the rule (I think so))). All the rules, sends a letter to the mail but there is only one problem. This link does not expire. by default there is 60 minutes, after which the link should be destroyed. But this does not happen. Below my code will be grateful for any help.
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class mytelresetpassword extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* #return void
*/
public function __construct($token)
{
//
$this->token = $token;
}
/**
* The callback that should be used to build the mail message.
*
* #var \Closure|null
*/
public static $toMailCallback;
/**
* Get the notification's delivery channels.
*
* #param mixed $notifiable
* #return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* #param mixed $notifiable
* #return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
if (static::$toMailCallback) {
return call_user_func(static::$toMailCallback, $notifiable, $this->token);
}
$time = ['count' => config('auth.passwords.'.config('auth.defaults.passwords').'.expire')];
$time = $time['count'];
return (new MailMessage)
->subject('Şifrənin Dəyişdirilməsi')
->line('Hesabınızdan şifrə yeniləmə tələbi aldığımız üçün bu mail sizə göndərilib.')
->action('Şifrəni Yenilə', url(config('app.url').route('password.reset', ['token' => $this->token, 'email' => $notifiable->getEmailForPasswordReset()], false)))
->line('Bu link '.$time.' dəqiqə müddətində keçərlidir, bu müddət bitdikdən sonra deaktiv olunacaq!')
->line('Şifrə dəyişməyi tələb etməmişsinizsə, "Şifrəni Yenilə" düyməsini klikləməyə ehtiyac yoxdur.');
}
/**
* Get the array representation of the notification.
*
* #param mixed $notifiable
* #return array
*/
public function toArray($notifiable)
{
return [
//
];
}
/**
* Set a callback that should be used when building the notification mail message.
*
* #param \Closure $callback
* #return void
*/
public static function toMailUsing($callback)
{
static::$toMailCallback = $callback;
}
}
in app\user:
public function sendPasswordResetNotification($token)
{
$this->notify(new mytelresetpassword($token));
}
I propose adding a few lines in your ResetPasswordController.php:
public function showResetForm(Request $request, $token = null)
{
$email = $request->email;
if (!$token)
return redirect()->route('login');
if (!$email)
return redirect()->route('login');
$reset = PasswordReset::where('email', $request->input('email'))->first();
if (!$reset)
return redirect()->route('login');
$expiry = Carbon::now()->subMinutes( 60 );
if ($reset->created_at <= $expiry) {
return redirect()->route('login');
}
return view('auth.passwords.reset')->with(
['token' => $token, 'email' => $request->email]
);
}
laravel notify() undefined method. how to solved it....help me..
Error is:-
Call to undefined method stdClass::notify()
My Controller Code Here:
use Illuminate\Http\Request;
use \App\Notifications\ResetLink;
use Auth;
use App\User;
use DB;
use App\Password;
public function forgot(){
return view('forgot');
}
public function forgotPw(Request $request){
if($user = User::where('email',$request->email)->first()){
DB::table('password_resets')->insert([
'token' => $this->token(),
'email' => $user->email
]);
$pr = DB::table('password_resets')->where('email',$user->email)- >first();
$pr->notify(new ResetLink($pr));
request()->session()->flash('success', "Forgot Link Successfully Sent...");
return redirect('login');
} else {
request()->session()->flash('error', "Forgot Link Not Sent...");
return redirect('forgot');
}
}
My Notification Code Here:
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class ResetLink extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* #return void
*/
public function __construct()
{
public $pr;
}
/**
* Get the notification's delivery channels.
*
* #param mixed $notifiable
* #return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* #param mixed $notifiable
* #return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('Click the Button and Reset Password!')
->action('Password Reset', url('/reset'))
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* #param mixed $notifiable
* #return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
I tried to last 2 hours but No send mail in mailtrap....
Thank You Advance for Help me..
I am trying to send an email using Notification class in laravel 5.6 which I am new to.
I am trying to pass user and book information but every time I get the following :
Undefined variable: user
here is my successEmail.php:
<?php
namespace BOOK_DONATION\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class successEmail extends Notification
{
use Queueable;
public $user;
public $book;
/**
* Create a new notification instance.
*
* #return void
*/
public function __construct($user,$book)
{
//
$this->user=$user;
$this->book=$book;
}
/**
* Get the notification's delivery channels.
*
* #param mixed $notifiable
* #return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* #param mixed $notifiable
* #return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->greeting('Dear'.$user->name.'thak you for creating donation for '.$book->name);
}
/**
* Get the array representation of the notification.
*
* #param mixed $notifiable
* #return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
and here is the function that call the class successEmail:
public function doBook(Request $request){
$validatedData = $request->validate([
'title' => 'string|required|max:255',
'Author'=> 'string|required|max:255',
'Cover_image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
'Book_description'=>'string|required|max:255',
]);
$path= $request->Cover_image->store('images');
$uid=Auth::user()->id;
$country=Auth::user()->country;
$city=Auth::user()->city;
$book=Book::create(['title'=>$request->input('title'),
'Author'=>$request->input('Author'),
'user_id'=>$uid,
'country'=>$country,
'city' =>$city,
'Book_description'=>$request->input('Book_description'),
'path'=>$path,
]);
$user = Auth::user();
$user->notify(new successEmail($user,$book));
return redirect('/donation/create')->with('status', 'Thank you for your donation');
}
as you see I passed the user and the book variable to the constructor and declared them as public so what is that I am missing?
Those variables do not exist in the function scope but they do exist as class properties so they should be treated as such.
return (new MailMessage)
->greeting('Dear '.$this->user->name.' thank you for creating donation for '.$this->book->name);
You need to make the call to $this->user to access the variable in the class scope.
/**
* Get the mail representation of the notification.
*
* #param mixed $notifiable
* #return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->greeting('Dear'.$this->user->name.'thak you for creating donation for '.$this->book->name);
}
You should try this:
use Auth;
public function toMail($notifiable)
{
return (new MailMessage)
->greeting('Dear'.$this->user->name.'thak you for creating donation for '.$this->book->name);
}
That happened to me too. In the function toMail you need to redeclare the variables like this
$user= $this->user;
And then you call the variable like this
->line('user:'.''.$user,'')