bingoman27 Posted November 29, 2012 Share Posted November 29, 2012 I am a absolute Newbie, So please be kind I have a form that i need to be cc'ed to my alternate email addresse. Please let me know how do i add a CC on this form. here is the code. Also what i need to do is 1. When a user types his email id in the email id field. That email id should be used in the from header. right now the default system email is being used and that is the email that is used to reply back. So the mail appears like mail sent from steven< joe@joe.com> wherein it should be steven<steven@hisemail.com 2. I need to cc the form to sales@joe.com Here is the code <?php /* Template Name: Book */ // Fetch options stored in $data global $data; // Get booking page ID $booking_page = home_url() . '/?page_id=' . $data['booking_page_id']; // Check if form has been submit if ( $_SERVER['HTTP_REFERER'] == home_url() . '/?page_id=' . $data['booking_page_id'] && $_POST['submit'] == 'submit' ) { $submit = true; // Post form data from this page $book_room_type = $_POST['book_room_type']; $book_date_from = $_POST['book_date_from']; $book_date_to = $_POST['book_date_to']; $book_full_name = $_POST['book_full_name']; $book_num_rooms = $_POST['book_num_rooms']; $book_email = $_POST['book_email']; $book_phone = $_POST['book_phone']; $book_message = $_POST['book_message']; $book_room_price = $_POST['book_room_price']; if(trim( $book_room_type ) === '') { $book_room_type_error = __('Room Type is a required field', 'qns'); $got_error = true; } if(trim( $book_date_from ) === '') { $book_date_from_error = __('Date From is a required field', 'qns'); $got_error = true; } if(trim( $book_date_to ) === '') { $book_date_to_error = __('Date To is a required field', 'qns'); $got_error = true; } if(trim( $book_full_name ) === '' or trim( $book_full_name ) === 'Full Name') { $book_full_name_error = __('Full Name is a required field', 'qns'); $got_error = true; } if(trim( $book_num_rooms ) === '' or trim( $book_num_rooms ) === 'Number of Rooms') { $book_num_rooms_error = __('Number of Rooms is a required field', 'qns'); $got_error = true; } if(trim( $book_email ) === '' or trim( $book_email ) === 'Email Address' or valid_email( trim($book_email) ) === FALSE ) { $book_email_error = __('Email Address is a required field', 'qns'); $got_error = true; } } // If the form on this page has not been submit post the values from the accommodation page else { if($_POST['book_room_type_and_price'] != '') { $book_room_price_array = explode(',', $_POST['book_room_type_and_price']); $book_room_type = $book_room_price_array[0]; $book_room_price = $book_room_price_array[1]; } else { $book_room_type = $_POST['book_room_type']; $book_room_price = $_POST['book_room_price']; } $book_date_from = $_POST['book_date_from']; $book_date_to = $_POST['book_date_to']; } // Calculate Length of Stay $date_from_stt = strtotime($book_date_from); $date_to_stt = strtotime($book_date_to); $datediff = $date_to_stt - $date_from_stt; $total_stay = floor($datediff/(60*60*24)); // Calculate Total Price $total_price = $book_room_price * $book_num_rooms; // Get currency unit stored in the theme options if ( $data['currency_unit'] !== '' ) { $currency_unit = $data['currency_unit']; } // If the unit is not set else { $currency_unit = '$'; } // If form has been submit and there are no errors send it if ( $submit == true && $got_error != true ) { $email_to = $data['contact_email']; if (!isset($email_to) || ($email_to == '') ){ $email_to = get_option('admin_email'); } $subject = get_bloginfo( 'name' ) . __(' Booking Via Website','qns'); $body = '<ul style="margin: 0px;padding:0 0 0 15px;">'; $body .= '<li style="margin-bottom: 3px;"><strong>' . __('Room Type','qns') . ":</strong> " . $book_room_type . "</li>"; $body .= '<li style="margin-bottom: 3px;"><strong>' . __('Date','qns') . ":</strong> " . $book_date_from . " - " . $book_date_to . "</li>"; $body .= '<li style="margin-bottom: 3px;"><strong>' . __('Guest Name','qns') . ":</strong> " . $book_full_name . "</li>"; $body .= '<li style="margin-bottom: 3px;"><strong>' . __('Number of Rooms','qns') . ":</strong> " . $book_num_rooms . "</li>"; $body .= '<li style="margin-bottom: 3px;"><strong>' . __('Email','qns') . ":</strong> " . $book_email . "</li>"; $body .= '<li style="margin-bottom: 3px;"><strong>' . __('Phone','qns') . ":</strong> " . $book_phone . "</li>"; $body .= '<li style="margin-bottom: 3px;"><strong>' . __('Price Quoted','qns') . ":</strong> " . $currency_unit.$total_price . "</li>"; $body .= '<li style="margin-bottom: 3px;"><strong>' . __('Special Requirements','qns') . ":</strong> " . $book_message . "</li>"; $body .= '<li style="margin-bottom: 3px;"><strong>' . __('IP Address','qns') . ":</strong> " . $_SERVER['REMOTE_ADDR'] . "</li>"; $body .= '</ul>'; $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=UTF-8\r\n"; $headers .= "From: ".$book_full_name." <".$email_to.">" . "\r\n" . "Reply-To: " . $book_email; mail($email_to, $subject, $body, $headers); $emailSent = true; } ?> <?php get_header(); ?> <?php //Display Page Header global $wp_query; $postid = $wp_query->post->ID; echo page_header( get_post_meta($postid, 'qns_page_header_image', true) ); wp_reset_query(); ?> <!-- BEGIN .section --> <div class="section page-full clearfix"> <h2 class="page-title">Room Booking</h2> <!-- BEGIN .page-content --> <div class="page-content page-content-full"> <?php // Prevent users from loading the page directly if ( $_SERVER['HTTP_REFERER'] == '' or $_POST['book_confirm'] != '1' ) : echo '<div class="msg fail"><p>Please do not load this page directly, go to the accommodation page first and select room</p></div>'; ?> <?php else : ?> <!-- BEGIN .even-cols --> <div class="even-cols clearfix"> <!-- BEGIN .one-half --> <div class="one-half"> <?php if ( $submit == true && $got_error != true ) { echo '<div class="msg success"><p>'; _e('Booking Successful! Please expect to get a reply within 24 hours','qns'); echo '</p></div>'; } if ( $got_error == true ) { echo '<div class="msg fail"> <ul class="list-fail">'; if ( $book_room_type_error != '' ) { echo '<li>' . $book_room_type_error . '</li>'; } if ( $book_date_from_error != '' ) { echo '<li>' . $book_date_from_error . '</li>'; } if ( $book_date_to_error != '' ) { echo '<li>' . $book_date_to_error . '</li>'; } if ( $book_full_name_error != '' ) { echo '<li>' . $book_full_name_error . '</li>'; } if ( $book_num_rooms_error != '' ) { echo '<li>' . $book_num_rooms_error . '</li>'; } if ( $book_email_error != '' ) { echo '<li>' . $book_email_error . '</li>'; } echo '</ul></div>'; } ?> <?php if ( $emailSent == true ) : ?> <table class="booking-table"> <thead> <tr> <th><?php _e('Booking Option','qns'); ?></th> <th><?php _e('Selection','qns'); ?></th> </tr> </thead> <tbody> <tr> <td><strong><?php _e('Room Type','qns'); ?>:</strong></td> <td><?php echo $book_room_type; ?></td> </tr> <tr> <td><strong><?php _e('Date','qns'); ?>:</strong></td> <td><?php echo $book_date_from; ?> - <?php echo $book_date_to; ?> (<?php echo $total_stay; ?> <?php _e('Nights','qns'); ?>)</td> </tr> <tr> <td><strong><?php _e('Name','qns'); ?>:</strong></td> <td><?php echo $book_full_name; ?></td> </tr> <tr> <td><strong><?php _e('Number of Rooms','qns'); ?>:</strong></td> <td><?php echo $book_num_rooms; ?></td> </tr> <tr> <td><strong><?php _e('Email Address','qns'); ?>:</strong></td> <td><?php echo $book_email; ?></td> </tr> <tr> <td><strong><?php _e('Phone Number','qns'); ?>:</strong></td> <td><?php echo $book_phone; ?></td> </tr> <tr> <td><strong><?php _e('Special Requirements','qns'); ?>:</strong></td> <td><?php echo $book_message; ?></td> </tr> <tr class="table-highlight"> <td><strong><?php _e('Total Cost','qns'); ?>:</strong></td> <td><?php echo $currency_unit.$total_price; ?></td> </tr> </tbody> </table> <?php else : ?> <!-- BEGIN .booknow-accompage --> <div class="booknow-accompage full-booking-form"> <div class="book-price"> <h3 class="price"><?php echo $currency_unit; ?><span class="room-price"><?php //echo $total_price; ?><? echo $book_room_price * $_POST['book_num_rooms'];?></span><span class="price-detail"><span class="price-detail-value"><?php echo $total_stay; ?></span> Nights</span></h2> </div> <script> // Set room price for external JS file var getPrice = <?php echo $book_room_price; ?>; </script> <form class="booking-form booking-form-accompage" name="bookroom" action="<?php echo $booking_page; ?>" method="post"> <input type="text" value="<?php echo $book_room_type; ?>" class="text-input" disabled="disabled"> <div class="clearfix"> <input type="text" name="book_date_from" id="datefrom" value="<?php echo $book_date_from; ?>" class="input-half datepicker"> <input type="text" name="book_date_to" id="dateto" value="<?php echo $book_date_to; ?>" class="input-half input-half-last datepicker"> </div> <input type="text" onblur="if(this.value=='')this.value='Full Name';" onfocus="if(this.value=='Full Name')this.value='';" value="<?php if(isset($_POST['book_full_name'])) : echo $_POST['book_full_name']; else : _e('Full Name','qns'); endif; ?>" name="book_full_name" class="text-input" /> <input type="text" onblur="if(this.value=='')this.value='Number of Rooms';" onfocus="if(this.value=='Number of Rooms')this.value='';" value="<?php if(isset($_POST['book_num_rooms'])) : echo $_POST['book_num_rooms']; else : _e('Number of Rooms','qns'); endif; ?>" name="book_num_rooms" class="text-input" /> <input type="text" onblur="if(this.value=='')this.value='Email Address';" onfocus="if(this.value=='Email Address')this.value='';" value="<?php if(isset($_POST['book_email'])) : echo $_POST['book_email']; else : _e('Email Address','qns'); endif; ?>" name="book_email" class="text-input" /> <input type="text" onblur="if(this.value=='')this.value='Phone Number';" onfocus="if(this.value=='Phone Number')this.value='';" value="<?php if(isset($_POST['book_phone'])) : echo $_POST['book_phone']; else : _e('Phone Number','qns'); endif; ?>" name="book_phone" class="text-input" /> <textarea class="text-input" rows="6" name="book_message" onfocus="if(this.value=='Special Requirements')this.value='';" onblur="if(this.value=='')this.value='Special Requirements';"><?php if(isset($_POST['book_message'])) : echo $_POST['book_message']; endif; ?></textarea> <input type="hidden" name="book_confirm" value="1" /> <input type="hidden" name="book_room_type" value="<?php echo $book_room_type; ?>" /> <input type="hidden" name="book_room_price" value="<?php echo $book_room_price; ?>" /> <input type="hidden" name="submit" value="submit" /> <input class="bookbutton" type="submit" value="<?php _e('Book Now','qns'); ?>" /> </form> <!-- END .booknow-accompage --> </div> <?php endif; ?> <!-- END .one-half --> </div> <!-- BEGIN .one-half --> <div class="one-half last-col"> <?php the_content(); ?> <!-- END .one-half --> </div> <!-- END .even-cols --> </div> <?php endif; ?> <!-- END .page-content --> </div> <!-- END .section --> </div> <?php get_footer(); ?> Thanks for the help and sorry for the trouble. Quote Link to comment https://forums.phpfreaks.com/topic/271357-adding-cc-to-my-form/ Share on other sites More sharing options...
mrMarcus Posted November 29, 2012 Share Posted November 29, 2012 To CC somebody it's just a simple addition to the mail headers: $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=UTF-8\r\n"; $headers .= "From: ".$book_full_name." <".$email_to.">" . "\r\n" . "Reply-To: " . $book_email ."\r\n"; $headers .= "Cc: somebody@example.com\r\n"; mail($email_to, $subject, $body, $headers); Quote Link to comment https://forums.phpfreaks.com/topic/271357-adding-cc-to-my-form/#findComment-1396226 Share on other sites More sharing options...
bingoman27 Posted December 1, 2012 Author Share Posted December 1, 2012 Hi thanks for the help. I tried that but still the code doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/271357-adding-cc-to-my-form/#findComment-1396608 Share on other sites More sharing options...
DavidAM Posted December 1, 2012 Share Posted December 1, 2012 $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=UTF-8\r\n"; $headers .= "From: ".$book_full_name." <".$email_to.">" . "\r\n" . "Reply-To: " . $book_email; mail($email_to, $subject, $body, $headers); $emailSent = true; Show us this segment of the code with your changes. It should be something like this: $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=UTF-8\r\n"; $headers .= "From: ".$book_full_name." <".$email_to.">" . "\r\n" . "Reply-To: " . $book_email . "\r\n"; # Added CR-LF $headers .= 'Cc: ' . $email_cc . "\r\n"; # ADDED - Use the variable with the CC address # NOTE: You are unconditionally setting $emailSent to TRUE, set it to the return value of mail() $emailSent = mail($email_to, $subject, $body, $headers); #$emailSent = true; REMOVE THIS LINE Also, please tell us what "doesn't work" means. Do you have error reporting turned on at the highest settings? Do you get any errors or warnings? Does the script SAY it worked but the email never arrives? Did you check you Junk Mail and Spam folders? Quote Link to comment https://forums.phpfreaks.com/topic/271357-adding-cc-to-my-form/#findComment-1396682 Share on other sites More sharing options...
bingoman27 Posted December 6, 2012 Author Share Posted December 6, 2012 Hello, The script works fine but i didn't receive the email. I also checked the junk folder but the mail wasn't there. I got no errors whatsoever. Quote Link to comment https://forums.phpfreaks.com/topic/271357-adding-cc-to-my-form/#findComment-1397852 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.