联系表格PHP电子邮件脚本

时间:2014-03-24 08:30:33

标签: php forms email twitter-bootstrap

您好我的网站上的电子邮件脚本有问题。

我用过这个文件

https://jonbake.com/blog/bootstrap-3-contact-form-with-captcha/

但我添加了一些字段。它正在发送到我的电子邮件,但我没有收到在这些字段上输入的所有数据。

这是我的HTML表单

<form role="form" id="feedbackForm">
          <div class="form-group">
            <input type="text" class="form-control" id="first_name" name="first_name" placeholder="First Name">
            <span class="help-block" style="display: none;">Please enter your name.</span>
          </div>
          <div class="form-group">
            <input type="text" class="form-control" id="last_name" name="last_name" placeholder="Last Name">
            <span class="help-block" style="display: none;">Please enter your name.</span>
          </div>
          <div class="form-group">
            <input type="email" class="form-control" id="email" name="email" placeholder="Email Address">
            <span class="help-block" style="display: none;">Please enter a valid e-mail address.</span>
          </div>
          <div class="form-group">
            <input type="text" class="form-control" id="company_name" name="company_name" placeholder="Company">
            <span class="help-block" style="display: none;">Please enter your name.</span>
          </div>
          <div class="form-group">
            <textarea rows="10" cols="100" class="form-control" id="message" name="message" placeholder="Message"></textarea>
            <span class="help-block" style="display: none;">Please enter a message.</span>
          </div>
          <div class="form-group">
            <label for="selectbasic">How did you hear about us?</label>
                <select id="selectbasic" name="selectbasic" class="form-control">
                    <option>Select</option>
                    <option>Search engine</option>
                    <option>Microsoft DPE</option>
                    <option>Microsoft event</option>
                    <option>Social media</option>
                    <option>Word of mouth</option>
                    <option>Other</option>
                </select>
            </div>
          <img id="captcha" src="library/vender/securimage/securimage_show.php" alt="CAPTCHA Image" />
          <a href="#" onclick="document.getElementById('captcha').src = 'library/vender/securimage/securimage_show.php?' + Math.random(); return false" class="btn btn-info btn-sm">Show a Different Image</a><br/>
          <div class="form-group" style="margin-top: 10px;">
            <input type="text" class="form-control" name="captcha_code" id="captcha_code" placeholder="For security, please enter the code displayed in the box." />
            <span class="help-block" style="display: none;">Please enter the code displayed within the image.</span>
          </div>

          <span class="help-block" style="display: none;">Please enter a the security code.</span>
          <button type="submit" id="feedbackSubmit" class="btn btn-primary btn-lg" style="display: block; margin-top: 10px;">Send Feedback</button>
        </form>

这是我的PHP脚本

<?php
 //start a session -- needed for Securimage Captcha check
 session_start();

 //add you e-mail address here
 define("MY_EMAIL", "dummyemail@gmail.com");

/**
 * Sets error header and json error message response.
 *
 * @param  String $messsage error message of response
 * @return void
 */
function errorResponse ($messsage) {
header('HTTP/1.1 500 Internal Server Error');
die(json_encode(array('message' => $messsage)));
}

/**
 * Return a formatted message body of the form:
 * Name: <name of submitter>
 * Comment: <message/comment submitted by user>
 *
 * @param String $name     name of submitter
 * @param String $messsage message/comment submitted
 */
function setMessageBody ($first_name, $last_name, $email, $message, $selectbasic) {
  $message_body = "Name: " . $first_name. $last_name. "\n\n";
  $message_body = "Email: " . $email."\n\n";
  $message_body = "Company Name: " . $company_name."\n\n";
  $message_body .= "Message:\n" . nl2br($message);
  $message_body .= "How did you hear about us?:" . $selectbasic."\n\n";
  return $message_body;
}
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];  
$email = $_POST['email']; 
$company_name = $_POST['company_name']; 
$message = $_POST['message'];
$selectbasic = $_POST['selectbasic'];

header('Content-type: application/json');
//do some simple validation. this should have been validated on the client-side also
if (empty($email) || empty($message)) {
errorResponse('Email or message is empty.');
}

//do Captcha check, make sure the submitter is not a robot:)...
include_once './vender/securimage/securimage.php';
$securimage = new Securimage();
if (!$securimage->check($_POST['captcha_code'])) {
  errorResponse('Invalid Security Code');
}

