1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

newline & return characters showing up in newsletter

Discussion in 'PHP' started by bonecone, Dec 7, 2009.

  1. #1
    I've got a newsletter that sends out posts in html format, but it keeps showing the return and newline characters (\r\n) between paragraphs once it gets to the inbox. To try and filter out these characters I use

    $body = str_replace("\n", "", $body);
    $body = str_replace("\r", "", $body);

    Then I send the email with these parameters:

    mail($to, $subject, $body, 'From: ' . "\r\nContent-Type: text/html", '-f '. 'email@address.com')

    Any suggestions?
     
    bonecone, Dec 7, 2009 IP
  2. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #2
    Try it
    $body = nl2br($body);
    $body = str_replace("<br />", "", $body);

    The tag "<br />" can be also "<br>". It depends on PHP version.
     
    s_ruben, Dec 7, 2009 IP
  3. bonecone

    bonecone Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Tried it. No luck. It renders fine when you visit the webpage itself. But not in the inbox.
     
    bonecone, Dec 8, 2009 IP
  4. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Technically there is no such thing as return/newline characters in HTML. They only exist as part of headers or textual content.

    I'm guessing that what you're sending simply isn't proper HTML. Otherwise I can't think of another reason why they'd appear magically, unless you have some filtering going on beforehand that's converting the HTML to text.
     
    szalinski, Dec 8, 2009 IP
  5. bonecone

    bonecone Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Okay, fixed it afterwords. Originally I assigned the $_POST['body'] variable to $body. Then when I was modifying the html template to contain the newsletter I used the code

    $template_source = str_replace("{CONTENT}", $body, $template_source);
    after which I would assigned the $template_source to $body and send the email.

    so instead I used

    $template_source = str_replace("{CONTENT}", $_POST['body'], $template_source);

    Don't know why, but this fixed the problem.
     
    bonecone, Dec 8, 2009 IP
  6. califmerchant

    califmerchant Active Member

    Messages:
    112
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    63
    #6
    make sure correct headers are used:

    $headers = "Date: ".date('r')."\n";
    $headers .= "Return-Path: ".something@yourdomain.com."\n";
    $headers .= "From: ".$from."\n";
    $headers .= "Message-ID: <".md5(uniqid(time()))."@yourdomain.com>\n";
    $headers .= "X-Priority: 3\n";
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-Transfer-Encoding: 8bit\n";
    $headers .= 'Content-Type: text/html; charset="iso-8859-1"'."\n";
     
    califmerchant, Dec 8, 2009 IP