PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/system/helpers/email.php

https://github.com/MHordecki/milionkostek
PHP | 136 lines | 65 code | 22 blank | 49 comment | 6 complexity | 19d26d2f1a03bf56eec63c5a7a90f494 MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Email helper class.
  4. *
  5. * $Id: email.php 2748 2008-06-05 21:01:03Z Shadowhand $
  6. *
  7. * @package Core
  8. * @author Kohana Team
  9. * @copyright (c) 2007-2008 Kohana Team
  10. * @license http://kohanaphp.com/license.html
  11. */
  12. class email_Core {
  13. // SwiftMailer instance
  14. protected static $mail;
  15. /**
  16. * Creates a SwiftMailer instance.
  17. *
  18. * @param string DSN connection string
  19. * @return object Swift object
  20. */
  21. public static function connect($config = NULL)
  22. {
  23. if ( ! class_exists('Swift', FALSE))
  24. {
  25. // Load SwiftMailer
  26. require_once Kohana::find_file('vendor', 'swift/Swift');
  27. // Register the Swift ClassLoader as an autoload
  28. spl_autoload_register(array('Swift_ClassLoader', 'load'));
  29. }
  30. // Load default configuration
  31. ($config === NULL) and $config = Config::item('email');
  32. switch ($config['driver'])
  33. {
  34. case 'smtp':
  35. // Set port
  36. $port = empty($config['options']['port']) ? 25 : (int) $config['options']['port'];
  37. if (empty($config['options']['encryption']))
  38. {
  39. // No encryption
  40. $encryption = Swift_Connection_SMTP::ENC_OFF;
  41. }
  42. else
  43. {
  44. // Set encryption
  45. switch (strtolower($config['options']['encryption']))
  46. {
  47. case 'tls': $encryption = Swift_Connection_SMTP::ENC_TLS; break;
  48. case 'ssl': $encryption = Swift_Connection_SMTP::ENC_SSL; break;
  49. }
  50. }
  51. // Create a SMTP connection
  52. $connection = new Swift_Connection_SMTP($config['options']['hostname'], $port, $encryption);
  53. // Do authentication, if part of the DSN
  54. empty($config['options']['username']) or $connection->setUsername($config['options']['username']);
  55. empty($config['options']['password']) or $connection->setPassword($config['options']['password']);
  56. if ( ! empty($config['options']['auth']))
  57. {
  58. // Get the class name and params
  59. list ($class, $params) = arr::callback_string($config['options']['auth']);
  60. if ($class === 'PopB4Smtp')
  61. {
  62. // Load the PopB4Smtp class manually, due to it's odd filename
  63. require_once Kohana::find_file('vendor', 'swift/Swift/Authenticator/$PopB4Smtp$');
  64. }
  65. // Prepare the class name for auto-loading
  66. $class = 'Swift_Authenticator_'.$class;
  67. // Attach the authenticator
  68. $connection->attachAuthenticator(($params === NULL) ? new $class : new $class($params[0]));
  69. }
  70. // Set the timeout to 5 seconds
  71. $connection->setTimeout(empty($config['options']['timeout']) ? 5 : (int) $config['options']['timeout']);
  72. break;
  73. case 'sendmail':
  74. // Create a sendmail connection
  75. $connection = new Swift_Connection_Sendmail
  76. (
  77. empty($config['options']) ? Swift_Connection_Sendmail::AUTO_DETECT : $config['options']
  78. );
  79. // Set the timeout to 5 seconds
  80. $connection->setTimeout(5);
  81. break;
  82. default:
  83. // Use the native connection
  84. $connection = new Swift_Connection_NativeMail;
  85. break;
  86. }
  87. // Create the SwiftMailer instance
  88. return email::$mail = new Swift($connection);
  89. }
  90. /**
  91. * Send an email message.
  92. *
  93. * @param string|array recipient email (and name)
  94. * @param string|array sender email (and name)
  95. * @param string message subject
  96. * @param string message body
  97. * @param boolean send email as HTML
  98. * @return integer number of emails sent
  99. */
  100. public static function send($to, $from, $subject, $message, $html = FALSE)
  101. {
  102. // Connect to SwiftMailer
  103. (email::$mail === NULL) and email::connect();
  104. // Determine the message type
  105. $html = ($html === TRUE) ? 'text/html' : 'text/plain';
  106. // Create the message
  107. $message = new Swift_Message($subject, $message, $html, '8bit', 'utf-8');
  108. // Make a personalized To: address
  109. is_object($to) or $to = is_array($to) ? new Swift_Address($to[0], $to[1]) : new Swift_Address($to);
  110. // Make a personalized From: address
  111. is_object($from) or $from = is_array($from) ? new Swift_Address($from[0], $from[1]) : new Swift_Address($from);
  112. return email::$mail->send($message, $to, $from);
  113. }
  114. } // End email