doulian7252 2016-06-23 19:06
浏览 34
已采纳

无法通过php脚本/ html表单发送电子邮件[重复]

This question already has an answer here:

I have created a simple html form to send an email using php. Currently whenever I try to send the info I just get redirected to my process.php page with and browser error (myserver.com unable to handle this request).

I have already tried sending a test email by making my php page just the mail() function and it does indeed work so it has to do with my code somewhere. I'm sure it's something simple so here is my code.

HTML (contact.html):

<!-- Contact form -->
<form id="form" action="process.php" method="post">
    <div>
        <label for='name'><span class='required'></span></label>
        <input id="Field1" type='text' name='name' placeholder='Type your Email Here' required/>
    </div>
    <div>
        <label for='message'><span class='required'></span></label>
        <textarea id="Field2" name='message' placeholder="Type a Message for us Here" required></textarea>
    </div>
    <div>
        <button type='submit'>SEND MESSAGE</button>
    </div>  
</form>

PHP (process.php):

<?php
//if "email" variable is filled out, send email
if(isset($_POST['name']) && isset($_POST['message'])) {

    //Email information
    $admin_email = "test@mydomain.com";
    $email = $_POST['name'];
    $subject = "Email from contact form";
    $comment = $_POST['message'];

    //send email
    if(mail($admin_email, $subject, $comment, "From:" . $email)) {
        echo '<p>Success</p>';
        header('Location: contact.html');
    } else { 
        echo '<p>Error sending message</p>'; 
    }
} else {
    echo '<p>Please fully fill out the form</p>'; 
}
?>
</div>
  • 写回答

2条回答 默认 最新

  • dongzhuang5741 2016-06-23 19:33
    关注

    You have syntax error in the PHP code you have shared - You are missing ; in the $subject = "Email from contact form" line.

    Please see bellow -

    <?php
    //if "email" variable is filled out, send email
    if (isset($_POST['name'], $_POST['message'])) {
    
        //Email information
        $admin_email = "test@mydomain.com";
        $email = $_POST['name'];
        $subject = "Email from contact form";
        $comment = $_POST['message'];
    
        // //send email
        if(mail($admin_email, $subject, $comment, "From:" . $email)) {
            echo '<p>Success</p>';
            header('Location: contact.html');
        } else { 
            echo '<p>Error sending message</p>'; 
        }
    } else {
        echo '<p>Please fully fill out the form</p>'; 
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?