BaseEmailConfig.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. * You can set up your $default and other configs without having to specify a password
  16. * Those will be read from Configure::read('Email.Pwd').
  17. *
  18. * You can also specify a few more things via Configure, e.g. 'Email.live' to force sending emails.
  19. * Per default it would not send mails in debug mode, but log them away.
  20. *
  21. * Additionally, you can set custom SMTP configs via Configure::read('Mail'):
  22. * - smtpHost
  23. * - smtpUsername
  24. * - smtpPassword
  25. * Those will then be merged in.
  26. *
  27. * Your email.php config file then should not contain any sensitive information and can be part of version control.
  28. */
  29. class BaseEmailConfig {
  30. public $default = array(
  31. 'transport' => 'Smtp',
  32. );
  33. /**
  34. * Read Configure Email pwds and assign them to the configs.
  35. * Also assigns custom Mail config as well as log/trace configs.
  36. */
  37. public function __construct() {
  38. if (!empty($this->default['log'])) {
  39. $this->default['report'] = true;
  40. }
  41. if (isset($this->default['log'])) {
  42. unset($this->default['log']);
  43. }
  44. if (isset($this->default['trace'])) {
  45. $this->default['log'] = 'email_trace';
  46. }
  47. if (Configure::read('debug') && !Configure::read('Email.live')) {
  48. $this->default['transport'] = 'Debug';
  49. if (!isset($this->default['trace'])) {
  50. $this->default['log'] = 'email_trace';
  51. }
  52. }
  53. if ($config = Configure::read('Mail')) {
  54. if (!empty($config['smtpHost'])) {
  55. $this->default['host'] = $config['smtpHost'];
  56. }
  57. if (!empty($config['smtpUsername'])) {
  58. $this->default['username'] = $config['smtpUsername'];
  59. }
  60. if (!empty($config['smtpPassword'])) {
  61. $this->default['password'] = $config['smtpPassword'];
  62. }
  63. if (!empty($config['smtpPort'])) {
  64. $this->default['port'] = $config['smtpPort'];
  65. }
  66. if (isset($config['smtpTimeout'])) {
  67. $this->default['timeout'] = $config['smtpTimeout'];
  68. }
  69. if (isset($config['smtpTls'])) {
  70. $this->default['tls'] = $config['smtpTls'];
  71. }
  72. }
  73. $pwds = (array)Configure::read('Email.Pwd');
  74. foreach ($pwds as $key => $val) {
  75. if (isset($this->{$key})) {
  76. $this->{$key}['password'] = $val;
  77. }
  78. }
  79. }
  80. }