PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/system/helpers/email.php

https://github.com/Toushi/flow
PHP | 181 lines | 99 code | 26 blank | 56 comment | 11 complexity | 559587a3e56269569185711e204c9764 MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Email helper class.
  4. *
  5. * $Id: email.php 3213 2008-07-27 14:28:37Z Geert $
  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 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 = Kohana::config('email');
  32. switch ($config['driver'])
  33. {
  34. case 'smtp':
  35. // Set port
  36. $port = empty($config['options']['port']) ? NULL : (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 its odd filename
  63. require 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), or an array of To, Cc, Bcc names
  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. if (is_string($to))
  109. {
  110. // Single recipient
  111. $recipients = new Swift_Address($to);
  112. }
  113. elseif (is_array($to))
  114. {
  115. if (isset($to[0]) AND isset($to[1]))
  116. {
  117. // Create To: address set
  118. $to = array('to' => $to);
  119. }
  120. // Create a list of recipients
  121. $recipients = new Swift_RecipientList;
  122. foreach ($to as $method => $set)
  123. {
  124. if ( ! in_array($method, array('to', 'cc', 'bcc')))
  125. {
  126. // Use To: by default
  127. $method = 'to';
  128. }
  129. // Create method name
  130. $method = 'add'.ucfirst($method);
  131. if (is_array($set))
  132. {
  133. // Add a recipient with name
  134. $recipients->$method($set[0], $set[1]);
  135. }
  136. else
  137. {
  138. // Add a recipient without name
  139. $recipients->$method($set);
  140. }
  141. }
  142. }
  143. if (is_string($from))
  144. {
  145. // From without a name
  146. $from = new Swift_Address($from);
  147. }
  148. elseif (is_array($from))
  149. {
  150. // From with a name
  151. $from = new Swift_Address($from[0], $from[1]);
  152. }
  153. return email::$mail->send($message, $recipients, $from);
  154. }
  155. } // End email