php mail() script not working

冷暖自知 提交于 2019-12-12 03:55:49

问题


Hey guys I cannot for the life of me figure out where I went wrong here. When the form submits it takes it to a blank page. No errors or confirmed. Just Blank. Im assuming it is a syntax error but I just cant see it for some reason. Do you guys see anything? Here is my form code:

<form method="POST" action="mailtest.php">

<label>Name<span class="req">*</span></label>
<input name="name" placeholder="Type Here">

<label>Email<span class="req">*</span></label>
<input name="email" type="email" placeholder="Type Here">

<label>Subject</label>
<input name="subjectf" placeholder="Type Here">

<label>Message<span class="req">*</span></label>
<textarea name="message" placeholder="Type Here"></textarea>

<input class="submit" name="submit" type="submit" value="">
<span class="right" style="color:red">* represents a mandatory field.</span>
</form>

And here is the php script I have on the mailtest.php page:

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = $_POST['name'];
    $to = 'rkoolman@bellsouth.net'; 
    $subject= 'new enquiry on website';
    $subjectf= $_POST['subjectf'];  
    $body = "From: $name\n E-Mail: $email\n Subject: $subjectf\n Message:\n $message";

    if ($_POST['submit']) {
    if ($name != '' && $email != '' && $message != '') {         
            if (mail ($to, $subject, $body, $from)) { 
            echo '<p>Your message has been sent!</p>';
        } else { 
            echo '<p>Something went wrong, go back and try again!</p>'; 
        } 
    } else {
        echo '<p>You need to fill in all required fields!!</p>';
    }
}
?>

回答1:


You should always first check whether the form was submitted so

remove if ($_POST['submit']) and put it on the top of the script..

also you should check whther the $_POST["submit"] was set.. the script should look like this:

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

    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = $_POST['name'];
    $to = 'test@test.com'; 
    $subject= 'new enquiry on website';
    $subjectf= $_POST['subjectf'];  
    $body = "From: $name\n E-Mail: $email\n Subject: $subjectf\n Message:\n $message";


    if ($name != '' && $email != '' && $message != '') {         
            if (mail ($to, $subject, $body, $from)) { 
            echo '<p>Your message has been sent!</p>';
        } else { 
            echo '<p>Something went wrong, go back and try again!</p>'; 
        } 
    } else {
        echo '<p>You need to fill in all required fields!!</p>';
    }
}



回答2:


Set your submit input value to submit

<input class="submit" type="submit" name="submit" value="submit">


来源:https://stackoverflow.com/questions/16329398/php-mail-script-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!