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.

Thread Status:
Not open for further replies.
  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
    
    class BIRS_Shortcode {
    
        const SHORTCODE_NAME = 'bp-scheduler-bookingform';
    
        function __construct() {
            add_action('init', array(&$this, 'init'));
            add_action('template_redirect', array(&$this, 'add_js_css'));
            add_action('wp_ajax_nopriv_birs_save_appointment_frontend', array(&$this, 'ajax_save_appointment'));
            add_action('wp_ajax_birs_save_appointment_frontend', array(&$this, 'ajax_save_appointment'));
            add_action('wp_ajax_nopriv_birs_get_avaliable_time', array(&$this, 'ajax_get_avaliable_time'));
            add_action('wp_ajax_birs_get_avaliable_time', array(&$this, 'ajax_get_avaliable_time'));
        }
    
        function init() {
            add_shortcode(self::SHORTCODE_NAME, array(&$this, 'get_shortcode_html'));
            add_filter('birchschedule_validate_booking_form_info', array($this, 'validate_time'));
            add_filter('birchschedule_booking_form_fields', array($this, 'get_form_fields_html'));
        }
    
        private function get_calendar_view() {
            global $birchschedule;
            return $birchschedule->calendar_view;
        }
    
        function add_js_css() {
            if (!$this->get_util()->has_shortcode(self::SHORTCODE_NAME)) {
                return;
            }
            $calendar = $this->get_calendar_view();
            if (is_page() || is_single()) {
                $params = array(
                    'ajax_url' => admin_url('admin-ajax.php'),
                    'all_schedule' => $this->get_all_schedule(),
                    'service_price_map' => $calendar->get_service_price_map(),
                    'service_staff_map' => $calendar->get_service_staff_map(),
                    'location_staff_map' => $calendar->get_location_staff_map(),
                    'gmt_offset' =>$this->get_util()->get_gmt_offset()
                );
                wp_enqueue_script('birchschedule');
                wp_localize_script('birchschedule', 'birs_params', $params);
    
                wp_enqueue_style('jquery-ui-bootstrap');
                wp_enqueue_style('birchschedule_styles');
            }
        }
    
        function ajax_save_appointment() {
            $permitted = check_ajax_referer("birs_save_appointment-0", '_wpnonce', false);
            if ($permitted) {
                $calendar = $this->get_calendar_view();
                $errors = apply_filters('birchschedule_validate_booking_form_info', array());
                if (!$errors) {
                    $appointment_id = $calendar->save_appointment();
                    if (!$appointment_id) {
                        $errors['birs_saving_appointment'] = __('Booking appointment failed');
                    } else {
                        $appointment = new BIRS_Appointment($appointment_id, array(
                                    'meta_keys' => array(
                                        '_birs_appointment_location',
                                        '_birs_appointment_service',
                                        '_birs_appointment_staff',
                                        '_birs_appointment_timestamp'
                                    )
                                ));
                        $appointment->load();
                        $location = new BIRS_Location($appointment['_birs_appointment_location'], array(
                                    'base_keys' => array(
                                        'post_title'
                                    )
                                ));
                        $location->load();
                        $service = new BIRS_Service($appointment['_birs_appointment_service'], array(
                                    'base_keys' => array(
                                        'post_title'
                                    ),
                                    'meta_keys' => array(
                                        '_birs_service_length', '_birs_service_length_type',
                                        '_birs_service_padding', '_birs_service_padding_type'
                                    )
                                ));
                        $service->load();
                        $service_length = $service->get_service_length();
                        $staff = new BIRS_Staff($appointment['_birs_appointment_staff'], array(
                                    'base_keys' => array(
                                        'post_title'
                                    )
                                ));
                        $staff->load();
                        $time = $this->get_util()->convert_to_datetime($appointment['_birs_appointment_timestamp']);
                    }
                }
            } else {
                $errors = array(
                    'birs_booking' => 'Booking appointment failed.'
                );
            }
            ?>
            <div id="birs_response">
                <?php
                if (!$errors):
                    $to = "random@gmail.com";
                    $subject = "Booking - Confirmation";
                    $body = "Hi Random,\n\nYou have a booking on $time\n\n";
                    mail($to, $subject, $body);
                    ?>
                    <div id="birs_success">
                        <h3> <?php _e('Your appointment has been booked successfully.', 'birchschedule'); ?></h3>
                        <div>
                            <ul>
                                <li>
                                    <h4><?php _e('Location:', 'birchschedule'); ?></h4>
                                    <p><?php echo $location['post_title']; ?></p>
                                </li>
                                <li>
                                    <h4><?php _e('Service:', 'birchschedule'); ?></h4>
                                    <p><?php echo " $service->post_title ($service_length mins) with $staff->post_title"; ?></p>
                                </li>
                                <li>
                                    <h4><?php _e('Time:', 'birchschedule'); ?></h4>
                                    <p><?php echo $time; ?></p>
                                </li>
                            </ul>
                        </div>
                    </div>
                <?php else: ?>
                    <div id="birs_errors">
                        <?php foreach ($errors as $error_id => $message): ?>
                            <p id="<?php echo $error_id; ?>"><?php echo $message; ?></p>
                        <?php endforeach; ?>
                    </div>
                <?php endif ?>
            </div>
            <?php
            die;
        }
    
        function get_all_schedule() {
            $query = new BIRS_Model_Query(
                            array(
                                'post_type' => 'birs_staff'
                            ),
                            array(
                                'meta_keys' => array(
                                    '_birs_staff_schedule'
                                )
                    ));
            $staff = $query->query();
            $allschedule = array();
            foreach (array_values($staff) as $thestaff) {
                $schedule = $thestaff->get_all_schedule();
                $allschedule[$thestaff->ID] = $schedule;
            }
            return $allschedule;
        }
    
        function get_util() {
            return BIRS_Util::get_instance();
        }
    
        function render_service_options($selection) {
            $query = new BIRS_Model_Query(
                            array(
                                'post_type' => 'birs_service'
                            ),
                            array(
                                'meta_keys' => array(
                                    '_birs_service_length', '_birs_service_length_type', '_birs_service_price', '_birs_service_price_type'
                                ),
                                'base_keys' => array(
                                    'post_title'
                                )
                    ));
            $services = $query->query();
            foreach ($services as $service_id => $service) {
                if ($service_id == $selection) {
                    $selected = ' selected="selected" ';
                } else {
                    $selected = '';
                }
                $length = $service->get_service_length();
                $price = $this->get_service_price_text($service);
                echo "<option value='$service->ID' $selected>$service->post_title ($length mins) - $price</option>";
            };
            if ($selection) {
                return $selection;
            } else {
                return key($services);
            }
        }
    
        function validate_time($errors) {
            $avaliable_times = $this->get_avaliable_time();
            $time = $_POST['birs_appointment_time'];
            $valid = array_key_exists($time, $avaliable_times) && $avaliable_times[$time]['avaliable'];
            if (!$valid) {
                $errors = array_merge(
                        array(
                    'birs_appointment_datetime' => __('Time is unavaliable', 'birchschedule'
                        )), $errors);
            }
            return $errors;
        }
    
        function get_avaliable_time() {
            $location_id = 0;
            if (isset($_POST['birs_appointment_location'])) {
                $location_id = $_POST['birs_appointment_location'];
            }
            $service_id = 0;
            if (isset($_POST['birs_appointment_service'])) {
                $service_id = $_POST['birs_appointment_service'];
            }
            $staff_id = 0;
            if (isset($_POST['birs_appointment_staff'])) {
                $staff_id = $_POST['birs_appointment_staff'];
            }
            $date = 0;
            if (isset($_POST['birs_appointment_date'])) {
                $date = $_POST['birs_appointment_date'];
            }
            if (!($location_id && $service_id && $staff_id && $date)) {
                return array();
            }
            $staff = new BIRS_Staff($staff_id, array(
                        'meta_keys' => array(
                            '_birs_staff_schedule'
                        )
                    ));
            $staff->load();
            $time_options = $staff->get_avaliable_time($location_id, $service_id, $date);
            return $time_options;
        }
    
        function ajax_get_avaliable_time() {
            ?>
            <div>
            <?php
            $time_options = $this->get_avaliable_time();
            foreach ($time_options as $key => $value) {
                if ($value['avaliable']) {
                    $text = $value['text'];
                    echo "<span><a data-time='$key' href='javascript:void(0)'>$text</a></span>";
                }
            }
            ?>
            </div>
            <?php
            die();
        }
    
        public function get_service_price_text($service) {
            global $birchschedule;
            $services_view = $birchschedule->services_view;
            $text_map = $services_view->get_price_type_text_map();
            $price_type = $service['_birs_service_price_type'];
            if ($price_type == 'fixed') {
                return apply_filters('birchschedule_price', $service['_birs_service_price']);
            } else if ($price_type == 'dont-show') {
                return '';
            } else {
                return $text_map[$price_type];
            }
        }
    
        function get_shortcode_html($attr) {
            if (!is_page() && !is_single()) {
                return;
            }
            $calendar = $this->get_calendar_view();
            ob_start();
            ?>
            <div class="birchschedule" id="birs_booking_box">
                <form id="birs_appointment_form">
                    <input type="hidden" id="birs_appointment_price" name="birs_appointment_price">
                    <div>
                        <?php wp_nonce_field("birs_save_appointment-0"); ?>
                        <?php echo apply_filters('birchschedule_booking_form_fields', ''); ?>
                    </div>
                    <div class="birs_footer">
                        <div class="birs_error" id="birs_booking_error"></div>
                        <input type="button" value="<?php _e('Submit', 'birchschedule'); ?>" class="button" id="birs_book_appointment">
                    </div>
                </form>
            </div>
            <div id="birs_booking_success">
            </div>
            <?php
            return ob_get_clean();
        }
    
        function get_form_fields_html($html) {
            $calendar = $this->get_calendar_view();
            ob_start();
            ?>
            <ul>
            <li class="birs_form_field">
                <h2 class="birs_section"><?php _e('Appointment Info', 'birchschedule'); ?></h2>
            </li>
            <li class="birs_form_field">
                <label><?php _e('Location', 'birchschedule'); ?></label>
                <div>
                    <select id="birs_appointment_location" name="birs_appointment_location">
                        <?php $location_id = $calendar->render_location_options(); ?>
                    </select>
                </div>
            </li>
            <li class="birs_form_field">
                <label><?php _e('Service', 'birchschedule'); ?></label>
                <div>
                    <select id="birs_appointment_service" name="birs_appointment_service">
                        <?php $service_id = $this->render_service_options(0); ?>
                    </select>
                </div>
            </li>
            <li class="birs_form_field">
                <label><?php _e('Service Provider', 'birchschedule'); ?></label>
                <div>
                    <select id="birs_appointment_staff" name="birs_appointment_staff">
                        <?php $calendar->render_staff_options($location_id, $service_id, 0); ?>
                    </select>
                </div>
                <div class="birs_error" id="birs_appointment_service_error"></div>
            </li>
            <li class="birs_form_field">
                <label><?php _e('Date & Time', 'birchschedule'); ?></label>
                <input id="birs_appointment_date" name="birs_appointment_date" type="hidden">
                <input id="birs_appointment_time" name="birs_appointment_time" type="hidden">
                <table class="birs_datetime">
                    <tbody>
                        <tr>
                            <td><div id="birs_appointment_datepicker"></div></td>
                            <td><div id="birs_appointment_timeoptions"></div></td>
                        </tr>
                    </tbody>
                </table>
                <div class="birs_error" id="birs_appointment_datetime_error"></div>
            </li>
            <li class="birs_form_field">
                <h2 class="birs_section"><?php _e('Your Info', 'birchschedule'); ?></h2>
            </li>
            <li class="birs_form_field">
                <label><?php _e('First Name', 'birchschedule') ?></label>
                <div>
                    <input id="birs_client_name_first" name="birs_client_name_first" type="text">
                    <input type="hidden" name="birs_client_fields[]" value="_birs_client_name_first" />
                </div>
                <div class="birs_error" id="birs_client_name_first_error"></div>
            </li>
            <li class="birs_form_field">
                <label><?php _e('Last Name', 'birchschedule') ?></label>
                <div>
                    <input id="birs_client_name_last" name="birs_client_name_last" type="text">
                    <input type="hidden" name="birs_client_fields[]" value="_birs_client_name_last" />
                </div>
                <div class="birs_error" id="birs_client_name_last_error"></div>
            </li>
            <li class="birs_form_field">
                <label><?php _e('Email', 'birchschedule') ?></label>
                <div>
                    <input id="birs_client_email" name="birs_client_email" type="text">
                    <input type="hidden" name="birs_client_fields[]" value="_birs_client_email" />
                </div>
                <div class="birs_error" id="birs_client_email_error"></div>
            </li>
            <li class="birs_form_field">
                <label><?php _e('Phone', 'birchschedule') ?></label>
                <div>
                    <input id="birs_client_phone" name="birs_client_phone" type="text">
                    <input type="hidden" name="birs_client_fields[]" value="_birs_client_phone" />
                </div>
                <div class="birs_error" id="birs_client_phone_error"></div>
            </li>
            <li class="birs_form_field">
                <label><?php _e('Notes', 'birchschedule') ?></label>
                <div>
                    <textarea id="birs_appointment_notes" name="birs_appointment_notes"></textarea>
                    <input type="hidden" name="birs_appointment_fields[]" value="_birs_appointment_notes" />
                </div>
            </li>
            </ul>
            <?php
            $html = ob_get_clean();
            return $html;
        }
    
    }
    PHP:
     
    Last edited: Apr 24, 2014
    fullylucky, Oct 28, 2013 IP
  2. HowDoYou

    HowDoYou Well-Known Member

    Messages:
    443
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    130
    #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 Member

    Messages:
    62
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #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 global $current_user;
          get_currentuserinfo();
          echo 'Username: ' . $current_user->user_login . "\n";
          echo 'User email: ' . $current_user->user_email . "\n";
          echo 'User first name: ' . $current_user->user_firstname . "\n";
          echo 'User last name: ' . $current_user->user_lastname . "\n";
          echo 'User display name: ' . $current_user->display_name . "\n";
    PHP:
     
    ttyler333, Oct 29, 2013 IP
  4. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #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 Well-Known Member

    Messages:
    40
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    108
    #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 Well-Known Member

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

    fullylucky Well-Known Member

    Messages:
    40
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    108
    #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

    $body = var_dump($_POST);
    PHP:
    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
    $_POST
    PHP:
    ? 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:

    <input type="hidden" name="birs_client_fields[]" value="_birs_client_name_first" />
    Code (markup):
    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 Well-Known Member

    Messages:
    443
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    130
    #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.

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

    <script>
    $(document).ready(function(e){
        $('#birs_book_appointment').click(function(){
            //Ajax Post Data
        });
    });
    </script>
    HTML:
    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 Well-Known Member

    Messages:
    40
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    108
    #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 Well-Known Member

    Messages:
    443
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    130
    #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
Thread Status:
Not open for further replies.