似乎无法让我的php代码正常工作

时间:2018-09-05 14:55:55

标签: php forms email

<?php

if(isset($_POST['email'])) {
    $user_email = $_POST['email'];          // this is just grabbed form the user email field
    $email_to   = "btndeals@btn.deals";     // where is the email going?
    $fullname   = $_POST['fname'];          // get the full name from hte $_POST data
    $subject    = $_POST ['subject'];       // what is the email subject?
    $message    = $_POST['message'];        // get the message from the $_POST data

    // ...geting data
    if(mail($email_to, $fname, $subject, $message, "From:" . $email)) {
        //the email got sent!!!
        //you can echo html out here :)
        echo "<strong>Sucessfully sent</strong>";
    } else {
        //email didn't work!
        //you can out html here :)
        echo "Error";
    }
}

?>

当在服务器上运行代码时,它总是出错,有人可以帮助我,我是php的新手,所以我仍然在学习如何使用它。

2 个答案:

答案 0 :(得分:1)

您使用mail函数错误。第二个和第三个参数应该分别是主题和消息。第四(附加标头)和第五(附加参数)参数是可选的。这是您需要的代码:

    if(isset($_POST['email'])) {
        $user_email = $_POST['email'];          //this is just grabbed form the user email field
        $email_to   = "btndeals@btn.deals"; //where is the email going?
        $fullname   = $_POST['fname'];         // get the full name from hte $_POST data
        $subject    = $_POST ['subject'];     //what is the email subject?
        $message    = $_POST['message'];        //get the message from the $_POST data

        $headers = "From:  $fullname <$user_email>";

        // ...geting data
        if(mail($email_to, $subject, $message, $headers)){
            //the email got sent!!!
            //you can echo html out here :)
            echo "<strong>Sucessfully sent</strong>";
        }
        else {
            //email didn't work!
            //you can out html here :)
            echo "Error";
        }
    }

如果仍然不起作用,请发布您收到的错误消息。另外请记住,您需要验证$_POST['email']参数以确保其有效。

答案 1 :(得分:-1)

这是我测试过的作品。问题是发送,只生成标题或没有标题:-

       <?php
         if(isset($_POST['email'])) {
        $user_email = $user_email = $_POST['email']; ;          //this is just grabbed form the user email field
        $email_to   = "btndeals@btn.deals"; //where is the email going?
        $fullname   = $_POST['fname']; ;         // get the full name from hte $_POST data
        $subject    = $_POST ['subject']; ;     //what is the email subject?
        $message    = $_POST['message'];        //get the message from the $_POST data

        // ...geting data
        if(mail($email_to,$fullname, $subject, $message, $email)){
            //the email got sent!!!
            //you can echo html out here :)
            echo "<strong>Sucessfully sent</strong>";
        }
        else {
            //email didn't work!
            //you can out html here :)
            echo "Error";
        }
   } 
相关问题