简体   繁体   中英

My PHP contact form keeps giving error

Following is my code of contact form. Fields are ok but it is not sending mail and gives error message "Error sending message". I checked with the samplemail.php file from this link and it is working well, but if I use the same code for my website replacing the values, it doesn't work.

index.html

<form id="contact-valid-form" class="form-style" action="contact.php" method="post" role="form">
    <div class="form-group">
        <input type="text" class="text-field form-control field-validation required" data-validation-type="string" id="form-name" placeholder="Full Name" name="name" />
        <i class="form-icon fa fa-user"></i>
    </div>

    <div class="form-group">
        <input type="text" class="text-field form-control field-validation required" data-validation-type="email" id="form-email" placeholder="Email Address" name="email" />
    <i class="form-icon fa fa-envelope"></i>
    </div>

    <div class="form-group">
        <textarea placeholder="Message..." class="form-control field-validation required" name="message"></textarea>
        <i class="form-icon fa fa-comment"></i>
    </div>

    <div class="form-group submit">
        <span class="form-loader"><i class="fa fa-spinner fa-spin"></i></span>
        <input type="submit" name="submit" value="submit" class="btn btn-default" />
    </div>   
</form>

in contact.php

<?php
if (isset($_POST['name'],$_POST['email'],$_POST['message'])){

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: ' . $_POST['email']; 
$to  = 'myemail@mail.com'; 
$subject = 'subject';


$body = "From: $name\n E-Mail: $email\nMessage:\n $message";
if ($name != '' && $email != '' && $message != '') {

        if (mail ($to, $subject, $body, $from)) { 
        echo '<p>Success</p>';
    } else { 
        echo '<p>Error sending message</p>'; 
    } 


} else {
    echo '<p>fill the form</p>';
}
}
?>

The generic error message is not adding more Information. Can you please add the below code in your else part ? This should provide the last failure.

if (mail ($to, $subject, $body, $from)) { 
    echo '<p>Success</p>';
} else { 
    print_r(error_get_last()); 
} 

我在浏览器中运行您的代码,该代码正常运行。由于mail()函数返回1。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM