ZF-8493: Zend_Mail_Transport_Sendmail creates warning if it doesnt exist instead of throwing an exception

Description

The _sendMail() function doesnt check if mail() is actually available. When I run it on my local XAMPP installation I don't have the mail-function available and it will return an error about that. If the mail would have the error surpressed I will get an exception. So, please place @'s before the mail() functions so I can catch the exception without getting extra errors/warnings. Simple fix I guess :)

Comments

Hi, R Slootjes.

In substitution for @ mark, could it also match Your purpose like as following code ?


if (function_exists('mail') === false) {
    require_once 'Zend/Mail/Transport/Exception.php';
    throw new Zend_Mail_Transport_Exception('Unable to use mail');
}

That won't be sufficient since this:

var_dump(function_exists('mail'));

displays

bool(true).

The function exists but it's just not configured to work (i guess).

Thank You for responce, R Slootjes. Then I will be happy if you would try the following code.


//Add property
protected $_errstr;

public function _sendMail()
{
    //Add next line
    set_error_handler(array($this, '_handleSomeErrors'));
    if ($this->parameters === null) {
        $result = mail(
            $this->recipients,
            $this->_mail->getSubject(),
            $this->body,
            $this->header);
    } else {
        $result = mail(
            $this->recipients,
            $this->_mail->getSubject(),
            $this->body,
            $this->header,
            $this->parameters);
    }
    //Add next line
    restore_error_handler();

    //Change next line
    if ($this->_errstr !== null || !$result) {
        require_once 'Zend/Mail/Transport/Exception.php';
        //Change next argument
        throw new Zend_Mail_Transport_Exception('Unable to send mail:' . $this->_errstr);
    }
}

//Add function
protected function _handleSomeErrors($errno, $errstr, $errfile = null, $errline = null, array $errcontext = null)
{
    $this->_errstr = $errstr;
    return true;
}

I will try to test this today, i just got some tight deadlines ;)

This seems to be working like it should:


try
{
    $objMail = new Zend_Mail();
    $objMail->setBodyHtml('

Test

'); $objMail->setFrom('robert@sender.com', 'Robert'); $objMail->addTo('robert@receiver.com', 'Robert'); $objMail->setSubject('Testmail'); $objMail->send(); } catch(Exception $objException) { echo 'exception: ' . $objException->getMessage(); }

returns:


exception: Unable to send mail:mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()

Thank You for cooperation, R Slootjes ;-) I reflected it at SVN trunk r19712.

change to next mini. SVN r19915 in 1.9 branch