Issues

ZF-2043: Unappealing From header

Description

When a letter is sent and the message does not provide a name for the sender then the From-header is set with empty brackets.


$mail->setFrom('adress@mail.ca');

Results as From : "" adress@mail.ca

One would expect the from-header not to include empty brackets "".

The setFrom function of the Zend_Mail class should be changed as follows



/**
* Sets From-header and sender of the message
*
* @param  string    $email
* @param  string    $name     optional
* @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;

            if ($name != '') {
                $name = '"' . $this->_encodeHeader($name) . '" ';
            }

            $this->_storeHeader('From', $name .' <'.$email.'>', true);
        } else {
            throw new Zend_Mail_Exception('From Header set twice');
        }
        return $this;
}

Comments

Assigned to Nico

Solved in SVN r12370

As the second parameter is optional I suggest setting $name = null and verifying whether $name === null to be conform with "PHP Coding Standard (draft) -> Optional Parameters".

One more thing: if name is given you're currently adding two spaces between name and email address. A possible variant, IMO also easier to read could be:

public function setFrom($email, $name = null)
{
    if ($this->_from === null) {
        $email = strtr($email,"\r\n\t",'???');
        $this->_from = $email;

        if (null === $name) {
            $from = sprintf('"%s" ', $this->_encodeHeader($name));
        } else {
            $from = '';
        }
        $from .= sprintf('<%s>', (string) $email);
        $this->_storeHeader('From', $from, true);
    } else {
        ...

Cheers, Thomas

Sorry for nitpicking, could not resist ;-)

Hello, Thomas. You are welcome.;-) Thank You for Your advice.

I refixed as following.

1) If $name is not used or the $name is same as $email, header is "From: $email".

2) If $name is used and the $name is encoded, header is "From: $name <$email>".

3) If $name is used and the $name is not encoded and not contains comma, header is "From: $name <$email>".

4) If $name is used and the $name is not encoded and contains comma, header is "From: "$name" <$email>".

Best Regards, Satoru Yoshida

Great. One last note: Wouln't 1) better be From: <$email> ?

Regards, Thomas Gelf

Sorry, my fault. "From: user@domain.tld" is correct.

Hi, Thomas.

Thank You for checking my comment.

Yes, both "From: user@domain.tld" and "From: user@domain.tld" are correct.

I selected "From: user@domain.tld" on implementation. :-)

Changing issues in preparation for the 1.7.0 release.