Hi Justin and ZFers

I've published a refactored Zend_Mail_Smtp_Transport to the incubator for you to try out (and get community feedback).

I've used your code as a starting base for building a range of AUTH methods for the Mail Transport to extend.

Could you give this a try to see if it meets your needs? You'll need to include the incubator path as your primary search path.

=====

require_once 'Zend/Mail/Transport/Smtp.php';

$config = array('auth' => 'plain', // defaults to FALSE,
                'username' => 'myusername', // defaults to NULL,
                'password' => 'mypass', // defaults to NULL,
                'port' => 25, // defaults from php.ini,
                'host' => 'localhost'); // for EHLO

$tr = new Zend_Mail_Transport_Smtp('mail.someone.com', $config);

require_once 'Zend/Mail.php';

$mail = new Zend_Mail();
$mail->setBodyText('This is the text of the mail.');
$mail->setFrom('[EMAIL PROTECTED]', 'Some Sender');
$mail->addTo('[EMAIL PROTECTED]', 'Some Recipient');
$mail->setSubject('TestSubject');
$mail->send($tr);

=====

The new Zend_Mail_Client_xxx classes are the ones that handle transport negotiotion. If 'auth' is passed as a config option, Zend_Mail_Transport_Smtp tries to load the associated class (e.g. 'auth' => 'login' with load Zend_Mail_Client_Smtp_Auth_Login) and handles the authentication immediately after the 'HELO' command is issued.

The reasoning behind this implementation was that additional AUTH methods could be implemented without enforcing a non-flexible signature for the Zend_Mail_Client_Smtp::auth() method. It simply gets passed the appropriate $config array and can extract the parameters it needs to.

So PLAIN and LOGIN will use 'username' and 'password' but CRAM-MD5 (yet to be implemented) can pass across the 'secret', 'username' and then parse the remote server's response to the challenge and authenticate properly from there.

Hopefully this will add enough flexibility for everyone's uses. I think TLS would also be a worthy addition to the class.

The base Zend_Mail_Client class has been added to allow other specific clients to be written (e.g. POP3) and relieve the transport classes of connection/negotiation duties.

I look forward to receiving feedback from interested parties - Matthew is the component lead and will obviously have a big say in the appropriateness of the class and implementation. Hopefully this is a step in the right direction.

Kind regards

I wrote this a few weeks back and it seems to be working alright. It only supports AUTH LOGIN and AUTH PLAIN though.

<?php
require_once 'Zend/Mail/Transport/Smtp.php';

class My_Zend_Mail_Transport_Smtp_Auth extends Zend_Mail_Transport_Smtp {

    /[EMAIL PROTECTED]
     * Authentication types
     * @var string
     */
    const LOGIN = 'LOGIN';
    const PLAIN = 'PLAIN';
    /[EMAIL PROTECTED]/

    /**
     * @param string $username
     * @param string $password
     * @param string $method
     */
protected function authenticate($username, $password, $method = self::PLAIN) {
        switch($method) {
            case self::LOGIN:
                $this->authenticateLogin($username, $password);
            break;

            case self::PLAIN:
                $this->authenticatePlain($username, $password);
            break;
        }
    }

    /**
     * @param  string $username
     * @param  string $password
     * @throws Zend_Mail_Transport_Exception
     */
    protected function authenticateLogin($username, $password) {
        $this->_send('AUTH LOGIN');

        try {
            $this->_expect(334);
        } catch(Zend_Mail_Transport_Exception $e) {
            if(substr($e->getMessage(), 0, 3) == 503) {
                return;
            }
            throw $e;
        }

        $this->_send(base64_encode($username));
        $this->_expect(334);
        $this->_send(base64_encode($password));
        $this->_expect(235);
    }

    /**
     * @param  string $username
     * @param  string $password
     * @throws Zend_Mail_Transport_Exception
     */
    protected function authenticatePlain($username, $password) {
        $this->_send('AUTH PLAIN');

        try {
            $this->_expect(334);
        } catch(Zend_Mail_Transport_Exception $e) {
            if(substr($e->getMessage(), 0, 3) == 503) {
                return;
            }
            throw $e;
        }

$this->_send(base64_encode(chr(0) . $username . chr(0) . $password));
        $this->_expect(235);
    }

}

--

Simon Mundy | Director | PEPTOLAB

""" " "" """""" "" "" """"""" " "" """"" " """"" "  """""" "" "
202/258 Flinders Lane | Melbourne | Victoria | Australia | 3000
Voice +61 (0) 3 9654 4324 | Mobile 0438 046 061 | Fax +61 (0) 3 9654 4124
http://www.peptolab.com


Reply via email to