Search code examples
phpzend-frameworkzend-mail

Zend framework 1 Zend_Mail setFrom name will shortened or deleted


hi i will send a normal email with zend mail via smtp. It works fine. But the 'from' must but the fromemail should look like this :

$mail->setFrom('[email protected]', '[email protected]');

my goal is to have something like that in the email programm:

"[email protected]" <[email protected]> 

but I only see this:

 <[email protected]> 

or something like that

"[email protected]" <[email protected]> 

result:

"firtstudio@" <[email protected]> 

I need the full name. how?


Solution

  • If you look at Zend_Mail, the setFrom uses an internal function named _formatAddress to set the display name. The function goes as follows:

    /**
         * Formats e-mail address
         *
         * @param string $email
         * @param string $name
         * @return string
         */
        protected function _formatAddress($email, $name)
        {
            if ($name === '' || $name === null || $name === $email) {
                return $email;
            } else {
                $encodedName = $this->_encodeHeader($name);
                if ($encodedName === $name  &&  strcspn($name, '()<>[]:;@\\,') != strlen($name)) {
                    $format = '"%s" <%s>';
                } else {
                    $format = '%s <%s>';
                }
                return sprintf($format, $encodedName, $email);
            }
        }
    

    As you can see, if the email and name are the same, the function will only return the email. Here are some things you could try to display it:

    $mailer->setFrom('[email protected]', 'Firt Studio');
    

    would display it as Firt Studio

    or you could edit the _formatAddress function in the Zend_Mailer library to remove the

     || $name === $email
    

    condition and you would get

    [email protected] <[email protected]>