<?php
/**
 * A class for sending emails using smtp.
 *
 * This class is a thin wrapper over swift smtp transport.
 */
class email_transport_smtp extends email_transport_swift
{
    /**
     * @const string The security methods.
     */
    const SSL = 'ssl';
    const TLS = 'tls';
    
    ////////////////////////////////////////////////////////////////////////////
    // Construct and destruct functions.

    /**
     * @param string (optional) $host The smtp host.
     * @param integer (optional) $port The port.
     * @param string (optional) $security The encryption method.
     * @param string (optional) $username The username.
     * @param string (optional) $password The password.
     * @throws outofrangeexception If the security method does not exist or is
     * not supported.
     */
    public function __construct($host = 'localhost', $port = 25, $security = null, $username = null, $password = null)
    {
        $this->transport = new Swift_SmtpTransport($host, $port);
        
        if ($security !== null)
        {
            if ($security !== self::SSL && $security !== self::TLS)
            {
                throw new outofrangeexception('Security method ' . $security . ' does not exist.');
            }
            
            if (!in_array($security, stream_get_transports(), true))
            {
                throw new outofrangeexception('Transport ' . $security . ' is not supported.');
            }
            
            $this->transport->setEncryption($security);
        }
        
        if ($username !== null)
        {
            $this->transport->setUsername($username)
                            ->setPassword($password);
        }
    }

    ////////////////////////////////////////////////////////////////////////////
    // Overload functions.

    ////////////////////////////////////////////////////////////////////////////
    // Interface functions.
    
    ////////////////////////////////////////////////////////////////////////////
    // Internal functions.
}
