Send notification to email - php

I work on a website project with the laravel framework and I want when I click on the button send a notification either send to the user's email
$invite = Invite::create([
'name' => $request->get('name'),
'email' => $request->get('email'),
'token' => str_random(60),
]);
$invite->notify(new UserInvite());
tnx to help me

What you are using is mail notification, Here is the answer but you can refer to notification section of laravel docs for more information:
https://laravel.com/docs/5.4/notifications
first generate the notification using your terminal in project folder:
php artisan make:notification UserInvite
Then in generated file specify your driver to be 'Mail'. byr default it is. Also laravel has a nice sample code there. And it is better you inject your $invite to the notification so you can use it there. here is a quick sample code. You can find the notification generated under App\Notifications.
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use App\Invite;
class UserInvite extends Notification implements ShouldQueue
{
use Queueable;
/**
* Create a new notification instance.
*
* #return void
*/
public function __construct()
{
//
}
public function via($notifiable)
{
return ['mail']; // Here you specify your driver, your case is Mail
}
public function toMail($notifiable)
{
return (new MailMessage)
->greeting('Your greeting comes here')
->line('The introduction to the notification.') //here is your lines in email
->action('Notification Action', url('/')) // here is your button
->line("You can use {$notifiable->token}"); // another line and you can add many lines
}
}
now you can call your notification:
$invite->notify(new UserInvite());
since you are notifying on invite, your notifiable is the same invite. As a result in your notification you can use $notification->token to retrieve the token of invite object.
Please let me know if I can be of any help.
regards.

Related

Laravel queue stalls when used with sentry

i have a working project written in laravel, i've integrated Sentry to monitor for errors, i followed instructions on their site, basically just
composer require sentry/sentry-laravel
and in my App/Exceptions/Handler.php
if (app()->bound('sentry') && $this->shouldReport($exception)) {
app('sentry')->captureException($exception);
}
Now this works fine, i see errors in sentry.
But at the other end of the app wherever i use queue, for example i have "forgot password" notification and it implements ShouldQueue class, practically:
<?php
namespace App\Notifications\Auth;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class PasswordResetRequest extends Notification implements ShouldQueue
{
use Queueable;
protected $token;
public function __construct($token)
{
$this->token = $token;
}
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
$url = url('/password/find/'.$this->token);
return (new MailMessage)
->line('You are receiving this email because we received a password reset request for your account.')
->action('Reset Password', url($url))
->line('If you did not request a password reset, no further action is required.');
}
}
And when triggered works as expected, i have a new record in jobs table (i have database as queue driver)
now when i start my worker
php artisan queue:work
it stalls it works last job over and over
there is no mail sent or received...
By the way this is only when i have reporting in sentry, if i remove those lines in App/Exceptions/Handler.php in report() method it's all good, also if i remove queue from notification it works, only the combinatio of sentry error reporting and notification queuing is making me a big problem.
Unfortunately, Sentry-laravel doesn't work with queues.
Please, see this issue on their GitHub repo to get more info about it:
https://github.com/getsentry/sentry-laravel/issues/18
There is another issue (open) where they are trying to fix it:
https://github.com/getsentry/sentry-laravel/pull/228
If you really need to put these logs on a queue now you should use https://github.com/twineis/raven-php.

Laravel 5.5 email setup using Mailgun and Bogardo

I am attempting to setup automated email notification in my Laravel 5.5 app using Mailgun. I have the Mailgun SDK installed along with the recommended Laravel library - Bogardo. The reason I am using the Bogardo library instead of just using the Mailgun SDK or built in Laravel email functionality is neither of these allow for click tracking, bounces and other analytic functionality (that I know of). I am able to send emails just fine using Tinker. However, I am not 100% sure how to properly call my new mailable to send an email that way. Here is my mailable class:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class BaseEmail extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
$data = ['This is a message from Mailgun!'];
return Mailgun::raw($data, function($message) {
$message
->to('email#domain.com', 'Name Name')
->subject('Yoohoo!')
->from('otheremail#domain.com', 'Name')
->tag(['tag','tag2']);
});
}
}
When I call:
$mail = new App\Mail\BaseEmail();
$mail->send();
I get the following error
TypeError: Too few arguments to function Illuminate\Mail\Mailable::send(), 0 passed in /web/vendor/psy/psysh/src/Psy/ExecutionLoop/Loop.php(90) : eval()'d code on line 1 and exactly 1 expected
and
$mail->send('this');
I get
TypeError: Argument 1 passed to Illuminate\Mail\Mailable::send() must be an instance of Illuminate\Contracts\Mail\Mailer, string given on line 1
Sorry if this is trivial, but I have been following their docs and have Googled everything that I can think of with no luck.
Any direction would be fantastic!
Thanks!
It appears that the Bogardo package does not support Laravel Mailables...
https://github.com/Bogardo/Mailgun/issues/72
You don't need to use the send() method when you're using raw().This code will send an email:
Mailgun::raw($data, function($message) {
$message->to('email#domain.com', 'Name Name')
->subject('Yoohoo!')
->from('otheremail#domain.com', 'Name')
->tag(['tag','tag2']);
});
You also don't need to use Mailable when you're using the package.

How to modify password change mail made with Laravel auth?

In Laravel Framework 5.4.18 I just ran php artisan make:auth
When I request to reset my password, I get an email that says
(...)
You are receiving this email because we received a password reset
request for your account
(...)
Where is the file where it is specified to say that? I want to change it completely.
Notice that here is how to change (only) the general appearance of any notification, and that here is how to change (in addition) the body of the notification.
Thank you very much.
Your User model uses the Illuminate\Auth\Passwords\CanResetPassword trait. This trait has the following function:
public function sendPasswordResetNotification($token)
{
// use Illuminate\Auth\Notifications\ResetPassword as ResetPasswordNotification
$this->notify(new ResetPasswordNotification($token));
}
When a password reset is requested, this method is called and uses the ResetPassword notification to send out the email.
If you would like to modify your reset password email, you can create a new custom Notification and define the sendPasswordResetNotification method on your User model to send your custom Notification. Defining the method directly on the User will take precedence over the method included by the trait.
Create a notification that extends the built in one:
use Illuminate\Auth\Notifications\ResetPassword;
class YourCustomResetPasswordNotification extends ResetPassword
{
/**
* Build the mail representation of the notification.
*
* #param mixed $notifiable
* #return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('This is your custom text above the action button.')
->action('Reset Password', route('password.reset', $this->token))
->line('This is your custom text below the action button.');
}
}
Define the method on your User to use your custom notification:
class User extends Authenticatable
{
public function sendPasswordResetNotification($token)
{
$this->notify(new YourCustomResetPasswordNotification($token));
}
}
First open a terminal and go to the root of the application and run the following command:
php artisan vendor:publish
You will see some files copied, you can find the email template files in
Root_Of_App/resources/views/vendor
You can edit the Email templates for notifications there.

Laravel echo pusher not receiving broadcast events

I've a problem that I'm trying to fix all day now. I've followed
this tutorial. The goal is to make a chat with Laravel echo, vue.js and pusher.
I've done everything exactly like the tutorial but for some reason I do not receive any events in my pusher console. Only the connection shows up:
But no events. The event that I fire looks like this:
<?php
namespace App\Events;
use App\Message;
use App\User;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class MessageSent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* #var
*/
public $user;
/**
* #var
*/
public $message;
/**
* MessageSent constructor.
* #param User $user
* #param Message $message
*/
public function __construct(User $user, Message $message)
{
$this->user = $user;
$this->message = $message;
}
/**
* Get the channels the event should broadcast on.
*
* #return Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('chat');
}
}
I fire the event like this:
broadcast(new MessageSent($user, $message))->toOthers();
When I dd('test'); like this in my MessageSent event class:
public function broadcastOn()
{
dd('test');
return new PrivateChannel('chat');
}
The dd('test'); shows up in my network tab.
I'm using Laravel 5.4 and Vue.js 2.0 with Homestead. What could be going on here?!
It looks likes you are following this tutorial. I also had a hard time to figure it out. I already answered here. Can you please check it out?
I worked on typing feature in the chat system. Please take a look at the code on GitHub.
Let me know if you have any questions. Thanks :)
By the looks of your debug console screenshot you are never managing to subscribe to any channels, have you set up the necessary authentication for the private channel subscription?
The complete demo code for the tutorial that you've been following is on github so you might want to take a look at that and see where yours differs.
If you are on Laravel 5.4, make sure you have set-up the channel authentication.
For example, in your routes/channels.php file, there should be something like this:
Broadcast::channel('chat', function ($user) {
return true; // change this to your authentication logic
});

Queue notification laravel 5.3 issue

Well im testing this new notification stuff implemented in laravel 5.3 and its great,
i have this notification class which sends a mail to the authenticated user (when he hits a specific route) which is the default code.
notification
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class notifyme extends Notification implements ShouldQueue
{
use Queueable;
public function __construct()
{
//
}
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', 'https://laravel.com')
->line('Thank you for using our application!');
}
This is the controller functions that instantiates the notification class
public function notifyme()
{
$user = Auth::user()
$user->notify(new notifyme($user));
//$user->notify((new notifyme($user))->delay(Carbon::now()->addMinutes(10)));
return redirect('/home');
}
now while using a ubuntu os , and setting my queue driver as sync which should work fine on localhost QUEUE_DRIVER="sync"
i started a worker php artisan queue:work
But nothing shows on the terminal windows also page still a bit slow (queues are not working)
i have the default queue.php and i didnt change it, and as i mentioned, im using sync as a driver
Any suggested solution?
sync driver doesn't use queues, it allows to run jobs synchronously for running tests for examle.
You need to use one of the driver provided by laravel listed here - Laravel queues, or install some custom like RabbitMQ or something else

Categories