1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Help with PHP Emailing

Discussion in 'PHP' started by fullylucky, Oct 28, 2013.

  1. #1
    Hi I need help modifying the php code inside a plug in.

    I think it's fairly simply but I have no idea what I'm doing.... can you please help.

    I want to change the code below so the body of the email also contains the name of the person who made the booking as well as other notes or details they filled in the form.

    See this section of the code:
    if (!$errors):
    $to = "";
    $subject = "Booking - Confirmation";
    $body = "Hi random,\n\nYou have a booking on $time\n\n";
    mail($to, $subject, $body);
    ?>

    That's the bit I added in myself. It current works but now I want the body of the email to also contain other info the user filled in the form!

    not just $time

    But all other info... can you help?

    PHP:
    1. <?php
    2.  
    3. class BIRS_Shortcode {
    4.  
    5.     const SHORTCODE_NAME = 'bp-scheduler-bookingform';
    6.  
    7.     function __construct() {
    8.         add_action('init', array(&$this, 'init'));
    9.         add_action('template_redirect', array(&$this, 'add_js_css'));
    10.         add_action('wp_ajax_nopriv_birs_save_appointment_frontend', array(&$this, 'ajax_save_appointment'));
    11.         add_action('wp_ajax_birs_save_appointment_frontend', array(&$this, 'ajax_save_appointment'));
    12.         add_action('wp_ajax_nopriv_birs_get_avaliable_time', array(&$this, 'ajax_get_avaliable_time'));
    13.         add_action('wp_ajax_birs_get_avaliable_time', array(&$this, 'ajax_get_avaliable_time'));
    14.     }
    15.  
    16.     function init() {
    17.         add_shortcode(self::SHORTCODE_NAME, array(&$this, 'get_shortcode_html'));
    18.         add_filter('birchschedule_validate_booking_form_info', array($this, 'validate_time'));
    19.         add_filter('birchschedule_booking_form_fields', array($this, 'get_form_fields_html'));
    20.     }
    21.  
    22.     private function get_calendar_view() {
    23.         global $birchschedule;
    24.         return $birchschedule->calendar_view;
    25.     }
    26.  
    27.     function add_js_css() {
    28.         if (!$this->get_util()->has_shortcode(self::SHORTCODE_NAME)) {
    29.             return;
    30.         }
    31.         $calendar = $this->get_calendar_view();
    32.         if (is_page() || is_single()) {
    33.             $params = array(
    34.                 'ajax_url' => admin_url('admin-ajax.php'),
    35.                 'all_schedule' => $this->get_all_schedule(),
    36.                 'service_price_map' => $calendar->get_service_price_map(),
    37.                 'service_staff_map' => $calendar->get_service_staff_map(),
    38.                 'location_staff_map' => $calendar->get_location_staff_map(),
    39.                 'gmt_offset' =>$this->get_util()->get_gmt_offset()
    40.             );
    41.             wp_enqueue_script('birchschedule');
    42.             wp_localize_script('birchschedule', 'birs_params', $params);
    43.  
    44.             wp_enqueue_style('jquery-ui-bootstrap');
    45.             wp_enqueue_style('birchschedule_styles');
    46.         }
    47.     }
    48.  
    49.     function ajax_save_appointment() {
    50.         $permitted = check_ajax_referer("birs_save_appointment-0", '_wpnonce', false);
    51.         if ($permitted) {
    52.             $calendar = $this->get_calendar_view();
    53.             $errors = apply_filters('birchschedule_validate_booking_form_info', array());
    54.             if (!$errors) {
    55.                 $appointment_id = $calendar->save_appointment();
    56.                 if (!$appointment_id) {
    57.                     $errors['birs_saving_appointment'] = __('Booking appointment failed');
    58.                 } else {
    59.                     $appointment = new BIRS_Appointment($appointment_id, array(
    60.                                 'meta_keys' => array(
    61.                                     '_birs_appointment_location',
    62.                                     '_birs_appointment_service',
    63.                                     '_birs_appointment_staff',
    64.                                     '_birs_appointment_timestamp'
    65.                                 )
    66.                             ));
    67.                     $appointment->load();
    68.                     $location = new BIRS_Location($appointment['_birs_appointment_location'], array(
    69.                                 'base_keys' => array(
    70.                                     'post_title'
    71.                                 )
    72.                             ));
    73.                     $location->load();
    74.                     $service = new BIRS_Service($appointment['_birs_appointment_service'], array(
    75.                                 'base_keys' => array(
    76.                                     'post_title'
    77.                                 ),
    78.                                 'meta_keys' => array(
    79.                                     '_birs_service_length', '_birs_service_length_type',
    80.                                     '_birs_service_padding', '_birs_service_padding_type'
    81.                                 )
    82.                             ));
    83.                     $service->load();
    84.                     $service_length = $service->get_service_length();
    85.                     $staff = new BIRS_Staff($appointment['_birs_appointment_staff'], array(
    86.                                 'base_keys' => array(
    87.                                     'post_title'
    88.                                 )
    89.                             ));
    90.                     $staff->load();
    91.                     $time = $this->get_util()->convert_to_datetime($appointment['_birs_appointment_timestamp']);
    92.                 }
    93.             }
    94.         } else {
    95.             $errors = array(
    96.                 'birs_booking' => 'Booking appointment failed.'
    97.             );
    98.         }
    99.         ?>
    100.         <div id="birs_response">
    101.             <?php
    102.             if (!$errors):
    103.                 $to = "random@gmail.com";
    104.                 $subject = "Booking - Confirmation";
    105.                 $body = "Hi Random,\n\nYou have a booking on $time\n\n";
    106.                 mail($to, $subject, $body);
    107.                 ?>
    108.                 <div id="birs_success">
    109.                     <h3> <?php _e('Your appointment has been booked successfully.', 'birchschedule'); ?></h3>
    110.                     <div>
    111.                         <ul>
    112.                             <li>
    113.                                 <h4><?php _e('Location:', 'birchschedule'); ?></h4>
    114.                                 <p><?php echo $location['post_title']; ?></p>
    115.                             </li>
    116.                             <li>
    117.                                 <h4><?php _e('Service:', 'birchschedule'); ?></h4>
    118.                                 <p><?php echo " $service->post_title ($service_length mins) with $staff->post_title"; ?></p>
    119.                             </li>
    120.                             <li>
    121.                                 <h4><?php _e('Time:', 'birchschedule'); ?></h4>
    122.                                 <p><?php echo $time; ?></p>
    123.                             </li>
    124.                         </ul>
    125.                     </div>
    126.                 </div>
    127.             <?php else: ?>
    128.                 <div id="birs_errors">
    129.                     <?php foreach ($errors as $error_id => $message): ?>
    130.                         <p id="<?php echo $error_id; ?>"><?php echo $message; ?></p>
    131.                     <?php endforeach; ?>
    132.                 </div>
    133.             <?php endif ?>
    134.         </div>
    135.         <?php
    136.         die;
    137.     }
    138.  
    139.     function get_all_schedule() {
    140.         $query = new BIRS_Model_Query(
    141.                         array(
    142.                             'post_type' => 'birs_staff'
    143.                         ),
    144.                         array(
    145.                             'meta_keys' => array(
    146.                                 '_birs_staff_schedule'
    147.                             )
    148.                 ));
    149.         $staff = $query->query();
    150.         $allschedule = array();
    151.         foreach (array_values($staff) as $thestaff) {
    152.             $schedule = $thestaff->get_all_schedule();
    153.             $allschedule[$thestaff->ID] = $schedule;
    154.         }
    155.         return $allschedule;
    156.     }
    157.  
    158.     function get_util() {
    159.         return BIRS_Util::get_instance();
    160.     }
    161.  
    162.     function render_service_options($selection) {
    163.         $query = new BIRS_Model_Query(
    164.                         array(
    165.                             'post_type' => 'birs_service'
    166.                         ),
    167.                         array(
    168.                             'meta_keys' => array(
    169.                                 '_birs_service_length', '_birs_service_length_type', '_birs_service_price', '_birs_service_price_type'
    170.                             ),
    171.                             'base_keys' => array(
    172.                                 'post_title'
    173.                             )
    174.                 ));
    175.         $services = $query->query();
    176.         foreach ($services as $service_id => $service) {
    177.             if ($service_id == $selection) {
    178.                 $selected = ' selected="selected" ';
    179.             } else {
    180.                 $selected = '';
    181.             }
    182.             $length = $service->get_service_length();
    183.             $price = $this->get_service_price_text($service);
    184.             echo "<option value='$service->ID' $selected>$service->post_title ($length mins) - $price</option>";
    185.         };
    186.         if ($selection) {
    187.             return $selection;
    188.         } else {
    189.             return key($services);
    190.         }
    191.     }
    192.  
    193.     function validate_time($errors) {
    194.         $avaliable_times = $this->get_avaliable_time();
    195.         $time = $_POST['birs_appointment_time'];
    196.         $valid = array_key_exists($time, $avaliable_times) && $avaliable_times[$time]['avaliable'];
    197.         if (!$valid) {
    198.             $errors = array_merge(
    199.                     array(
    200.                 'birs_appointment_datetime' => __('Time is unavaliable', 'birchschedule'
    201.                     )), $errors);
    202.         }
    203.         return $errors;
    204.     }
    205.  
    206.     function get_avaliable_time() {
    207.         $location_id = 0;
    208.         if (isset($_POST['birs_appointment_location'])) {
    209.             $location_id = $_POST['birs_appointment_location'];
    210.         }
    211.         $service_id = 0;
    212.         if (isset($_POST['birs_appointment_service'])) {
    213.             $service_id = $_POST['birs_appointment_service'];
    214.         }
    215.         $staff_id = 0;
    216.         if (isset($_POST['birs_appointment_staff'])) {
    217.             $staff_id = $_POST['birs_appointment_staff'];
    218.         }
    219.         $date = 0;
    220.         if (isset($_POST['birs_appointment_date'])) {
    221.             $date = $_POST['birs_appointment_date'];
    222.         }
    223.         if (!($location_id && $service_id && $staff_id && $date)) {
    224.             return array();
    225.         }
    226.         $staff = new BIRS_Staff($staff_id, array(
    227.                     'meta_keys' => array(
    228.                         '_birs_staff_schedule'
    229.                     )
    230.                 ));
    231.         $staff->load();
    232.         $time_options = $staff->get_avaliable_time($location_id, $service_id, $date);
    233.         return $time_options;
    234.     }
    235.  
    236.     function ajax_get_avaliable_time() {
    237.         ?>
    238.         <div>
    239.         <?php
    240.         $time_options = $this->get_avaliable_time();
    241.         foreach ($time_options as $key => $value) {
    242.             if ($value['avaliable']) {
    243.                 $text = $value['text'];
    244.                 echo "<span><a data-time='$key' href='javascript:void(0)'>$text</a></span>";
    245.             }
    246.         }
    247.         ?>
    248.         </div>
    249.         <?php
    250.         die();
    251.     }
    252.  
    253.     public function get_service_price_text($service) {
    254.         global $birchschedule;
    255.         $services_view = $birchschedule->services_view;
    256.         $text_map = $services_view->get_price_type_text_map();
    257.         $price_type = $service['_birs_service_price_type'];
    258.         if ($price_type == 'fixed') {
    259.             return apply_filters('birchschedule_price', $service['_birs_service_price']);
    260.         } else if ($price_type == 'dont-show') {
    261.             return '';
    262.         } else {
    263.             return $text_map[$price_type];
    264.         }
    265.     }
    266.  
    267.     function get_shortcode_html($attr) {
    268.         if (!is_page() && !is_single()) {
    269.             return;
    270.         }
    271.         $calendar = $this->get_calendar_view();
    272.         ob_start();
    273.         ?>
    274.         <div class="birchschedule" id="birs_booking_box">
    275.             <form id="birs_appointment_form">
    276.                 <input type="hidden" id="birs_appointment_price" name="birs_appointment_price">
    277.                 <div>
    278.                     <?php wp_nonce_field("birs_save_appointment-0"); ?>
    279.                     <?php echo apply_filters('birchschedule_booking_form_fields', ''); ?>
    280.                 </div>
    281.                 <div class="birs_footer">
    282.                     <div class="birs_error" id="birs_booking_error"></div>
    283.                     <input type="button" value="<?php _e('Submit', 'birchschedule'); ?>" class="button" id="birs_book_appointment">
    284.                 </div>
    285.             </form>
    286.         </div>
    287.         <div id="birs_booking_success">
    288.         </div>
    289.         <?php
    290.         return ob_get_clean();
    291.     }
    292.  
    293.     function get_form_fields_html($html) {
    294.         $calendar = $this->get_calendar_view();
    295.         ob_start();
    296.         ?>
    297.         <ul>
    298.         <li class="birs_form_field">
    299.             <h2 class="birs_section"><?php _e('Appointment Info', 'birchschedule'); ?></h2>
    300.         </li>
    301.         <li class="birs_form_field">
    302.             <label><?php _e('Location', 'birchschedule'); ?></label>
    303.             <div>
    304.                 <select id="birs_appointment_location" name="birs_appointment_location">
    305.                     <?php $location_id = $calendar->render_location_options(); ?>
    306.                 </select>
    307.             </div>
    308.         </li>
    309.         <li class="birs_form_field">
    310.             <label><?php _e('Service', 'birchschedule'); ?></label>
    311.             <div>
    312.                 <select id="birs_appointment_service" name="birs_appointment_service">
    313.                     <?php $service_id = $this->render_service_options(0); ?>
    314.                 </select>
    315.             </div>
    316.         </li>
    317.         <li class="birs_form_field">
    318.             <label><?php _e('Service Provider', 'birchschedule'); ?></label>
    319.             <div>
    320.                 <select id="birs_appointment_staff" name="birs_appointment_staff">
    321.                     <?php $calendar->render_staff_options($location_id, $service_id, 0); ?>
    322.                 </select>
    323.             </div>
    324.             <div class="birs_error" id="birs_appointment_service_error"></div>
    325.         </li>
    326.         <li class="birs_form_field">
    327.             <label><?php _e('Date & Time', 'birchschedule'); ?></label>
    328.             <input id="birs_appointment_date" name="birs_appointment_date" type="hidden">
    329.             <input id="birs_appointment_time" name="birs_appointment_time" type="hidden">
    330.             <table class="birs_datetime">
    331.                 <tbody>
    332.                     <tr>
    333.                         <td><div id="birs_appointment_datepicker"></div></td>
    334.                         <td><div id="birs_appointment_timeoptions"></div></td>
    335.                     </tr>
    336.                 </tbody>
    337.             </table>
    338.             <div class="birs_error" id="birs_appointment_datetime_error"></div>
    339.         </li>
    340.         <li class="birs_form_field">
    341.             <h2 class="birs_section"><?php _e('Your Info', 'birchschedule'); ?></h2>
    342.         </li>
    343.         <li class="birs_form_field">
    344.             <label><?php _e('First Name', 'birchschedule') ?></label>
    345.             <div>
    346.                 <input id="birs_client_name_first" name="birs_client_name_first" type="text">
    347.                 <input type="hidden" name="birs_client_fields[]" value="_birs_client_name_first" />
    348.             </div>
    349.             <div class="birs_error" id="birs_client_name_first_error"></div>
    350.         </li>
    351.         <li class="birs_form_field">
    352.             <label><?php _e('Last Name', 'birchschedule') ?></label>
    353.             <div>
    354.                 <input id="birs_client_name_last" name="birs_client_name_last" type="text">
    355.                 <input type="hidden" name="birs_client_fields[]" value="_birs_client_name_last" />
    356.             </div>
    357.             <div class="birs_error" id="birs_client_name_last_error"></div>
    358.         </li>
    359.         <li class="birs_form_field">
    360.             <label><?php _e('Email', 'birchschedule') ?></label>
    361.             <div>
    362.                 <input id="birs_client_email" name="birs_client_email" type="text">
    363.                 <input type="hidden" name="birs_client_fields[]" value="_birs_client_email" />
    364.             </div>
    365.             <div class="birs_error" id="birs_client_email_error"></div>
    366.         </li>
    367.         <li class="birs_form_field">
    368.             <label><?php _e('Phone', 'birchschedule') ?></label>
    369.             <div>
    370.                 <input id="birs_client_phone" name="birs_client_phone" type="text">
    371.                 <input type="hidden" name="birs_client_fields[]" value="_birs_client_phone" />
    372.             </div>
    373.             <div class="birs_error" id="birs_client_phone_error"></div>
    374.         </li>
    375.         <li class="birs_form_field">
    376.             <label><?php _e('Notes', 'birchschedule') ?></label>
    377.             <div>
    378.                 <textarea id="birs_appointment_notes" name="birs_appointment_notes"></textarea>
    379.                 <input type="hidden" name="birs_appointment_fields[]" value="_birs_appointment_notes" />
    380.             </div>
    381.         </li>
    382.         </ul>
    383.         <?php
    384.         $html = ob_get_clean();
    385.         return $html;
    386.     }
    387.  
    388. }

    Last edited: Apr 24, 2014
    fullylucky, Oct 28, 2013 IP
  2. HowDoYou

    HowDoYou Active Member

    Messages:
    425
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    80
    #2
    This is one of those times when it would be easier just to do it for you, then explain how to do it.
    Look at the function "get_form_fields_html" in the code, this seems to be where it drops the form code to the website. in here you can get the names associated with each form field for example "birs_client_phone", "birs_client_email", etc.. the "function get_shortcode_html" seems to be where they are dropping the html for the submit button. In the function " ajax_save_appointment()" is where your going to place your code. the first thing i would do is a var_dump($_post) in that function, before your mail call. or in the body of your mail call, so you can make sure these variables from the form are being passed to the function. Let me know if you get this far, and if these variables are being passed to that function.
    HowDoYou, Oct 29, 2013 IP
  3. ttyler333

    ttyler333 Greenhorn

    Messages:
    62
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    18
    #3
    I am unfamiliar with birs however you could convert your email into an html email. http://css-tricks.com/sending-nice-html-email-with-php/.
    I see some "WP" functions and assumed you're using wordpress. If that is the case this may help you in getting the username.
    PHP:
    1. <?php global $current_user;
    2.       get_currentuserinfo();
    3.       echo 'Username: ' . $current_user->user_login . "\n";
    4.       echo 'User email: ' . $current_user->user_email . "\n";
    5.       echo 'User first name: ' . $current_user->user_firstname . "\n";
    6.       echo 'User last name: ' . $current_user->user_lastname . "\n";
    7.       echo 'User display name: ' . $current_user->display_name . "\n";
    ttyler333, Oct 29, 2013 IP
  4. ezprint2008

    ezprint2008 Active Member

    Messages:
    595
    Likes Received:
    14
    Best Answers:
    2
    Trophy Points:
    90
    Digital Goods:
    4
    #4
    // catch the email of the person booking through a form.
    when its submitted:
    if($user_email){
    $user_email = $_POST['user_email'];

    if (!$errors):
    $to = $user_email;
    $subject = "Booking - Confirmation";
    $body = "Hi Fullylucky,\n\nYou have a booking on $time\n\n";
    mail($to, $subject, $body);
    }
    ?>
    ezprint2008, Oct 30, 2013 IP
  5. fullylucky

    fullylucky Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #5
    Thanks guys for taking the time to look at the code....
    I looked at the function

    I want to pass these variables to the email call and email it to myself not to the user.
    birs_client_phone
    birs_client_email
    birs_client_name_first
    birs_client_name_last

    Right now the $time bit works perfectly...
    But it would be great if I can get the other variables... like the client's first and surname.
    The body of the email would be like:
    $body = "Hi Fullylucky,\n\nYou have a booking on $time\n\n" With client: $Firstname $Surname.\n;

    How do I get the variables inside $Firstname $Surname?

    Are you saying I should put
    if($birs_client_name_first){
    $Firstname = $_POST['birs_client_name_first'];

    Where do I put this bit of code? I'm a total newb maybe I should learn PHP first...

    I'm guessing the if statement determines if the string is empty? if it's empty it's false. so the next part doesn't go through.

    Sorry guys for being so pathetic.. lol
    fullylucky, Nov 2, 2013 IP
  6. HowDoYou

    HowDoYou Active Member

    Messages:
    425
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    80
    #6
    it checks to see if the variable exists, not if its empty.
    To check if its "empty", not including ""
    PHP:
    1. bool isset ( mixed $var [, mixed $... ] )
    PHP:
    1. if (isset($var)) {
    2.     echo "This var is set so I will print.";
    3. }
    Or...
    PHP:
    1. if (!empty($variable)) do_something();
    Similar, all vars should be set, even if null. if you are going to set, and use them in the future.
    PHP:
    1. $foo = isset($foo) ? $foo : null;
    Ok, class over! :D
    just use something like this, to see if all the variables are passed, so your not recreating them.
    PHP:
    1. $to = $user_email;
    2. $subject = "Booking - Confirmation";
    3. $body = var_dump($_POST);
    4. mail($to, $subject, $body);
    Last edited: Nov 3, 2013
    HowDoYou, Nov 3, 2013 IP
  7. fullylucky

    fullylucky Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #7
    I tried using

    I think I get what you are trying to say. Use var_dump to just dump every single smelly variable into the email and then look at the email and find the variables for first name surname etc....


    I did exactly that

    PHP:
    1. $body = var_dump($_POST);
    but the email I got was blank.... it was sent correctly to my inbox, with the correct subject line etc but the body was blank...

    I filled out the form myself. filling in all details.

    what is
    PHP:
    1. $_POST
    ? did I make a typo somewhere?

    I've been trying for an hour no other $variables can be printed other than $time...

    Also looking at the code does it have anything to do with this:

    Code (Text):
    1. <input type="hidden" name="birs_client_fields[]" value="_birs_client_name_first" />
    why is it hidden? what does that do? would it work if i remove these?
    Last edited: Nov 3, 2013
    fullylucky, Nov 3, 2013 IP
  8. HowDoYou

    HowDoYou Active Member

    Messages:
    425
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    80
    #8
    Ok, I've taken the time to look further into your code.
    This is what i would do.

    Code a new plugin to handle the Email that goes to you.
    This plugin creates a new JQuery JS Include file that goes in the header of where the current plugin's form is located, and contains a php mail script that your going to code.

    PHP:
    1. <input type="button" value="<?php _e('Submit', 'birchschedule'); ?>" class="button" id="birs_book_appointment">
    This is the submit button for the form; where going to hijack it..

    HTML:
    1. $(document).ready(function(e){
    2.     $('#birs_book_appointment').click(function(){
    3.         //Ajax Post Data
    4.     });
    5. });
    Using this method you can hijack the submit button click event and execute your own JQuery JavaScript code to grab all form fields with user inputted data that you want emailed to you, and write a separate php file to post the data to using a simple jQuery ajax request. You can now have this new php file email you the information. - and as long as you don't use any kind of preventdefault function on the click event, the plugin your trying to modify now will still execute the same way it always has.

    Using this method your:
    1. Not modifying the plugin directly.
    2. You can make it do anything you want using your own plugin code.
    HowDoYou, Nov 4, 2013 IP
  9. fullylucky

    fullylucky Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #9
    Where would I put the second block of code in your post?

    the <script>stuff</script>

    Which line? would I insert that code into (reference with the OP code block).

    I'm guessing //Ajax Post Data
    is the part where I need to put email function so it emails me the info... unfortunately I don't know jQuery or ajax request...

    :'( :'( :'( :'( :'( :'(
    fullylucky, Nov 5, 2013 IP
  10. HowDoYou

    HowDoYou Active Member

    Messages:
    425
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    80
    #10
    Message me if you want my Skype information. I'm not going to code a WordPress plugin in a forum post.
    HowDoYou, Nov 6, 2013 IP