【问题标题】:I'm not able to send messages using the mail() PHP function [duplicate]我无法使用 mail() PHP 函数发送消息 [重复]
【发布时间】:2016-08-15 03:23:08
【问题描述】:

我无法在我的代码中找到错误。我的 PHP 代码在我的 index.html 文件中。我无法从我的静态网页发送消息。

HTML 代码:

<div class="col-md-6">
    <form action="index.html" method="POST" class="contact-form" role="form">
        <input type="text" class="form-control" id="fname" name="fname" placeholder="Your Full Name">
        <input type="email" class="form-control" id="email" name="email" placeholder="Your E-mail">
        <input type="text" class="form-control" id="subj" name="subj" placeholder="Your Subject">
        <textarea id="mssg" name="mssg" placeholder="Your Message" class="form-control" rows="10"></textarea>
        <input class="btn btn-main btn-lg" type="submit" id="submit" name="submit" data-loading-text="<i class='fa fa-spinner fa-spin'></i> Sending...">
    </form>
</div>

PHP 代码:

<?php

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

    $to = 'email@company.com';
    $siteName = 'http://www.website.com';
    $name = $_POST['fname'];
    $mail = $_POST['email'];
    $subject = $_POST['subj'];
    $message = $_POST['mssg'];
    $mailSub = '[Contact] [' . $siteName . '] '.$subject;

    $body = 'Sender Name: ' . $name . "\n\n";
    $body .= 'Sender Mail: ' . $mail . "\n\n";
    $body .= 'Message Subject: ' . $subject . "\n\n";
    $body .= 'Message: ' . $message;

    $header = 'From: ' . $mail . "\r\n";
    $header .= 'To:  ' . $to . "\r\n";

    mail($to, $mailSub, $body, $header);
}else{
    echo '0';
}
?>

每次我尝试通过我的网站发送消息时都会收到错误消息。谢谢!

【问题讨论】:

  • "每次我尝试通过我的网站发送消息时都会收到一个错误"什么错误?
  • action="index.html" 您是否指示您的系统将.html 文件作为 PHP 处理?如果不是,那么“休斯顿,我们有问题”

标签: php html contact-form sendmessage


【解决方案1】:

如果您没有指示系统将 .html 文件视为 PHP,那么查看 action="index.html" 可能会出现问题。

如果不是,那么您需要将其更改为 .php 文件扩展名并重命名您当前的文件以适应它。然后将您的操作值更改为指向该文件。

例如:

&lt;form action="script.php" method="post"&gt;

  • script.php 是您当前的 PHP 处理程序。

【讨论】:

    猜你喜欢
    • 2015-02-02
    • 1970-01-01
    • 2011-08-13
    • 2018-06-08
    • 2015-11-20
    • 1970-01-01
    • 1970-01-01
    • 2015-01-31
    • 2014-03-24
    相关资源
    最近更新 更多