【问题标题】:Issue with contact form - message failed [duplicate]联系表问题-消息失败[重复]
【发布时间】:2015-06-27 07:28:00
【问题描述】:

我在让我的contact.php 表单工作时遇到问题。在我根本没有收到邮件到我的电子邮件之前,现在,电子邮件确实已发送,请始终显示:

“消息失败。请发送电子邮件至 treneree@gmail.com” 这就是它所显示的全部内容:

表格:

<form action="contact.php" method="post" class="tm-contact-form">
    <div class="form-group">
        <input name=”cf_name” type="text" id="contact_name" class="form-control" placeholder="NAME..." />
    </div>
    <div class="form-group">
        <input name=”cf_email” type="text" id="contact_email" class="form-control" placeholder="EMAIL..." />
    </div>
    <div class="form-group">
        <textarea name=”cf_message” id="contact_message" class="form-control" rows="8" placeholder="WRITE A MESSAGE..."></textarea>
    </div>
    <button type="submit" class="btn text-uppercase tm-dark-bg tm-orange-text tm-send-btn">Send</button>
</form>

和php:

<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];

$mail_to = 'trener.waw@gmail.com' ;
$subject = 'Message from a site visitor '.$field_name;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
    <script language="javascript" type="text/javascript">
        alert('Thank you for the message. We will contact you shortly.');
        window.location = 'index.html';
    </script>
<?php
}
else { ?>
    <script language="javascript" type="text/javascript">
        alert('Message failed. Please, send an email to treneree@gmail.com');
        window.location = 'index.html';
    </script>
<?php
}
?>

有人吗?

【问题讨论】:

    标签: php forms email contact


    【解决方案1】:

    请添加这些标题,

    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= 'From: '.$field_email."\r\n";
    $headers .= 'Reply-To: '.$field_email."\r\n";
    

    【讨论】:

    • 仍然无法正常工作。同样的问题。
    • 只需将所有表单名称中的name=”cf_name” 替换为name="cf_name"
    【解决方案2】:

    问题在于您的表单在您的form input name 中有 特殊字符

    只需将所有表单名称中的name=”cf_name” 替换为name="cf_name"

    <form action="" method="post" class="tm-contact-form">
        <div class="form-group">
            <input name="cf_name" type="text" id="contact_name" class="form-control" placeholder="NAME..." />
        </div>
        <div class="form-group">
            <input name="cf_email: type="text" id="contact_email" class="form-control" placeholder="EMAIL..." />
        </div>
        <div class="form-group">
            <textarea name="cf_message" id="contact_message" class="form-control" rows="8" placeholder="WRITE A MESSAGE..."></textarea>
        </div>
        <button type="submit" class="btn text-uppercase tm-dark-bg tm-orange-text tm-send-btn">Send</button>
    </form>
    

    如果你使用像name=”cf_name”这样的名字

    你的帖子数据看起来像

    Array ( [â€cf_nameâ€] =>value  [â€cf_emailâ€] =>value  [â€cf_messageâ€] => value )
    

    【讨论】:

    • 仍然无法正常工作。有什么想法吗?
    • 你的$_POST的值是多少请粘贴
    • $field_name = $_POST['cf_name']; $field_email = $_POST['cf_email']; $field_message = $_POST['cf_message'];
    • 我想在contact.php 文件结果中添加print_r($_POST)
    • 我在contact.php中没有这个print_r($_POST)
    猜你喜欢
    • 1970-01-01
    • 2019-08-14
    • 1970-01-01
    • 2017-08-26
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-30
    相关资源
    最近更新 更多