Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use different POSTMARK_SECRET & POSTMARK_SERVER? #149

Open
christoph-werker opened this issue Sep 11, 2024 · 3 comments
Open

Use different POSTMARK_SECRET & POSTMARK_SERVER? #149

christoph-werker opened this issue Sep 11, 2024 · 3 comments

Comments

@christoph-werker
Copy link

Detailed description

I have software that manages client orders, and depending on the order, I need to use a specific POSTMARK_SECRET and POSTMARK_SERVER. Maybe I overlooked something, but how can I accomplish that?

Your environment

Include as many relevant details about the environment you experienced the bug in and how to reproduce it.

  • Laravel Postmark Version: 2.11.2
  • Laravel Version: 8.7.4
  • PHP Version: 8.2
@craigpaul
Copy link
Owner

Hey @christoph-werker, is this something that was made possible by #134?

@christoph-werker
Copy link
Author

Hi @craigpaul ,

That was fast. 💪
Tbo I'm not sure, because I need also to set another API-Key not just the Server-Id.

Furthermore what could be an elegant way to implement this? Without redundant code?

@craigpaul
Copy link
Owner

@christoph-werker The X-Postmark-Server-Token header is your API key, not the server ID (I'm not actually sure you supply a server ID when sending a transactional email, just the token, which determines the server to use, to my knowledge), but regardless I just realized you had provided the fact that you're on Laravel 8 with v2.11 of this package so that changes things. There isn't a built in way to do that, but I can provide an overview of how I've done it in the past before this existed (fair warning, it's not simple unfortunately).

We started off with creating a custom EmailChannel class that extended the MailChannel from Laravel. In the send method, before we passed it back to the parent to actually send the email, we would update the configuration and re-build the mail manager by re-assigning the $this->mailer dependency. It looks a little something like this at a high level:

$secret = '...'; // your updated secret

config()->set('mail.driver', 'postmark-custom');
config()->set('mail.default', 'postmark-custom');
config()->set('postmark.secret', $secret); // `services.postmark.secret` also works in this version of the package

app('mail.manager')->extend('postmark-custom', function () use ($secret) {
    return new PostmarkTransport(
        app(Factory::class),
        config()->get('mail.mailers.postmark.message_stream_id'),
        [],
        $secret,
    );
});

$this->mailer = app()->make('mail.manager');

The problem with doing this in a long running process like a queue worker/Horizon is that those configuration changes stick around for the next email being sent through that process, so you also need a way to reset $this->mailer back to the default mail driver. Hope that might spark an idea, but if you have further questions let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants