ZF-3689: Recycle a Zend_Mail object and allow setting a new from

Description


/**
     * Sets From-header and sender of the message
     *
     * @param  string    $email
     * @param  string    $name
     * @return Zend_Mail Provides fluent interface
     * @throws Zend_Mail_Exception if called subsequent times
     */
    public function setFrom($email, $name = '')
    {
        if ($this->_from === null) {
            $email = strtr($email,"\r\n\t",'???');
            $this->_from = $email;
            $this->_storeHeader('From', $this->_encodeHeader('"'.$name.'"').' <'.$email.'>', true);
        } else {
            /**
             * @see Zend_Mail_Exception
             */
            require_once 'Zend/Mail/Exception.php';
            throw new Zend_Mail_Exception('From Header set twice');
        }
        return $this;
    }

Currently it will always throw an exception. I don't see why I cannot set another from if I am try to send out multiple emails in a single run but with different froms.

Comments

I managed to get around this issue by extending Zend_Mail, added my own methods for setFrom() and setSubject. In setFrom, you have to make sure to also unset $this->_headers['From'], otherwise your email ends up with two From headers, which also works, but some MUAs screw up.

It would be nice if this made it into Zend_Mail though, I can provide patches if wanted.

I think it depends on ZF-1626

I think clearFrom() function will solve your problem.

I add the function at SVN r13499 .