How to trigger a event for the Mail service? - laravel

How to trigger a event for the Mail service?
Example
If mail template code equals mail.order.complete add bcc?

OctoberCMS is built on Laravel 5.1 and to do handle mail events you will need handle it within your Plugin.php file.
For example to handle the process just before the mails being sent:
class Plugin extends PluginBase
{
[...]
public function boot()
{
Event::listen('mailer.sending', function(){
});
}
}
To learn more about events in OctoberCMS and Laravel you can check out these links:
https://octobercms.com/docs/services/events
https://laravel.com/docs/5.1/events
https://laravel.com/docs/5.1/mail

Related

Updating a model property by POST API, can run an event?

The clear description is, an API with the post method updates a value of a model property. how I can set up a listener in Laravel that after every change by API, run an event for example send email or sms?
If you were wanting to create an event when any update happened you could easily just do this via the boot.
class SomeModel extends Model
{
public static function boot()
{
static::updated(function ($model) {
// send your email/sms
});
parent::boot();
}
}
However this would trigger when ever the model is updated, Since you only want this when you are updating via the API you would want to create a custom event and trigger it via dispatch in your api route/controller.
https://laravel.com/docs/8.x/events#dispatching-events

How can I send email and SMS together using Laravel 5.5?

How can I send email and SMS together using Laravel 5.5 ?
Should I use mailable and SMS service or Notification with SMS and How ?
You can create an event in laravel's App/Events folder.
put all the code for send mail and sms with there configuration, just call that event wherever you want to hit sms or mail.
You may create one function which will include both notify() method for an email and dispatch() method for message
example
public function Notification(Model $tender)
{
$user = access()->user();
/* Send confirmation email */
$tender->notify(new Email($user));
/* Send confirmation message */
Send::dispatch($user);
}
Then you will just call a single function that is Notification() which will do both sending email and message

Vue - Listen to dynamically created channel name

I'm trying to integrate Laravel-Vue-Pusher based notification and laravel's broadcasting documentation has been very helpful.
So we create an laravel event, trigger and broadcast it on a Pusher channel.
On the Javascript side, we use Echo to listen for event broadcasts.
Here's some example-code for from the documentation:
Echo.private(`order.${orderId}`)
.listen('ShippingStatusUpdated', (e) => {
console.log(e.update);
});
I tried using it in Vue and it threw an ReferenceError: orderId is not defined.
On the Laravel side, here's the event that's broadcasting it on that channel:
public function broadcastOn()
{
return new PrivateChannel('order.'.$this->order->id);
}
Event is fired successfully and it also gets logged in Pusher Dashboard.
But i'm unable to figure out why i'm getting missing orderId error in Vue. Any help would be appreciated.

Listen to Event : User::creating

I'm doing a new plugin for OctoberCms. I would like to limit the front end registration for some specific domains.
I tried to this :
class Plugin extends PluginBase
{
[......]
public function boot()
{
// Listen for user creation
Event::listen('eloquent.creating: October\Rain\Auth\Models\User', function($model) {
{
$this->checkDomains($user);
[.....]
}
}
}
But my listener is not working. Do you know what is the Event, I should listen to catch before a new account is created ?
Thanks
You can bind to all of the model internal events like this:
User::extend(function($model) {
$model->bindEvent('model.beforeSave', function() use ($model) {
// do something
});
});
You can use before and after for create, update, save, fetch and delete
Alternatively, you can use,
public function boot()
{
User::creating(function($model) {
var_dump($model->name);
});
}
available events to listen:
creating, created, updating, updated, deleting, deleted, saving, saved, restoring, restored
You can also use the following:
\Event::listen('eloquent.creating: RainLab\User\Models\User', function($user){
$this->checkDomains($user);
});
Are you referring to a Front-End User's registration ? - I assumed you're using the RainLab User Plugin which has an event rainlab.user.beforeRegister fired in the Account component or you can add a custom one in the model's beforeCreate() event
then simply create a init.php file in the root directory of your plugin and list your listeners there :
Event::listen('rainlab.user.beforeRegister', 'Path\To\ListenersClass');

Send email from queued event handler

I use Lumen 5.1 and Redis for queues. And I have a pretty standard event handler that should send an email:
<?php
namespace App\Handlers\Events;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Events\UserHasRegistered;
use Illuminate\Contracts\Mail\Mailer;
class SendWelcomeEmail implements ShouldQueue
{
protected $mailer;
public function __construct(Mailer $mailer)
{
$this->mailer = $mailer;
}
public function handle(UserHasRegistered $event)
{
$user = $event->user;
$this->mailer->raw('Test Mail', function ($m) use ($user) {
$name = $user->getFirstName().''.$user->getLastName();
$m->to($user->auth()->getEmail(), $name)->subject('This is a test.');
});
}
}
The email is sent when I don't use the ShouldQueue interface. But when I push the event handler to the queue (i. e. use the ShouldQueue interface), the email is not sent and I don't get any error messages.
Do you have any ideas how to solve or debug this?
It was not a bug, just an unexpected behaviour.
I am using Xampp on Windows and the php mail driver for development. For some reason the queued mails were not saved in the default mailoutput folder within the Xampp directory. Instead a new mailoutput folder was automatically created within the Lumen directory.
There I found all the missed mails. :)

Resources