【问题标题】:Can't seem to get my php code working correctly [duplicate]似乎无法让我的 php 代码正常工作 [重复]
【发布时间】:2018-09-05 14:55:54
【问题描述】:
<?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 新手,所以我还在学习如何使用它。

【问题讨论】:

  • 您的网站可能禁用了邮件发送功能,您应该检查一下。
  • 您使用的是什么服务器?我问是因为在某些服务器中您必须启用邮件服务
  • 你好曹兰,请提供更多相关信息,例如错误信息是什么
  • 如果有帮助,我使用了液体服务器
  • 您使用的mail 函数有误。它需要四个参数,而不是五个。请在此处查看手册中的函数规范:php.net/manual/en/function.mail.php 编辑:我的错误,它确实需要五个参数,其中两个是可选的,但是您使用的参数顺序是错误的。第二个参数应该是主题,第三个参数应该是消息。您将主题和消息分别作为第三个和第四个参数,所以显然它不起作用。

标签: php forms email


【解决方案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'] 参数以确保其有效。

【讨论】:

  • 感谢您的帮助,现在一切正常
【解决方案2】:

这是我测试过的。问题是发送,只需制作标题或不发送标题:-

       <?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";
        }
   } 

【讨论】:

  • 想知道为什么投反对票
  • (不是我)我认为否决票是因为您的 mail 示例中的参数仍然错误。
  • 但是邮件在我尝试的时候已经发送了。
  • 不确定如何;)查看手册:php.net/manual/en/function.mail.php .. 您在主题中有全名,在消息中有主题,并且消息作为标题(此时应该会爆炸)。
  • 也许 PHP 发疯了并发送了它。如果您有任何修改,请修复它。
猜你喜欢
  • 2012-11-09
  • 2012-09-10
  • 1970-01-01
  • 2013-06-09
  • 2013-11-23
  • 2012-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多