Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

PHP Contact Form Problems?

I'm having one small problem with my PHP contact form. I have it set up, but when I submit something, it gives off this line:

' . "\r\n" . 'Reply-To:' . $email; mail($emailTo, $subject, $body, $headers); $emailSent = true; } } ?>

Here is the source code:

if(isset($_GET['submit'])) {

//Check to make sure that the name field is not empty

if(trim($_GET['name']) == '') {

$hasError = true;

} else {

$name = trim($_POST['name']);

}

//Check to make sure that the subject field is not empty

if(trim($_GET['subject']) == '') {

$hasError = true;

} else {

$subject = trim($_POST['subject']);

}

//Check to make sure sure that a valid email address is submitted

if(trim($_GET['email']) == '') {

$hasError = true;

} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_GET['email']))) {

$hasError = true;

} else {

$email = trim($_GET['email']);

}

//Check to make sure comments were entered

if(trim($_GET['comments']) == '') {

$hasError = true;

} else {

if(function_exists('stripslashes')) {

$comments = stripslashes(trim($_GET['comments']));

} else {

$comments = trim($_GET['comments']);

}

}

//If there is no error, send the email

if(!isset($hasError)) {

$emailTo = 'shadow_slayer15@yahoo.com';

$body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments";

$headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To:' . $email;

mail($emailTo, $subject, $body, $headers);

$emailSent = true;

}

}

Thanks for the help everyone!

4 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    Change this:

    $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To:' . $email;

    To this:

    $headers = "From: My Site <" . $emailTo . ">" . "\r\n";

    $headers .= "Reply-To: " . $email . "\r\n";

    That should fix it. It might also depend on your version of PHP. When you have the greater-than sign (>), it might think you are trying to end the code. Good luck (:

    Source(s): Me. also try out http://www.w3schools.com/PHP/func_mail_mail.asp for more help.
  • 1 decade ago

    Take out the $headers from mail($emailTo, $subject, $body, $headers); and see if the email sends without problem.

    if it does, put it back in and remove the spaces between the ' . "\r in the $headers function

    I think that you have some extra spaces in there and it is throwing off the escape characters \r\n

  • ?
    Lv 7
    1 decade ago

    $body = "Name: " . $name . "\r\n Email: " . $email . "\r\n Subject: " . $subject . "\r\n Comments: \r\n " . $comments;

    $headers = "From: " . $emailTo . "\r\n";

    (Php does not parse variable enclosed in single quotes, headers malformed. Check www.php.net "mail" function for more examples of headers).

  • Anonymous
    1 decade ago

    This is not a "shout out" type system. I will add support for that in another version.

    This system is meant for you to collect comments from users about your site. You can enable email too.

    You can play around with it at http://dzsoundnirvana.com/comment_system/

    or

    <?

    /***********************

    CLASS :: MYMail

    ************************/

    class MyMail{

    var $To;

    var $ToSender;//If you want the email to go to the sender

    var $Subject;

    var $Msg;

    var $Headers;

    var $From;

    var $ReplyTo;

    /*

    $headers = 'From: webmaster@example.com' . "\r\n" .

    'Reply-To: webmaster@example.com' . "\r\n" .

    'X-Mailer: PHP/' . phpversion();

    mail($to, $subject, $message, $headers);

    ***&&&&&*******

    For HTML Mail

    'MIME-Version: 1.0' . "\r\n";

    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    */

    function MyMail($To, $Subject, $Msg, $From, $ReplyTo){

    $this->To=(empty($To)) ? "0" : $To;

    $this->Subject=(empty($Subject)) ? "0" : $Subject;

    $this->Msg=(empty($Msg)) ? "0" : $Msg;

    $this->From=(empty($From)) ? "0" : $From;

    $this->ReplyTo=(empty($ReplyTo)) ? "0" : $ReplyTo;

    $this->Headers="MIME-Version: 1.0" . "\r\n" . "Content-type: text/html; charset=iso-8859-1" . "\r\n" . "From:" . $this->From . "\r\n" . "Reply-To:" . $this->ReplyTo . "\r\n" . "X-Mailer: PHP/" . phpversion();

    //Use this array if you want to send to only one person

    $SetMail=array(

    'To'=> $this->To,

    'Subject'=> $this->Subject,

    'Msg'=> $this->Msg,

    'Headers'=> $this->Headers,

    'From'=> $this->From,

    'ReplyTo'=> $this->ReplyTo

    );

    //Use this array if you want it to go to multiple people (the sender/CC:/Bcc:)

    /*

    $SetMail=array(

    'To'=> $this->To . "," . $ToSender,

    'Subject'=> $this->Subject,

    'Msg'=> $this->Msg,

    'Headers'=> $this->Headers,

    'From'=> $this->From,

    'ReplyTo'=> $this->ReplyTo

    );

    */

    if(in_array("0",$SetMail)){

    echo "<div align=\"left\"><font color=\"#640000\">Something wrong with the mail! Please make sure all fields are filled in!</font></div>";

    return;

    }

    else{

    if(!mail($SetMail['To'], $SetMail['Subject'], $SetMail['Msg'], $SetMail['Headers'])){

    echo "<script type=\"text/javascript\">window.location=\"error.php?app=Error&action=Echo&ErrorType=$this->Errors&Remarks=There is a problem with the Mail!\"</script>";

    }

    }

    }

    }

    ?>

    and call the class like this

    Code:

    $MyMail=new MyMail($To="Email address", $Subject="Subject", $Msg=Message or body, $From="From email address", $ReplyTo="reply to email address.or use from email address");

Still have questions? Get your answers by asking now.