Initial Kohana install
[speedfreak] / Server / system / helpers / email.php
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3  * Email helper class.
4  *
5  * $Id: email.php 3769 2008-12-15 00:48:56Z zombor $
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
14         // SwiftMailer instance
15         protected static $mail;
16
17         /**
18          * Creates a SwiftMailer instance.
19          *
20          * @param   string  DSN connection string
21          * @return  object  Swift object
22          */
23         public static function connect($config = NULL)
24         {
25                 if ( ! class_exists('Swift', FALSE))
26                 {
27                         // Load SwiftMailer
28                         require Kohana::find_file('vendor', 'swift/Swift');
29
30                         // Register the Swift ClassLoader as an autoload
31                         spl_autoload_register(array('Swift_ClassLoader', 'load'));
32                 }
33
34                 // Load default configuration
35                 ($config === NULL) and $config = Kohana::config('email');
36
37                 switch ($config['driver'])
38                 {
39                         case 'smtp':
40                                 // Set port
41                                 $port = empty($config['options']['port']) ? NULL : (int) $config['options']['port'];
42
43                                 if (empty($config['options']['encryption']))
44                                 {
45                                         // No encryption
46                                         $encryption = Swift_Connection_SMTP::ENC_OFF;
47                                 }
48                                 else
49                                 {
50                                         // Set encryption
51                                         switch (strtolower($config['options']['encryption']))
52                                         {
53                                                 case 'tls': $encryption = Swift_Connection_SMTP::ENC_TLS; break;
54                                                 case 'ssl': $encryption = Swift_Connection_SMTP::ENC_SSL; break;
55                                         }
56                                 }
57
58                                 // Create a SMTP connection
59                                 $connection = new Swift_Connection_SMTP($config['options']['hostname'], $port, $encryption);
60
61                                 // Do authentication, if part of the DSN
62                                 empty($config['options']['username']) or $connection->setUsername($config['options']['username']);
63                                 empty($config['options']['password']) or $connection->setPassword($config['options']['password']);
64
65                                 if ( ! empty($config['options']['auth']))
66                                 {
67                                         // Get the class name and params
68                                         list ($class, $params) = arr::callback_string($config['options']['auth']);
69
70                                         if ($class === 'PopB4Smtp')
71                                         {
72                                                 // Load the PopB4Smtp class manually, due to its odd filename
73                                                 require Kohana::find_file('vendor', 'swift/Swift/Authenticator/$PopB4Smtp$');
74                                         }
75
76                                         // Prepare the class name for auto-loading
77                                         $class = 'Swift_Authenticator_'.$class;
78
79                                         // Attach the authenticator
80                                         $connection->attachAuthenticator(($params === NULL) ? new $class : new $class($params[0]));
81                                 }
82
83                                 // Set the timeout to 5 seconds
84                                 $connection->setTimeout(empty($config['options']['timeout']) ? 5 : (int) $config['options']['timeout']);
85                         break;
86                         case 'sendmail':
87                                 // Create a sendmail connection
88                                 $connection = new Swift_Connection_Sendmail
89                                 (
90                                         empty($config['options']) ? Swift_Connection_Sendmail::AUTO_DETECT : $config['options']
91                                 );
92
93                                 // Set the timeout to 5 seconds
94                                 $connection->setTimeout(5);
95                         break;
96                         default:
97                                 // Use the native connection
98                                 $connection = new Swift_Connection_NativeMail($config['options']);
99                         break;
100                 }
101
102                 // Create the SwiftMailer instance
103                 return email::$mail = new Swift($connection);
104         }
105
106         /**
107          * Send an email message.
108          *
109          * @param   string|array  recipient email (and name), or an array of To, Cc, Bcc names
110          * @param   string|array  sender email (and name)
111          * @param   string        message subject
112          * @param   string        message body
113          * @param   boolean       send email as HTML
114          * @return  integer       number of emails sent
115          */
116         public static function send($to, $from, $subject, $message, $html = FALSE)
117         {
118                 // Connect to SwiftMailer
119                 (email::$mail === NULL) and email::connect();
120
121                 // Determine the message type
122                 $html = ($html === TRUE) ? 'text/html' : 'text/plain';
123
124                 // Create the message
125                 $message = new Swift_Message($subject, $message, $html, '8bit', 'utf-8');
126
127                 if (is_string($to))
128                 {
129                         // Single recipient
130                         $recipients = new Swift_Address($to);
131                 }
132                 elseif (is_array($to))
133                 {
134                         if (isset($to[0]) AND isset($to[1]))
135                         {
136                                 // Create To: address set
137                                 $to = array('to' => $to);
138                         }
139
140                         // Create a list of recipients
141                         $recipients = new Swift_RecipientList;
142
143                         foreach ($to as $method => $set)
144                         {
145                                 if ( ! in_array($method, array('to', 'cc', 'bcc')))
146                                 {
147                                         // Use To: by default
148                                         $method = 'to';
149                                 }
150
151                                 // Create method name
152                                 $method = 'add'.ucfirst($method);
153
154                                 if (is_array($set))
155                                 {
156                                         // Add a recipient with name
157                                         $recipients->$method($set[0], $set[1]);
158                                 }
159                                 else
160                                 {
161                                         // Add a recipient without name
162                                         $recipients->$method($set);
163                                 }
164                         }
165                 }
166
167                 if (is_string($from))
168                 {
169                         // From without a name
170                         $from = new Swift_Address($from);
171                 }
172                 elseif (is_array($from))
173                 {
174                         // From with a name
175                         $from = new Swift_Address($from[0], $from[1]);
176                 }
177
178                 return email::$mail->send($message, $recipients, $from);
179         }
180
181 } // End email