Help with simple PHP code?

by 5 replies
6
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");
}
?>
#programming #code #php #simple
  • 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.
  • 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.
    • [1] reply
    • \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).
  • $MESSAGE_BODY = str_replace("\r\n", "\n", $MESSAGE_BODY);
    $MESSAGE_BODY = nl2br($MESSAGE_BODY);
    then send the email with mail.

    Re's
    Rob Whisonant
  • 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.

Next Topics on Trending Feed