Avail feature updated
[ninja.git] / system / helpers / email.php
blob518060c2ac50f678d1ce57e31af8c5b18b905465
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3 * Email helper class.
5 * $Id: email.php 3917 2009-01-21 03:06:22Z zombor $
7 * @package Core
8 * @author Kohana Team
9 * @copyright (c) 2007-2008 Kohana Team
10 * @license http://kohanaphp.com/license.html
12 class email {
14 // SwiftMailer instance
15 protected static $mail;
17 /**
18 * Creates a SwiftMailer instance.
20 * @param string DSN connection string
21 * @return object Swift object
23 public static function connect($config = NULL)
25 if ( ! class_exists('Swift', FALSE))
27 // Load SwiftMailer
28 require Kohana::find_file('vendor', 'swift/Swift');
30 // Register the Swift ClassLoader as an autoload
31 spl_autoload_register(array('Swift_ClassLoader', 'load'));
34 // Load default configuration
35 ($config === NULL) and $config = Kohana::config('email');
37 switch ($config['driver'])
39 case 'smtp':
40 // Set port
41 $port = empty($config['options']['port']) ? NULL : (int) $config['options']['port'];
43 if (empty($config['options']['encryption']))
45 // No encryption
46 $encryption = Swift_Connection_SMTP::ENC_OFF;
48 else
50 // Set encryption
51 switch (strtolower($config['options']['encryption']))
53 case 'tls': $encryption = Swift_Connection_SMTP::ENC_TLS; break;
54 case 'ssl': $encryption = Swift_Connection_SMTP::ENC_SSL; break;
58 // Create a SMTP connection
59 $connection = new Swift_Connection_SMTP($config['options']['hostname'], $port, $encryption);
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']);
65 if ( ! empty($config['options']['auth']))
67 // Get the class name and params
68 list ($class, $params) = arr::callback_string($config['options']['auth']);
70 if ($class === 'PopB4Smtp')
72 // Load the PopB4Smtp class manually, due to its odd filename
73 require Kohana::find_file('vendor', 'swift/Swift/Authenticator/$PopB4Smtp$');
76 // Prepare the class name for auto-loading
77 $class = 'Swift_Authenticator_'.$class;
79 // Attach the authenticator
80 $connection->attachAuthenticator(($params === NULL) ? new $class : new $class($params[0]));
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
90 empty($config['options']) ? Swift_Connection_Sendmail::AUTO_DETECT : $config['options']
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;
102 // Create the SwiftMailer instance
103 return email::$mail = new Swift($connection);
107 * Send an email message.
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
116 public static function send($to, $from, $subject, $message, $html = FALSE)
118 // Connect to SwiftMailer
119 (email::$mail === NULL) and email::connect();
121 // Determine the message type
122 $html = ($html === TRUE) ? 'text/html' : 'text/plain';
124 // Create the message
125 $message = new Swift_Message($subject, $message, $html, '8bit', 'utf-8');
127 if (is_string($to))
129 // Single recipient
130 $recipients = new Swift_Address($to);
132 elseif (is_array($to))
134 if (isset($to[0]) AND isset($to[1]))
136 // Create To: address set
137 $to = array('to' => $to);
140 // Create a list of recipients
141 $recipients = new Swift_RecipientList;
143 foreach ($to as $method => $set)
145 if ( ! in_array($method, array('to', 'cc', 'bcc')))
147 // Use To: by default
148 $method = 'to';
151 // Create method name
152 $method = 'add'.ucfirst($method);
154 if (is_array($set))
156 // Add a recipient with name
157 $recipients->$method($set[0], $set[1]);
159 else
161 // Add a recipient without name
162 $recipients->$method($set);
167 if (is_string($from))
169 // From without a name
170 $from = new Swift_Address($from);
172 elseif (is_array($from))
174 // From with a name
175 $from = new Swift_Address($from[0], $from[1]);
178 return email::$mail->send($message, $recipients, $from);
181 } // End email