Ejemplo n.º 1
0
 /**
  * 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;
 }