PHP - there is a stray CR character in the message

Posted on 2004-12-01
Last Modified: 2012-05-05
I'm sending out email and a certain email keeps keeping bounced back.  This is the error:
      Remote host said: 501 5.5.2 there is a stray CR character in the message

I've fixed this in the past by using \n instead of \r\n (something like that) but now getting this message from an email.  Can someone tell me how I go about finding a CR character in a message.  Below is the function I use to send mail, it's part of a custom mail class.

    function send() {
      ini_set('sendmail_from', $this->fromName.' <'.$this->from.'>');
      $headers  = "{$this->mime}\n";
      $headers .= "{$this->currContentType}\n";
      $headers .= "From: {$this->fromName} <{$this->from}>\n";
      $headers .= "Reply-To: {$this->replyToName} <{$this->replyTo}>\n";
      if ($this->bcc != NULL) {
        $headers .= "Bcc: {$this->bccName} <{$this->bcc}>\n";              

      mail($this->toName.' <'.$this->to.'>', $this->subject, $this->message, $headers);
Question by:ronpac
    LVL 48

    Accepted Solution

    you will need \r\n instead of only \n
    Note:  You must use \r\n to separate headers, although some Unix mail transfer agents may work with just a single newline (\n).

     $headers  = "{$this->mime}\r\n";
          $headers .= "{$this->currContentType}\r\n";
          $headers .= "From: {$this->fromName} <{$this->from}>\r\n";
          $headers .= "Reply-To: {$this->replyToName} <{$this->replyTo}>\r\n";
    LVL 13

    Assisted Solution

    Try printing your $headers variable to the screen. You should see the stray character as a line break. It's likely in one of your variables.

    Author Comment

    gripe, I printed the headers, here they are:
    Content-type: text/html; charset=iso-8859-1
    From: <>
    Reply-To: <>
    Bcc:  <>

    There was no line feed after bcc.  I actually get the message as a bcc but the person I'm sending it gets bounced back so I assume it's the receiving server and not mine that is the problem.

    hernst42, I tried adding the \r\n and it did not work, thanks though.

    Author Comment

    please not that is not the real address, just changed it for this posting...not that it matters because I didn't change the name ~sigh~
    LVL 48

    Expert Comment

    Might be a problem if no bcc is given so this might work:

          $headers .= "Reply-To: {$this->replyToName} <{$this->replyTo}>";
          if ($this->bcc != NULL) {
            $headers .= "\r\nBcc: {$this->bccName} <{$this->bcc}>";            

    But I guess you tried that already?

    Author Comment

    The bcc is always given in this case. I actually get the bcc but the client doesn't get the message....I tried taking off the \n from the bcc but that didn't work either, I try that again though.
    LVL 25

    Expert Comment

    Try using phpmailer - it deals with line breaks properly for you, and it's much easier and more reliable than using mail().

    Author Comment

    never found a solution.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to count occurrences of each item in an array.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now