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
Posted by Thomas Weidner (thomas) on 2007-10-15T14:04:49.000+0000
Assigned to Nico
Posted by old of Satoru Yoshida (yoshida@zend.co.jp) on 2008-11-07T08:22:05.000+0000
Solved in SVN r12370
Posted by Thomas Gelf (tgelf) on 2008-11-07T08:46:20.000+0000
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 ;-)
Posted by old of Satoru Yoshida (yoshida@zend.co.jp) on 2008-11-07T10:51:30.000+0000
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
Posted by Thomas Gelf (tgelf) on 2008-11-07T12:51:13.000+0000
Great. One last note: Wouln't 1) better be From: <$email> ?
Regards, Thomas Gelf
Posted by Thomas Gelf (tgelf) on 2008-11-07T15:16:15.000+0000
Sorry, my fault. "From: user@domain.tld" is correct.
Posted by old of Satoru Yoshida (yoshida@zend.co.jp) on 2008-11-07T17:44:35.000+0000
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. :-)
Posted by Wil Sinclair (wil) on 2008-11-13T14:10:14.000+0000
Changing issues in preparation for the 1.7.0 release.