//try to send the message
if(mail(MY_EMAIL, "Feedback Form Results", setMessageBody($_POST["first_name"],         
$_POST["last_name"], $_POST["email"], $_POST["selectbasic"], $message), "From:    
 $first_name, $last_name")) {
echo json_encode(array('message' => 'Your message was successfully submitted.'));

  } else {
header('HTTP/1.1 500 Internal Server Error');
echo json_encode(array('message' => 'Unexpected error while attempting to send e-   
 mail.'));
 }
 ?>

我不知道出了什么问题,但我需要捕获所有数据。

感谢您的帮助。

这是我在将DOT置于等号前后得到的

enter image description here

新代码我正在使用上面的图片

<?php
 //start a session -- needed for Securimage Captcha check
 session_start();

 //add you e-mail address here
 define("MY_EMAIL", "aareyes00@ymail.com");

 /**
 * Sets error header and json error message response.
 *
 * @param  String $messsage error message of response
 * @return void
 */
 function errorResponse ($messsage) {
 header('HTTP/1.1 500 Internal Server Error');
 die(json_encode(array('message' => $messsage)));
 }

/**
 * Return a formatted message body of the form:
 * Name: <name of submitter>
 * Comment: <message/comment submitted by user>
 *
 * @param String $name     name of submitter
 * @param String $messsage message/comment submitted
 */
 function setMessageBody ($first_name, $last_name, $email, $company_name, $message,    
 $selectbasic) {
 $message_body .= "First Name: " . $first_name."\n\n";
 $message_body .= "Last Name: " . $last_name."\n\n";
 $message_body .= "Email: " . $email."\n\n";
 $message_body .= "Company Name:" . $company_name."\n\n";
 $message_body .= "Message:" . $message. "\n\n";
 $message_body .= "How did you hear about us?" . $selectbasic."\n\n";
 return $message_body;
 }
 $first_name = $_POST['first_name'];
 $last_name = $_POST['last_name'];  
 $email = $_POST['email']; 
 $company_name = $_POST['company_name']; 
 $message = $_POST['message'];
 $selectbasic = $_POST['selectbasic'];

 header('Content-type: application/json');
 //do some simple validation. this should have been validated on the client-side also
 if (empty($email) || empty($message)) {
 errorResponse('Email or message is empty.');
 }
 //do Captcha check, make sure the submitter is not a robot:)...
 include_once './vender/securimage/securimage.php';
 $securimage = new Securimage();
 if (!$securimage->check($_POST['captcha_code'])) {
 errorResponse('Invalid Security Code');
 }

 //try to send the message
 if(mail(MY_EMAIL, "Feedback Form Results", setMessageBody($_POST["first_name"],      
 $_POST["last_name"], $_POST["email"], $_POST["message"], $_POST["selectbasic"]), "From:    
 $first_name, $last_name")) {
 echo json_encode(array('message' => 'Your message was successfully submitted.'));

 } else {
 header('HTTP/1.1 500 Internal Server Error');
 echo json_encode(array('message' => 'Unexpected error while attempting to send e-   
 mail.'));
 }
 ?>

3 个答案:

答案 0 :(得分:0)

似乎没问题,只需修改一下:

<form role="form" id="feedbackForm" method="post">  

在表单标记中添加方法属性,因为在您的代码中,您没有定义任何发布方法,default post methodget。所以你有两个选择。

一次修改<form id="feedbackForm" method="post">

OR

将所有$_POST修改为$_GET

答案 1 :(得分:0)

如果您能通过电子邮件向我提供您收到的数据,我可以为您提供更好的解决方案。 但是看起来,你只是错过了这些行中函数setMessageBody(...)中的一些点(。) -

$message_body = "Email: " . $email."\n\n";
$message_body = "Company Name: " . $company_name."\n\n";

添加点(。) -

$message_body .= "Email: " . $email."\n\n";
$message_body .= "Company Name: " . $company_name."\n\n";

检查您的选择选项。你的行

<option>Select</option>
<option>Search engine</option>
<option>Microsoft DPE</option>
<option>Microsoft event</option>
<option>Social media</option>
<option>Word of mouth</option>
<option>Other</option>

会像 -

<option>Select</option>
<option value="Search engine">Search engine</option>
<option value="Microsoft DPE">Microsoft DPE</option>
<option value="Microsoft event">Microsoft event</option>
<option value="Social media">Social media</option>
<option value="Word of mouth">Word of mouth</option>
<option value="Other">Other</option>

答案 2 :(得分:-1)

更新您的HTML表单,如下所示:

<form action="email.php" method="post" role="form" id="feedbackForm">

试试这个email.php脚本:

<?php

//start a session -- needed for Securimage Captcha check
session_start();

//add you e-mail address here
define("MY_EMAIL", "dummyemail@gmail.com");

//error responder
function errorResponse($messsage)
{
    header('Content-type: application/json');
    header('HTTP/1.1 500 Internal Server Error');
    die(json_encode(array(
        'message' => $messsage
    )));
}

//parse input
$first_name   = isset($_POST['first_name'] ? $_POST['first_name'] : '');
$last_name    = isset($_POST['last_name'] ? $_POST['last_name'] : '');
$email        = isset($_POST['email']) ? $_POST['email'] : '';
$company_name = isset($_POST['company_name']) ? $_POST['company_name'] : '';
$message      = isset($_POST['message']) ? $_POST['message'] : '';
$selectbasic  = isset($_POST['selectbasic']) ? $_POST['selectbasic'] : '';

//do some simple validation. this should have been validated on the client-side also
if (empty($email) || empty($message)) {
    errorResponse('Email or message is empty.');
}

//do Captcha check, make sure the submitter is not a robot:)...
include_once './vender/securimage/securimage.php';
$securimage = new Securimage();
if (!$securimage->check($_POST['captcha_code'])) {
    errorResponse('Invalid Security Code');
}

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: ' . MY_EMAIL . "\r\n";
$headers .= "From: $first_name $last_name <$email>\r\n";

// Generate Message
$message_body = "Name: ". $first_name. $last_name. "<br>";
$message_body = "Email: ". $email ."<br>";
$message_body = "Company Name: ". $company_name ."<br>";
$message_body .= "Message:<br>". nl2br($message) ."<br>";
$message_body .= "How did you hear about us?: " . $selectbasic."\n\n";

//try to send the message
if (@mail(MY_EMAIL, "New message from $first_name $last_name", $message_body, $headers)) {
    echo json_encode(array(
        'message' => 'Your message was successfully submitted.'
    ));
} else {
    errorResponse('Unexpected error while attempting to send e-mail.');
}

?>