/** * Creates a SwiftMailer instance. * * @param string DSN connection string * @return object Swift object */ public static function connect($config = NULL) { if (!class_exists('Swift', FALSE)) { // Load SwiftMailer require Kohana::find_file('vendor', 'swift/Swift'); // Register the Swift ClassLoader as an autoload spl_autoload_register(array('Swift_ClassLoader', 'load')); } // Load default configuration $config === NULL and $config = Kohana::config('email'); switch ($config['driver']) { case 'smtp': // Set port $port = empty($config['options']['port']) ? NULL : (int) $config['options']['port']; if (empty($config['options']['encryption'])) { // No encryption $encryption = Swift_Connection_SMTP::ENC_OFF; } else { // Set encryption switch (strtolower($config['options']['encryption'])) { case 'tls': $encryption = Swift_Connection_SMTP::ENC_TLS; break; case 'ssl': $encryption = Swift_Connection_SMTP::ENC_SSL; break; } } // Create a SMTP connection $connection = new Swift_Connection_SMTP($config['options']['hostname'], $port, $encryption); // Do authentication, if part of the DSN empty($config['options']['username']) or $connection->setUsername($config['options']['username']); empty($config['options']['password']) or $connection->setPassword($config['options']['password']); if (!empty($config['options']['auth'])) { // Get the class name and params list($class, $params) = arr::callback_string($config['options']['auth']); if ($class === 'PopB4Smtp') { // Load the PopB4Smtp class manually, due to its odd filename require Kohana::find_file('vendor', 'swift/Swift/Authenticator/$PopB4Smtp$'); } // Prepare the class name for auto-loading $class = 'Swift_Authenticator_' . $class; // Attach the authenticator $connection->attachAuthenticator($params === NULL ? new $class() : new $class($params[0])); } // Set the timeout to 5 seconds $connection->setTimeout(empty($config['options']['timeout']) ? 5 : (int) $config['options']['timeout']); break; case 'sendmail': // Create a sendmail connection $connection = new Swift_Connection_Sendmail(empty($config['options']) ? Swift_Connection_Sendmail::AUTO_DETECT : $config['options']); // Set the timeout to 5 seconds $connection->setTimeout(5); break; default: // Use the native connection $connection = new Swift_Connection_NativeMail($config['options']); break; } // Create the SwiftMailer instance return email::$mail = new Swift($connection); }
/** * Builds a Swift_Connection depending on the type (native, smtp, sendmail, multi, rotator) * Params depend on the connection type * * - native: * additional_params * * - smtp: * server (*) * port * encryption (SSL, TLS, or OFF) * authentication: * username (*) * password * timeout * requires_ehlo * * - sendmail: * command * flags * timeout * requires_ehlo * * - multi: * connections: * connection_name1: * type * params * connection_name2: * type * params * etc... * requires_ehlo * * - rotator: * connections: * connection_name1: * type * params * connection_name2: * type * params * etc... * requires_ehlo * * (*) Mandatory ! * * @param string $type * @param array $params * @return Swift_Connection */ protected static function getConnection($type, $params = array()) { switch ($type) { case 'native': $connection = new Swift_Connection_NativeMail(); if (@$params['additional_params']) { $connection->setAdditionalMailParams($params['additional_params']); } break; case 'smtp': if (!@$params['encryption']) { $params['encryption'] = 'OFF'; } $encryption = constant('Swift_Connection_SMTP::ENC_' . $params['encryption']); $connection = new Swift_Connection_SMTP($params['server'], @$params['port'], $encryption); if (@$params['authentication']) { $connection->setUsername(@$params['authentication']['username']); $connection->setPassword(@$params['authentication']['password']); } if (@$params['timeout']) { $connection->setTimeout($params['timeout']); } if (@$params['requires_ehlo']) { $connection->setRequiresEHLO(true); } break; case 'sendmail': $connection = new Swift_Connection_Sendmail(); if (@$params['command']) { $connection->setCommand($params['command']); } if (@$params['flags']) { $connection->setFlags($params['flags']); } if (@$params['timeout']) { $connection->setTimeout($params['timeout']); } if (@$params['requires_ehlo']) { $connection->setRequiresEHLO(true); } break; case 'multi': $connection = new Swift_Connection_Multi(); foreach ($params['connections'] as $id => $conn_info) { $connection->addConnection(self::getConnection($conn_info['type'], $conn_info['params'])); } if (@$params['requires_ehlo']) { $connection->setRequiresEHLO(true); } break; case 'rotator': $connection = new Swift_Connection_Multi(); foreach ($params['connections'] as $id => $conn_info) { $connection->addConnection(self::getConnection($conn_info['type'], $conn_info['params'])); } if (@$params['requires_ehlo']) { $connection->setRequiresEHLO(true); } break; } return $connection; }