最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Where do you store configuration profiles for CakePHP 4 Mailer? - Stack Overflow

programmeradmin2浏览0评论

I'm writing an app in CakePHP 4.5 which sends emails.

I'm trying to avoid writing this all over my code

$mailer = new Mailer('default');

$mailer->setEmailFormat('both')
->setFrom(['[email protected]' => 'My Company Name'])
->setSender('[email protected]', 'My Company Name')

I don't want to specify the From/sender address ([email protected]) and the associated name, "My Company Name", every time I send an email. Otherwise it will litter my Controllers with the same code which isn't following DRY principles.

According to the docs

Defining delivery profiles allows you to consolidate common email settings into re-usable profiles.

It doesn't say where you store this configuration. It gives an example

The values of above keys using Mailer or array, like from, to, cc, etc will be passed as first parameter of corresponding methods. The equivalent for: $mailer->setFrom('[email protected]', 'My Site') would be defined as 'from' => ['[email protected]' => 'My Site'] in your config

Which config file and how does this get passed in when instantiating new Mailer()?

I'm writing an app in CakePHP 4.5 which sends emails.

I'm trying to avoid writing this all over my code

$mailer = new Mailer('default');

$mailer->setEmailFormat('both')
->setFrom(['[email protected]' => 'My Company Name'])
->setSender('[email protected]', 'My Company Name')

I don't want to specify the From/sender address ([email protected]) and the associated name, "My Company Name", every time I send an email. Otherwise it will litter my Controllers with the same code which isn't following DRY principles.

According to the docs

Defining delivery profiles allows you to consolidate common email settings into re-usable profiles.

It doesn't say where you store this configuration. It gives an example

The values of above keys using Mailer or array, like from, to, cc, etc will be passed as first parameter of corresponding methods. The equivalent for: $mailer->setFrom('[email protected]', 'My Site') would be defined as 'from' => ['[email protected]' => 'My Site'] in your config

Which config file and how does this get passed in when instantiating new Mailer()?

Share Improve this question asked Jan 22 at 15:03 AndyAndy 5,40412 gold badges65 silver badges156 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

Which config file?

If it is application specific and you have initialized with the skeleton, this is the file named app.php (compare config/app.php (github)).

As Configuring your Application (cakephp.) suggests, you may have a different file layout (.php and .ini configuration files are supported) and/or multiple files. The set-up of the configuration is done in bootstrap.php (config/bootstrap.php (github)) and there you can command to use additional files (there is also some convention about app_local.php for settings that are specific to the concrete/local environment (github)).

The mailer profiles are defined in your configuration file(s) then under the Email key.

    'Email' => [
        'default' => [
            'transport' => 'default',
            'from' => '[email protected]',
            # ...
        ],
    ],

In this "default" Mailer profile you also find the transport. Its value "default" (as well) relates to the EmailTransport configuration key. It works with the same principle.

For concrete settings available for both mailer profile and transport keys, please continue to read, they are linked after the following example.

and how does this get passed in when instantiating new Mailer()?

By using the name of the profile, e.g. "default". It can be injected either with a setter method or in the constructor:

$mailer = new Mailer();
$mailer->setProfile('default');

// Or in constructor
$mailer = new Mailer('default');

(example from Using CakePHP > Mailer > Configuration)

Example configurations:

  • EmailTransport configuration (Mailer Transports) in the cakephp cms-tutorial
  • Email configuration (Mailer Profiles) in the cakephp cms-tutorial
发布评论

评论列表(0)

  1. 暂无评论