Zend Framework

Zend_Mail_Transport_Sendmail creates warning if it doesnt exist instead of throwing an exception

Details

  • Type: Bug Bug
  • Status: Resolved Resolved
  • Priority: Minor Minor
  • Resolution: Fixed
  • Affects Version/s: 1.9.6
  • Fix Version/s: 1.9.7
  • Component/s: Zend_Mail
  • Labels:
    None

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

Activity

Hide
Satoru Yoshida added a comment -

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');
}
Show
Satoru Yoshida added a comment - 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');
}
Hide
R Slootjes added a comment -

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).

Show
R Slootjes added a comment - 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).
Hide
Satoru Yoshida added a comment -

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;
}
Show
Satoru Yoshida added a comment - 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;
}
Hide
R Slootjes added a comment -

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

Show
R Slootjes added a comment - I will try to test this today, i just got some tight deadlines
Hide
R Slootjes added a comment -

This seems to be working like it should:

try
{
	$objMail = new Zend_Mail();
	$objMail->setBodyHtml('<h1>Test</h1>');
	$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() [<a href='function.mail'>function.mail</a>]: Failed to connect to mailserver at &quot;localhost&quot; port 25, verify your &quot;SMTP&quot; and &quot;smtp_port&quot; setting in php.ini or use ini_set()
Show
R Slootjes added a comment - This seems to be working like it should:
try
{
	$objMail = new Zend_Mail();
	$objMail->setBodyHtml('<h1>Test</h1>');
	$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() [<a href='function.mail'>function.mail</a>]: Failed to connect to mailserver at &quot;localhost&quot; port 25, verify your &quot;SMTP&quot; and &quot;smtp_port&quot; setting in php.ini or use ini_set()
Hide
Satoru Yoshida added a comment -

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

Show
Satoru Yoshida added a comment - Thank You for cooperation, R Slootjes I reflected it at SVN trunk r19712.
Hide
Satoru Yoshida added a comment -

change to next mini. SVN r19915 in 1.9 branch

Show
Satoru Yoshida added a comment - change to next mini. SVN r19915 in 1.9 branch

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: