craftcms / postmark

A Postmark mail adapter for Craft CMS.
https://plugins.craftcms.com/postmark
MIT License
20 stars 4 forks source link

Set token via config #6

Closed joshuabaker closed 6 years ago

joshuabaker commented 6 years ago

It doesn’t appear possible to set the token value via a postmark.php config file. Attempting currently results in an error.

Config

<?php

return [
    'token' => 'TOKEN_HERE',
];

Error

Call to a member function setAttributes() on null
nateiler commented 6 years ago

Correct. Currently, the only way to set the token is via the admin panel. My assumption is you have alternate postmark instances and you want to tie them to an environment?

joshuabaker commented 6 years ago

Yeah, that’s right.

I was trying to figure out how to do this via the Mailer Component config. Wondering if that’s an approach here?

nateiler commented 6 years ago

I was just drilling into the System Settings and saw that option too.

I'm sure something like this might work (config/app.php):

<?php

return [
    '*' => [
        'components' => [
            'mailer' => function() {

                // Get the stored email settings
                $settings = Craft::$app->systemSettings->getEmailSettings();

                // Override the transport adapter class
                $settings->transportSettings['token'] = 'xxx';

                return Craft::createObject(
                    craft\helpers\App::mailerConfig($settings)
                );
            }
        ]
    ],

    'prod' => [
        'components' => [
            'mailer' => function() {

                // Get the stored email settings
                $settings = Craft::$app->systemSettings->getEmailSettings();

                // Override the transport adapter class
                $settings->transportSettings['token'] = 'prod-xxx';

                return Craft::createObject(
                    craft\helpers\App::mailerConfig($settings)
                );
            }
        ]
    ]
];

It's not very graceful.

I suppose it's also possible to add a settings model to the plugin and override what's in the database when the Postmark adapter is created.

nateiler commented 6 years ago

I don't really like the component config solution, because it implies that you'll always use Postmark.

joshuabaker commented 6 years ago

Yeah, I agree. As much as I hate the component config approach, it’s not really for this plugin to address.

Thanks for the responses, dude. 👍

nateiler commented 6 years ago

Hey Josh,

Check out the latest release 1.0.0 (https://github.com/flipboxfactory/craft-postmark/releases/tag/1.0.0)

You can now set the token via config/postmark.php

joshuabaker commented 6 years ago

Love it! Thanks, Nate. 💯