1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
6 * copyright (c) 2007-2008 Kohana Team
7 * license http://kohanaphp.com/license.html
11 /// SwiftMailer instance
12 protected static $mail;
15 * Creates a SwiftMailer instance.
17 * @param $config DSN connection string
18 * @return Swift object
20 public static function connect($config = NULL)
22 if ( ! class_exists('Swift', FALSE))
25 require_once Kohana
::find_file('vendor', 'swiftmailer/swift_required');
28 // Load default configuration
29 ($config === NULL) and $config = Kohana
::config('email');
31 switch ($config['driver'])
35 $port = empty($config['options']['port']) ?
NULL : (int) $config['options']['port'];
37 // Create a SMTP connection
38 $connection = Swift_SmtpTransport
::newInstance( $config['options']['hostname'], $port );
40 if (!empty($config['options']['encryption']))
43 switch (strtolower($config['options']['encryption']))
47 $connection->setEncryption( $config['options']['encryption'] );
52 // Do authentication, if part of the DSN
53 empty($config['options']['username']) or $connection->setUsername($config['options']['username']);
54 empty($config['options']['password']) or $connection->setPassword($config['options']['password']);
56 if ( ! empty($config['options']['auth']))
58 // Get the class name and params
59 list ($class, $params) = arr
::callback_string($config['options']['auth']);
61 if ($class === 'PopB4Smtp')
63 // Load the PopB4Smtp class manually, due to its odd filename
64 require Kohana
::find_file('vendor', 'swift/Swift/Authenticator/$PopB4Smtp$');
67 // Prepare the class name for auto-loading
68 $class = 'Swift_Authenticator_'.$class;
70 // Attach the authenticator
71 $connection->attachAuthenticator(($params === NULL) ?
new $class : new $class($params[0]));
74 // Set the timeout to 5 seconds
75 $connection->setTimeout(empty($config['options']['timeout']) ?
5 : (int) $config['options']['timeout']);
78 // Create a sendmail connection
79 $connection = Swift_SendmailTransport
::newInstance( $config['options'] );
82 // Use the native connection
83 $connection = Swift_MailTransport
::newInstance();
87 // Create the SwiftMailer instance
88 return email
::$mail = Swift_Mailer
::newInstance($connection);
92 * Send an email message.
94 * @param $to recipient email (and name), or an array of To, Cc, Bcc names
95 * @param $from sender email (and name)
96 * @param $subject message subject
97 * @param $body message body
98 * @param $html send email as HTML
99 * @return number of emails sent
101 public static function send($to, $from, $subject, $body, $html = FALSE)
103 // Connect to SwiftMailer
104 (email
::$mail === NULL) and email
::connect();
106 // Determine the message type
107 $html = ($html === TRUE) ?
'text/html' : 'text/plain';
109 // Create the message
110 $message = Swift_Message
::newInstance($subject);
111 $message->setBody( $body, $html );
116 $recipients = $message->setTo($to);
118 elseif (is_array($to))
120 if (isset($to[0]) AND isset($to[1]))
122 // Create To: address set
123 $to = array('to' => $to);
126 foreach ($to as $method => $set)
128 if ( ! in_array($method, array('to', 'cc', 'bcc')))
130 // Use To: by default
134 // Create method name
135 $method = 'add'.ucfirst($method);
139 // Add a recipient with name
140 $message->$method($set[0], $set[1]);
144 // Add a recipient without name
145 $message->$method($set);
150 if (is_string($from))
152 // From without a name
153 $from = $message->setFrom($from);
155 elseif (is_array($from))
158 $from = $message->setFrom( array($from[0] => $from[1]) );
161 return email
::$mail->send($message);
167 public static function send_report($to, $from, $subject, $content, $mime, $filename, $data)
169 // Connect to SwiftMailer
170 (email
::$mail === NULL) and email
::connect();
172 // Create the message
173 $message = Swift_Message
::newInstance($subject);
174 $message->setBody($content, 'text/plain');
175 $message->attach(Swift_Attachment
::newInstance($data, $filename, $mime));
177 $message->setTo($to);
178 $message->setFrom($from);
180 return email
::$mail->send($message);