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条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