使用php将表单信息发送到电子邮件

时间:2014-08-30 23:32:07

标签: php mysql forms send

我想请你帮忙。我使用简单的时段预订日历,一切正常(详细信息正确传输到msql数据库),但我无法将所有表单详细信息发送到我的电子邮件和客户电子邮件以进行确认目的。

我只能发送前。地址信息或姓名信息但不能发送两者,我也无法发送所有详细信息。你能帮我么。非常感谢你们任何人的帮助!

book_slots.php:

<?php

include('php/connect.php');
include('new.php');  

if(isset($_POST['slots_booked'])) $slots_booked = mysqli_real_escape_string($link, $_POST['slots_booked']);
if(isset($_POST['name'])) $name = mysqli_real_escape_string($link, $_POST['name']);
if(isset($_POST['address'])) $address = mysqli_real_escape_string($link, $_POST['address']);
if(isset($_POST['email'])) $email = mysqli_real_escape_string($link, $_POST['email']);
if(isset($_POST['phone'])) $phone = mysqli_real_escape_string($link, $_POST['phone']);
if(isset($_POST['booking_date'])) $booking_date = mysqli_real_escape_string($link, $_POST['booking_date']);
if(isset($_POST['cost_per_slot'])) $cost_per_slot = mysqli_real_escape_string($link, $_POST['cost_per_slot']);


$booking_array = array(
    "slots_booked" => $slots_booked,    
    "booking_date" => $booking_date,
    "cost_per_slot" => number_format($cost_per_slot, 2),
    "name" => $name,
    "address" => $address,
    "email" => $email,
    "phone" => $phone
);

$explode = explode('|', $slots_booked);

foreach($explode as $slot) {

    if(strlen($slot) > 0) {

        $query = "INSERT INTO bookings (date, start, name, address, email, phone) VALUES ('$booking_date', '$slot', '$name', '$address', '$email', '$phone')"; 
        $result = mysqli_query($link, $query) or die(mysqli_error($link)); 

    } // Close if

} // Close foreach

header( 'Location: thankyou.php' );


?> 

connect.php:

<?php

// Make a MySQL Connection
$host="my host";
$user="db_xxx";
$password="mypasswoe";
$db = "db_xxx";

$link = mysqli_connect($host, $user, $password);
mysqli_select_db($link, $db) or die(mysql_error());

?>

new.php:

<?php
//if "email" variable is filled out, send email
  if (isset($_REQUEST['email']))  {

  //Email information
  $admin_email = "booking@mydomain.com";
  $email = $_REQUEST['email'];
  $subject = "Booking Confirmation";
  $comment = "Hi, \n\n Thank you for joining  \n\n
Regards Site Admin \n\n";

  //send email
  mail($email, "$subject", "$comment", "From:" . $admin_email);

  //Email response
  echo "Thank you for contacting us!";
  }

  //if "email" variable is not filled out, display the form
  else  {
?>

 <form method="post">
  Email: <input name="email" type="text" /><br />
  Subject: <input name="subject" type="text" /><br />
  Message:Your Reservation has been made. Please check reservation detail below:<br />
  <textarea name="comment" rows="15" cols="40"></textarea><br />
  <textarea name="name" rows="15" cols="40"></textarea><br />
  <input type="submit" value="Submit" />
  </form>

<?php
  }
?>

1 个答案:

答案 0 :(得分:0)

if(isset($_POST) ){

//form validation vars
$formok = true;
$errors = array();

//sumbission data
$ipaddress = $_SERVER['REMOTE_ADDR'];
$date = date('d/m/Y');
$time = date('H:i:s');

//form data
$name = $_POST['name']; 
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$enquiry = $_POST['enquiry'];
$message = $_POST['message'];

//validate form data

//validate name is not empty
if(empty($name)){
    $formok = false;
    $errors[] = "You have not entered a name";
}

//validate email address is not empty
if(empty($email)){
    $formok = false;
    $errors[] = "You have not entered an email address";
//validate email address is valid
}elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){
    $formok = false;
    $errors[] = "You have not entered a valid email address";
}

//validate message is not empty
if(empty($message)){
    $formok = false;
    $errors[] = "You have not entered a message";
}
//validate message is greater than 20 charcters
elseif(strlen($message) < 20){
    $formok = false;
    $errors[] = "Your message must be greater than 20 characters";
}
//send email if all is ok
if($formok){
    $headers  = "MIME-Version: 1.0"."\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8"."\r\n";
    $headers .= "From: ".$email."(".$enquiry.")\r\n";   
    $emailbody = "<p>You have recieved a new message from the enquiries form on your website.</p>";
    $emailbody .="<p><strong>Name: </strong>".$name."</p>";
    $emailbody .= "<p><strong>Email Address:</strong>".$email."</p>";
    $emailbody .= "<p><strong>Telephone:</strong>".$telephone."</p>";
    $emailbody .= "<p><strong>Enquiry:</strong>".$enquiry."</p>";
    $emailbody .="<p><strong>Message:</strong>".$message."</p>";
    $emailbody .= "This message was sent from the IP Address:".$ipaddress." on ".$date." at ".$time."</p>";
    mail("service@globalsx.com","New Enquiry",$emailbody,$headers); 
}
//what we need to return back to our form
$returndata = array(
    'posted_form_data' => array(
        'name' => $name,
        'email' => $email,
        'telephone' => $telephone,
        'enquiry' => $enquiry,
        'message' => $message
    ),
    'form_ok' => $formok,
    'errors' => $errors
);
//if this is not an ajax request
if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest'){
    //set session variables
    session_start();
    $_SESSION['cf_returndata'] = $returndata;

    //redirect back to form
    header('location: ' . $_SERVER['HTTP_REFERER']);
}
}