molteber / puz-dynamic-mail

Change mail configuration for laravel during runtime.
MIT License
9 stars 3 forks source link

How can use when email is send using notification. #11

Open jigarhalani opened 4 years ago

molteber commented 4 years ago

Hi, @jigarhalani, thanks for your interest in using this package.

Unfortunately, this is not possible to do in this package (for now at least).

As you can see, I haven't updated this package for a while, but if you want to, you can try to see if you can make something work?

It looks like it is possible to do something like this if one make that the dynamic mailer sends a Mailable, because then the notification object will use the Mailable object to send.

Could maybe be done something like this:

// app/Mail/WelcomeEmail.php
<?php
namespace App\Mail\WelcomeEmail

class WelcomeEmail extends DynamicMailable
{
  // ...
}

// app/Notifications/Welcome.php
namespace App\Notifications;

use Illuminate\Notifications\Notification;

class Welcome extends Notification
{
  public function via($notifiable)
  {
    return ['mail'];
  }

  public function toMail($notifiable)
  {
    return (new WelcomeEmail())->via('mailgun')->with(['domain' => 'someotherdomain.com']);
  }
}

And then the magic will be a part of DynamicMailable (which does not exists)

molteber commented 4 years ago

I'm currently experimenting with queuing email, and will take a look at notifications as well this weekend.

What laravel version are you using?

molteber commented 4 years ago

Hi again @jigarhalani

I played around a little bit last night. To try it out, you need laravel >= 6 and php >= 7.2

If you're using an older laravel / php version, please say so, and I'll will see if i can support those versions.

Alright, so how i made it work for now:

Notifications have to use the dynamic-mail notification channel instead of mail.

public function via($notifiable)
{
    return ['dynamic-mail'];
}

When using the dynamic-mail channel, one will need to use the toDynamicMail method to send a notification through that channel. You will also need to use DynamicMailMessage to be able to override the mail driver and configuration.

use Puz\DynamicMail\Messages\DynamicMailMessage;

// ...

public function toDynamicMail($notifiable)
{
    return (new DynamicMailMessage)
        ->line('The introduction to the notification.')
         ->action('Notification Action', url('/'))
        ->line('Thank you for using our application!')
        ->via('log')
        ->driverConfig(['other => 'email', 'driver' => 'configurations']);
}

The API here is a little bit different from the mailer because notifications / laravels mail channel notification already has a method with, so as a temporary api method at least, driverConfig will be the same as with for sending Mails with DynamicMail.

If your notification returns a Mailable, you will have to make sure that your mailable extends the DynamicMailable (Puz\DynamicMail\DynamicMailable). Unfortunately this does not provide an API to override mail drivers. Might add that some other time. Still under development basicly.

The DynamicMailable is actually made to be able to send queued mails, and still be able to override mail driver and/or configuration.

Please give it a shot! Hope it works for you, if so I might make a release of it. You can try it out by using the dev-develop version of this package (https://packagist.org/packages/puz/dynamic-mail#dev-develop)

jigarhalani commented 4 years ago

Hi @molteber

I am using laravel 5.8

jigarhalani commented 4 years ago

I have customized my own method to pass email configuration dynamically still, I will take look once I am free...

Thanks for your answer

molteber commented 4 years ago

I can probably allow it to use Laravel 5.8 too. I don't have much time to work on this until friday or saturday. I'll keep you posted!

molteber commented 4 years ago

I might even separate the notifications into an own package to keep things separate

molteber commented 4 years ago

@jigarhalani I wasn't able to get time to finish this. I'll take a look at it the coming week, I'll keep you posted!