<?php
/**
 * @package Am_Mail
 */
class Am_Mail_Transport_Smtp extends Zend_Mail_Transport_Smtp implements Am_Mail_Transport_Iface
{
    public function _sendMail()
    {
        $_ = parent::_sendMail();
        $this->getConnection()->resetLog(); // workaround for memory overuse
        if (!empty($this->_config['new_conn'])) {
            try {
                $this->getConnection()->quit();
                $this->getConnection()->disconnect();
            } catch (Exception $e) {
                // ignore disconnect errors
            }
            $this->_connection = null;
        }
        return $_;
    }

    function sendFromSaved($from, $recipients, $body, array $headers, $subject, $EOL)
    {
        if ($EOL !== $this->EOL)
        {
            $my_eol = $EOL;
            $t_eol = $this->EOL;
            $body = str_replace($my_eol, $t_eol, $body);
            $headers = array_map(
                function ($_) use ($my_eol, $t_eol) {
                    return str_replace($my_eol, $t_eol, $_);
                },
                $headers
            );
        }

        $this->_mail = new Am_Mail_Saved;
        $this->_mail->from = $from;
        $this->_mail->subject = $subject;
        $this->_mail->recipients = explode(',', $recipients);
        $this->recipients = $recipients;
        $this->body = $body;
        $this->_prepareHeaders($headers);
        $this->_sendMail();
    }
}