联系表格不会发送到指定的电子邮件

时间:2013-12-04 01:02:54

标签: php forms contact

请帮助以下联系表格。它似乎没有发送到指定的电子邮件。

表格的代码:

<h4 class="bolder">Leave a Message</h4>
<form class="form-horizontal">
    <p>
        <form name="form1" method="post" action="send_contact.php">
        <label for="name">Name</label>
        <input id="name" name="name" class="span3" type="text">
        <label for="email">Email</label>
        <input id="email" name="email" class="span3" type="email">
        <label for="comment">Message</label>
        <textarea id="comment" name="comment" class="span5" cols="15" rows="3"></textarea>
    </p>

    <p>
        <button class="btn btn-primary" type="submit">Send</button>
    </p>
</form>

提交行动的代码:

$message="$comment";
$mail_from="$email";
$header="from: $name <$mail_from>";
$to ='partnersys.solutions@gmail.com';
$send_contact=mail($to,$message,$header);

if($send_contact)
{
    echo "We've recived your contact information";
} else {
    echo "ERROR";
}

我无法找到问题所在。

1 个答案:

答案 0 :(得分:0)

这里是一个更优化的代码。

<?php

if(isset($_POST['email']))
{
    $email_to = "partnersys.solutions@gmail.com"; 

    function died( $error )
    {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

    if(!isset($_POST['full_name']) || !isset($_POST['email']) || !isset($_POST['subject']) || !isset($_POST['comments']))
    {
        died('We are sorry, but there appears to be a problem with the form you submitted.');      
    }

    $full_name = $_POST['full_name'];
    $email_from = $_POST['email'];
    $subject = $_POST['subject'];
    $comments = $_POST['comments'];

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

    if(!preg_match($email_exp,$email_from))
    {
        $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
    }
    $string_exp = "/^[A-Za-z .'-]+$/";

    if(!preg_match($string_exp,$full_name))
    {
        $error_message .= 'The First Name you entered does not appear to be valid.<br />';
    }
    if(strlen($comments) < 2)
    {
        $error_message .= 'The Comments you entered do not appear to be valid.<br />';
    }

    if(strlen($error_message) > 0)
    {
        died($error_message);
    }

    $email_message = "Form details below.\n\n";

    function clean_string($string)
    {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_subject .= clean_string($subject);
    $email_message .= "Full name: ".clean_string($full_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";

    $headers = 'From: '.$email_from."\r\n".
    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers); 

    echo 'We\'ve recived your contact information';
}
?>


<!doctype html>  
<html lang="en">
<head>
    <meta charset="utf-8"> 
    <title>Contact us </title>
</head>
<body>

    <form name="contactform" method="post" action="">
        <input  type="text" name="full_name" placeholder="First and last name(optional)" maxlength="50" size="30">
        <input  type="text" name="email" placeholder="Email Address" maxlength="80" size="30">
        <input  type="text" name="subject" placeholder="Subject" maxlength="30" size="30">
        <textarea  name="comments" maxlength="1000" placeholder="Message goes here." cols="25" rows="6"></textarea>
        <input type="submit" value="Submit" class="widthfix">
    </form>

</body>
</html>