ZF-11371: Exception in case of optional Headers

Description

Part.php throws an exception in case of the optional header of "Subject" is not seen in the list of headers of a mail file in the Maildir type of mailboxes.

The functions of getHeader() or headerExists() should handle this gracefully by ignoring it all these optional headers. Subject header is an optional field as described in RFC 2822.


PHP Fatal error:  Uncaught exception 'Zend_Mail_Exception' with message 'no Header with Name subject or subject found' in /usr/share/php/Zend/Mail/Part.php:347
Stack trace:
#0 /usr/share/php/Zend/Mail/Part.php(418): Zend_Mail_Part->getHeader('subject', 'string')
#1 /var/www/email/mail_parser.php(247): Zend_Mail_Part->__get('subject')
#2 {main}
  thrown in /usr/share/php/Zend/Mail/Part.php on line 347

Comments

Part of the reason that Subject is considered a required field is due to how mail() works in PHP; subject is a required parameter of that method. As such, if you're using the Sendmail Zend_Mail transport, we have to require it.

One option would be to have it default to an empty string, however.

Can this be considered just a documentation issue? (ie: informing developers that subject is a required field despite RFC2822) Or should the default-value behavior be implemented?

This is not a problem when sending mail but when receiving it. Mails will come in without a subject and accessing $this->subject raises an Exception.

Bump for this... It's not a matter of sending, but of receiving: if you're trying to do any work with reading contents of a mail folder and somewhere in there is an email without a subject, the exception will cause things to crash out...

I was reading emails in a loop, this crashed my application. This should not throw an exception.

For anyone who needs a quick fix, you can do this:

try { $data['subject'] = $message->subject; } catch(Exception $e) { $data['subject'] = ''; }