Help with simple PHP code?

5 replies
This is my code in PHP to do when a form is submitted, everything works and I am receiving the mail but it is not putting a new line where the '\r\n' is. Any ideas?



<?php
if ($_POST["mail"]<>'') {
$ToEmail = 'xxx@yyy.com';
$EmailSubject = 'Contact Form';
$mailheader = "From: ".$_POST["mail"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["mail"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."\r\n";
$MESSAGE_BODY .= "Email: ".$_POST["mail"]."\r\n";
$MESSAGE_BODY .= "Comment: ".$_POST["message"]."\r\n";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
header("location:index.html");
}
?>
#code #php #simple
  • Profile picture of the author Brandon Tanner
    Hmmm... that code looks good to me. What are you using to check the emails with? Might try checking it in multiple different environments / accounts (ie desktop email client, online Gmail, etc)... that way you'll at least know if the issue originates on your server (could be a setting in the php.ini file), or if it's due to a setting in your email client.

    As a side note, I hope you're sanitizing your POST data... otherwise you could be vulnerable to a XSS attack.
    Signature

    {{ DiscussionBoard.errors[8009424].message }}
  • I might be way off base here but try using single quotes instead of double-quotes.

    I seem to remember having a problem where it treats special characters differently depending on the type of quotes you use.

    As brandon says you should sanitise the post value, try
    addslashes($_POST["mail"])

    for basic protection.
    Signature

    Do you buy competitor backlink data from the likes of MajesticSEO / SEOProfiler / SEOMoz / AHRefs?

    Manage it with Backlinks Explorer to help you get more backlinks more quickly.

    {{ DiscussionBoard.errors[8009552].message }}
    • Profile picture of the author Brandon Tanner
      Originally Posted by Backlinks Explorer View Post

      I might be way off base here but try using single quotes instead of double-quotes.
      \r\n won't work inside single quotes. It will just print it out as literal text if you do that. It's gotta be inside double quotes (same deal with putting a $variable inside quotes).
      Signature

      {{ DiscussionBoard.errors[8009648].message }}
  • Profile picture of the author Rob Whisonant
    $MESSAGE_BODY = str_replace("\r\n", "\n", $MESSAGE_BODY);
    $MESSAGE_BODY = nl2br($MESSAGE_BODY);
    then send the email with mail.

    Re's
    Rob Whisonant
    {{ DiscussionBoard.errors[8011008].message }}
  • Profile picture of the author Jr180180
    I could be wrong but I think it could be your content type. Try changing it to:

    PHP Code:
    "Content-type: text/plain; charset=utf-8rn"
    What could be happening is that you're sending the email as HTML. Even though it is creating a new line in the source, its still displaying as one line because the email client is trying to read it as HTML and not plain text. Try it and let me know.

    Otherwise, try using some <br> tags in the message body.
    Signature

    Sharing the love!

    Easy-to-Install Wordpress Plugin - SMS Points System
    Get started on DigitalOcean with DOCasts!

    {{ DiscussionBoard.errors[8011201].message }}

Trending Topics