联系表格问题 - 消息失败

时间:2015-06-27 07:28:00

标签: php forms email contact

我在使用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
}
?>

任何?

2 个答案:

答案 0 :(得分:0)

请添加这些标题,

$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";

答案 1 :(得分:0)

问题在于您的表单

中有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 )