email.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Fuel is a fast, lightweight, community driven PHP5 framework.
  4. *
  5. * @package Fuel
  6. * @version 1.5
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2013 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Email;
  13. class AttachmentNotFoundException extends \FuelException {}
  14. class InvalidAttachmentsException extends \FuelException {}
  15. class InvalidEmailStringEncoding extends \FuelException {}
  16. class EmailSendingFailedException extends \FuelException {}
  17. class EmailValidationFailedException extends \FuelException {}
  18. class Email
  19. {
  20. /**
  21. * Instance for singleton usage.
  22. */
  23. public static $_instance = false;
  24. /**
  25. * Driver config defaults.
  26. */
  27. protected static $_defaults;
  28. /**
  29. * Email priorities
  30. */
  31. const P_LOWEST = '5 (Lowest)';
  32. const P_LOW = '4 (Low)';
  33. const P_NORMAL = '3 (Normal)';
  34. const P_HIGH = '2 (High)';
  35. const P_HIGHEST = '1 (Highest)';
  36. /**
  37. * Email driver forge.
  38. *
  39. * @param string|array $setup setup key for array defined in email.setups config or config array
  40. * @param array $config extra config array
  41. * @return Email_Driver one of the email drivers
  42. */
  43. public static function forge($setup = null, array $config = array())
  44. {
  45. empty($setup) and $setup = \Config::get('email.default_setup', 'default');
  46. is_string($setup) and $setup = \Config::get('email.setups.'.$setup, array());
  47. $setup = \Arr::merge(static::$_defaults, $setup);
  48. $config = \Arr::merge($setup, $config);
  49. $driver = '\\Email_Driver_'.ucfirst(strtolower($config['driver']));
  50. if( ! class_exists($driver, true))
  51. {
  52. throw new \FuelException('Could not find Email driver: '.$config['driver']. ' ('.$driver.')');
  53. }
  54. $driver = new $driver($config);
  55. return $driver;
  56. }
  57. /**
  58. * Init, config loading.
  59. */
  60. public static function _init()
  61. {
  62. \Config::load('email', true);
  63. static::$_defaults = \Config::get('email.defaults');
  64. }
  65. /**
  66. * Call rerouting for static usage.
  67. *
  68. * @param string $method method name called
  69. * @param array $args supplied arguments
  70. */
  71. public static function __callStatic($method, $args = array())
  72. {
  73. if(static::$_instance === false)
  74. {
  75. $instance = static::forge();
  76. static::$_instance = &$instance;
  77. }
  78. if(is_callable(array(static::$_instance, $method)))
  79. {
  80. return call_user_func_array(array(static::$_instance, $method), $args);
  81. }
  82. throw new \BadMethodCallException('Invalid method: '.get_called_class().'::'.$method);
  83. }
  84. }