Contact.php裁掉名字

时间:2015-07-03 09:59:44

标签: php email contact-form

我从网站上收到了一封email.php。你如何为它添加一个额外的字段,比如电话号码? 我下载的原始版本工作正常。 电子邮件来自>>

  

来自:Nuski

     

电子邮件:my@email.com

     

主题:测试

     

消息:测试邮件

但是当我添加$field_phone = $_POST['cf_phone'];$body_message = "Phone: ".$field_phone."\n";时 电子邮件没有发件人姓名。像>>

  

电话:5559

     

电子邮件:flex@f.com

     

主题:reCaptcha测试

     

消息:电子邮件测试

这是我的HTML表单

<form  action="email.php" method="post">
     <div class="col-sm-6">
        <div class="form-group">
            <input class="form-control" name="cf_name" type="text" id="name" placeholder="Your Name" required>
        </div>
        <div class="form-group">
            <input class="form-control" name="cf_phone" type="text" id="phone" placeholder="Phone" >
        </div>

        <div class="form-group">
            <input class="form-control" name="cf_email" type="email" id="email" placeholder="Your Email" required>
        </div>
        <div class="form-group">
            <input class="form-control" name="cf_subject" type="text" id="subject" placeholder="Subject" required>
        </div>
    </div>
    <div class="col-sm-6">
        <div class="form-group">
            <textarea class="form-control btn-block" name="cf_message" id="comments" placeholder="Please include your address and item code for delivery" required></textarea><br>
        </div>
        <div class="form-group">
            <input type="submit" class="btn btn-block" id="submit" value="Send Message">
        </div>
    </div>
    <div class="col-sm-12">
        <p class="contact-success">Your Message has been Successfully Sent!</p>
        <p class="contact-error">Error! Something went wrong!</p>
    </div>
</form>

这是我的email.php

<?php
$field_name = $_POST['cf_name'];
$field_phone = $_POST['cf_phone']; //added by me
$field_email = $_POST['cf_email'];
$field_subject = $_POST['cf_subject'];
$field_message = $_POST['cf_message'];

$mail_to = 'ask@aki.co';
$subject = 'Message from AkiYTeeS - '.$field_subject;

$body_message = "From: ".$field_name."\n";
$body_message = "Phone: ".$field_phone."\n"; //added by me
$body_message .= "E-mail: ".$field_email."\n";
$body_message .= "Subject: ".$field_subject."\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 ');
        window.location = 'index.html';
    </script>
<?php
}
?>

2 个答案:

答案 0 :(得分:1)

替换:

$body_message = "Phone: ".$field_phone."\n"; //added by me

使用:

$body_message .= "Phone: ".$field_phone."\n"; //added by me // You forgot a "." here

另请注意,最好使用\r\n而不仅仅是\r\n,因为windows mac和linux每个都需要不同的2,所以通过添加它们应该始终工作< / p>

答案 1 :(得分:1)

$body_message = "Phone: ".$field_phone."\n";

您忘记了上一行中的连接运算符。 使用 ”。” (点)

$body_message .= "Phone: ".$field_phone."\n";