Issues

ZF2-185: LF/CR statically given in \Zend\Mail\Headers.php::toString()

Description

Hi!

In \Zend\Mail\Headers and its toString() function are two occurrences of "\r\n".

This breaks mails by adding a blank line between header lines. In my case removing the CR helped (i was using a CentOS VM and SmtpTransport).

Would you please check if it is useful to switch LF vs CR+LF based on the OS in use? I would prefer the use of __toString() instead of toString(), too.

Thx for having a look at it. Jade

Comments

Can you demonstrate a case where this breaks (not just OS)? I.e., what is the actual output?

The mail RFCs specify that headers are supposed to be separated using the combination of a CR and a LF ("\r\n"). Changing this would be very problematic, and I'd argue something to do in specialized transports only.

Hi! I have absolutely no clue if this is related to a specific transport. What i can tell:

When i use the "\r\n" part ( $headers .= $fieldName . ': ' . $value . "\r\n"; ) from the method in question, my mail client (Thunderbird, current and last few verions) shows the following mail source code and the output is visually broken:


X-Envelope-From: sender.mail@address.com X-Envelope-To: recipient.mail@address.com X-Delivery-Time: 1336052195 X-UID: 6720 Return-Path: sender.mail@address.com X-RZG-CLASS-ID: mi Received: from snip.tld ([IPv6:snip]) by snip.tld (jored mi19) (RZmta 29.1 OK) with ESMTP id snip for recipient.mail@address.com; Thu, 3 May 2012 15:36:35 +0200 (CEST) X-RZG-AUTH: :snip To: recipient.mail@address.com From: sender.mail@address.com Message-ID: <snip.tld> X-RZG-CLASS-ID: mo00 Received: from Some-Mail (snip.tld [snip]) by smtp.snip.tld (josoe mo29) (RZmta 29.1 DYNA|AUTH) with ESMTPA id snip for recipient.mail@address.com; Thu, 3 May 2012 15:36:35 +0200 (CEST) Date: Fri, 04 May 2012 00:36:50 +0200

Subject: super-important subject

From: Sender Name sender.mail@address.com

To: Recipient Name recipient.mail@address.com

Mime-Version: 1.0

Content-Type: text/html; charset="UTF-8"

Content-Transfer-Encoding: 8bit

Sender:

Cc:

snip


When i switch to a modified version where i use only "\n\ ( $headers .= $fieldName . ': ' . $value . "\n"; ), the same mail client shows the following mail source code (only relevant changes) - and this email displays well:


Date: Fri, 04 May 2012 00:36:50 +0200 Subject: super-important subject From: Sender Name sender.mail@address.com To: Recipient Name recipient.mail@address.com Mime-Version: 1.0 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: 8bit Sender: Cc:

snip


A few lines of code from my mail functions:


    protected function sendRegistrationMail(){
        try {
            $transport = $this->getTransport( array('transport' => 'smtp') );
            $message = $this->buildMessage();
            $transport->send($message);     
        } 
        catch (\Exception $e) {
            throw new RuntimeException('unable to send registration mail');
        }
    }

and


    private function buildMessage(){
        $message = new Message();
        $message->setSubject($this->_email_subject);
        $message->setFrom($this->_email_sender);
        $message->addTo($this->_email_recipient);
        // get mail content, html with valid markup
        $content = $this->getMessageBody($this->_template_file, $this->_template_vars);
        // build MIME message & parts
        $mimeMessage = new MimeMessage();
        $htmlPart = new MimePart($content);
        $htmlPart->type = Mime::TYPE_HTML;
        $htmlPart->charset = "UTF-8"; 
        $mimeMessage->setParts( array( $htmlPart ) );
            // set MIME Message as mail body
        $message->setBody($mimeMessage);
        return $message;
    }   

I can't tell (and have no quick idea on how to investigate) why the blank lines are inserted in the first and broken case. If you want me to give more details, please specify - i'll do my best.

Best regards, Jade

What is your MTA (Sendmail, Postfix, QMail, ...) and version?

@Maks 3w: If I understand correctly the internals of sending mail via PHP... the answer is:

I currently don't send by my own MTA. I use SMTP transport to access a "use and throw away" email account from another hoster. I have no clue what software they use to handle my mails.

But i have to admit that emails and related stuff is far away from my regular domain of knowledge. So please let me know if i mixed something up.

Honestly, this sounds like a b0rked MTA. I'll take a look later, but I'm very hesitant to introduce code that will deviate from standards, as this will cause things to break.

@Ja De I created this PR (https://github.com/zendframework/zf2/pull/1475) Could you check if fix your problem?

@Maks 3w: Sorry, can't do. After this incident i switched to a async worker script using PEAR mail which somehow is tolerant to this issue.

But as i looked at the PR code: Tweaking the constant Headers::HEADER_EOL will provide a fix to the problem with that specific mail service. It would have solved the trouble as it is similiar to the workaround i used back then.

Best regards, Jade

I found the real issue!!!!!!!

The problem is in Protocol\Smpt line 304, While the line endings for headers are CRLF, that loop is splitting only by LF beeing the final line CRCRLF