BaseEmailConfig.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. // Support BC (snake case config)
  3. if (!Configure::read('Mail.smtpHost')) {
  4. Configure::write('Mail.smtpHost', Configure::read('Mail.smtp_host'));
  5. }
  6. if (!Configure::read('Mail.smtpUsername')) {
  7. Configure::write('Mail.smtpUsername', Configure::read('Mail.smtp_username'));
  8. }
  9. if (!Configure::read('Mail.smtpPassword')) {
  10. Configure::write('Mail.smtpPassword', Configure::read('Mail.smtp_password'));
  11. }
  12. /**
  13. * BaseEmailConfig for APP/Config/email.php
  14. *
  15. * Defaults to `Smtp` as transport.
  16. *
  17. * You can set up your $default and other configs without having to specify a password
  18. * Those will be read from Configure::read('Email.Pwd').
  19. *
  20. * You can also specify a few more things via Configure, e.g. 'Email.live' to force sending emails.
  21. * Per default it would not send mails in debug mode, but log them away.
  22. *
  23. * Additionally, you can set custom SMTP configs via Configure::read('Mail'):
  24. * - Smtp.host
  25. * - Smtp.username
  26. * - Smtp.password
  27. * Those will then be merged in.
  28. *
  29. * Your email.php config file then should not contain any sensitive information and can be part of version control.
  30. */
  31. class BaseEmailConfig {
  32. public $default = [
  33. 'transport' => 'Smtp',
  34. ];
  35. /**
  36. * Read Configure Email pwds and assign them to the configs.
  37. * Also assigns custom Mail config as well as log/trace configs.
  38. */
  39. public function __construct() {
  40. if (!empty($this->default['log'])) {
  41. $this->default['report'] = true;
  42. }
  43. if (isset($this->default['log'])) {
  44. unset($this->default['log']);
  45. }
  46. if (isset($this->default['trace'])) {
  47. $this->default['log'] = 'email_trace';
  48. }
  49. // Depreated, use Email.[TransportClass]. instead
  50. if ($config = Configure::read('Mail')) {
  51. if (!empty($config['smtpHost'])) {
  52. $this->default['host'] = $config['smtpHost'];
  53. }
  54. if (!empty($config['smtpUsername'])) {
  55. $this->default['username'] = $config['smtpUsername'];
  56. }
  57. if (!empty($config['smtpPassword'])) {
  58. $this->default['password'] = $config['smtpPassword'];
  59. }
  60. if (!empty($config['smtpPort'])) {
  61. $this->default['port'] = $config['smtpPort'];
  62. }
  63. if (isset($config['smtpTimeout'])) {
  64. $this->default['timeout'] = $config['smtpTimeout'];
  65. }
  66. if (isset($config['smtpTls'])) {
  67. $this->default['tls'] = $config['smtpTls'];
  68. }
  69. }
  70. // Add transport specific Configure settings
  71. if ($config = Configure::read('Email.' . $this->default['transport'])) {
  72. $this->default = $config + $this->default;
  73. }
  74. // Add password data from Configure
  75. $pwds = (array)Configure::read('Email.Pwd');
  76. foreach ($pwds as $key => $val) {
  77. if (isset($this->{$key})) {
  78. $this->{$key}['password'] = $val;
  79. }
  80. }
  81. // Prevent debug mails to be accidently sent
  82. if (Configure::read('debug') && !Configure::read('Email.live')) {
  83. $this->default['transport'] = 'Debug';
  84. if (!isset($this->default['trace'])) {
  85. $this->default['log'] = 'email_trace';
  86. }
  87. }
  88. }
  89. }