1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
5 * $Id: email.php 3917 2009-01-21 03:06:22Z zombor $
9 * @copyright (c) 2007-2008 Kohana Team
10 * @license http://kohanaphp.com/license.html
14 // SwiftMailer instance
15 protected static $mail;
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))
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'])
41 $port = empty($config['options']['port']) ?
NULL : (int) $config['options']['port'];
43 if (empty($config['options']['encryption']))
46 $encryption = Swift_Connection_SMTP
::ENC_OFF
;
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']);
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);
97 // Use the native connection
98 $connection = new Swift_Connection_NativeMail($config['options']);
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');
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
151 // Create method name
152 $method = 'add'.ucfirst($method);
156 // Add a recipient with name
157 $recipients->$method($set[0], $set[1]);
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))
175 $from = new Swift_Address($from[0], $from[1]);
178 return email
::$mail->send($message, $recipients, $from);