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()
?
1 Answer
Reset to default 3Which 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-tutorialEmail
configuration (Mailer Profiles) in the cakephp cms-tutorial