Customize mail sent to admin when Appointment is asked

Hello !

I was wondering how to customize the mail sent to admins when someone asks for an Appointment.
I also need to customize the list of mails which it's sent to.
I think with the great help I had about my previous topics I will be able to put my very custom functions inside :slight_smile:

Thanks a lot !
Regards,
Guillaume

  • Atkotech

    The emails sent to admin (if required) are only a copy of the one sent to the client. The admin doesn't have their own design. Maybe you could add it as an idea for a feature request. As far as the list of people it is sent to, it can only be sent to the client, administrator & the service provider. The contents of the emails can be edited towards the foot of the General tab in the plugin settings. Hope this helps.

  • Michael Bissett

    Hey @Guigro, hope you're doing well this evening! :slight_smile:

    As @coremedia mentioned, the email that gets sent to the admin & the service provider is the same one that gets sent to the client, and the email that gets sent to the client can be edited where he mentioned.

    I can definitely make the making of a separate email for the admin/service provider into a feature request, and I've gone ahead and moved this into the Features & Feedback section for you. :slight_smile:

    As for changing the list of people to send it to though, that sort of thing was discussed over here:

    https://premium.wpmudev.org/forums/topic/change-e-mail-recipient-adress-for-apointments

    Kind Regards,
    Michael

  • Guigro

    Hello guys !!

    Thanks a lot for your answers !!
    The mail I wanted to change was the mail saying "There is an appointment waiting for an answer".

    Because I have sales rep and some customers (not new ones) have a dedicated sales rep. So when there is a new appointment that is asked I would have loved to do this :
    - If there is no dedicated sales rep (managed by a custom field related to the user), a mail is sent to all my sales rep and admin with details about that appointment
    - If there is a dedicated sales rep (returning customers), a mail is sent to the dedicated sales rep and admin.

    I can build the code on my own if I find the function that sends this mail notification :slight_smile:

    Thanks !!!

  • Michael Bissett

    Hey @Guigro, thanks for your patience here! :slight_smile:

    I can build the code on my own if I find the function that sends this mail notification :slight_smile:

    The function in question would be the "send_notification" function, located on line 4466 of:

    appointments/appointments.php

    The code that particularly mentions the "An appointment requires your confirmation" subject would be located in lines 4480-4483 of that file. :slight_smile:

    You can also check out where the "send_notification" function is referenced elsewhere in that file, to get a better idea on the usage of it.

    Hope this helps!

    Kind Regards,
    Michael

  • Guigro

    Hello Michael !

    Thanks it helps me a lot.
    But in fact I think I spoke too quickly.
    From a previous support message, I've received this function to customize the Appointments Confirmation mail (sent to customers) :

    add_filter('app_confirmation_message', 'wpmudev_app_custom_message',10, 3);
    function wpmudev_app_custom_message($message, $r, $app_id){
    
    	global $appointments;
    	global $wpdb;
    		$r = $wpdb->get_row( $wpdb->prepare("SELECT * FROM <code>wp_app_appointments</code> WHERE ID=%d", $app_id) );
    	$message = "MY MESSAGE";
    
    	return $message;
    
    }

    But it seems to be the same function that sends all messages. How can I customize others mails then ? (like the Cancel appointment notification, the Notification send to the Admin for An Appointment needs your confirmation, the Appointment Reminder, ...).
    In my ideal world, I would be able to customize all those mails !

    My idea is that the answer to my question is in the filter, like the number displayed at the end of the filter :
    add_filter('app_confirmation_message', 'wpmudev_app_custom_message',10, 3);
    The 10,3 !! The 3 is the ID of the mail am I right ? But then how can I know which number are the other mails ? Instead of testing them all ?

    Thanks a lot !
    Cheers !
    Guillaume

  • Michael Bissett

    Hey @Guigro! :slight_smile:

    My idea is that the answer to my question is in the filter, like the number displayed at the end of the filter :

    add_filter('app_confirmation_message', 'wpmudev_app_custom_message',10, 3);

    The 10,3 !! The 3 is the ID of the mail am I right ?

    The 10 would indicate when the attached function is run (lower numbers indicate earlier execution, and larger numbers indicate later execution), and the 3 would indicate how many parameters the function accepts (those parameters being the $message, $r, $app_id variables in the function you mentioned earlier).

    You can learn more about add_filter over here:

    https://codex.wordpress.org/Function_Reference/add_filter

    Let's back up a bit though, and address this point:

    From a previous support message, I've received this function to customize the Appointments Confirmation mail (sent to customers) :

    But it seems to be the same function that sends all messages.

    Actually, there's several filters available for email content:

    - app_reminder_message (for reminder emails)
    - app_notification_message (for notification emails)
    - app_confirmation_message (for confirmation emails)
    - app_removal_notification_message (for removal notification emails)

    To address your earlier question:

    How can I customize others mails then ? (like the Cancel appointment notification, the Notification send to the Admin for An Appointment needs your confirmation, the Appointment Reminder, ...).

    To customize the message for each email, you'd need to write a function for each type of email, and hook each function into the respective email (as you saw in the earlier example, the function written there was hooked into the filter for the confirmation email).

    There's more filters available to use, as the ones I've covered here only adjust the messages for the emails (there's filters that adjust the subject line as well).

    I'd want to check out these functions more inside of appointments.php, and see more about how they work:

    - send_confirmation
    - send_notification
    - send_removal_notification
    - send_reminder

    Hope this helps! :slight_smile:

    Kind Regards,
    Michael

  • Michael Bissett

    Hey @Guigro,

    In regards to changing who receives the email, you should find the "app_notification_email" filter to be useful in this regard, you'll find it on line 4474 of appointments.php.

    As for the content of the notification email you spoke of, you'd want to check out the "app-messages-notification-body" filter for that one, you'll see it mentioned on line 4486 of appointments.php.

    Hope this helps! :slight_smile:

    Kind Regards,
    Michael

  • Guigro

    Hello Michael !

    Sorry for my late reply.
    I think m lack in coding makes me fail this one.

    Is that code the right one ?

    add_filter('app-messages-notification-body', 'wpmudev_app_custom_message',10, 3);
    function wpmudev_app_custom_message($message, $r, $app_id){
    
    	global $appointments;
    	global $wpdb;
    	$message =  "MY MESSAGE";
    
    	return $message;
    
    }

    I'm a bit lost in all this :disappointed:
    Thanks a lot !
    Guillaume

  • Guigro

    OH yes it's working !!!!

    And as an additional tool for everyone that needs that, here is the code to send the "An Appointment requires your approval" notification message to custom mail address (and multiple mails addresses !) :

    add_filter('app_notification_email', 'wpmudev_app_custom_mail',10, 3);
    function wpmudev_app_custom_mail($mails, $r, $app_id){
    
    	global $appointments;
    	global $wpdb;
    	$mails =  "mail1@example.com,mail2@example.com,mail3@example.com";
    
    	return $mails;
    
    }
  • Guigro

    And thanks for the answer, that one was so FAST !!

    And then for another tip (one more !), here is how to customize the subject of that mail.
    Then below my own full organization and WHY i needed that :wink:

    add_filter('app-messages-notification-subject', 'wpmudev_app_custom_subject',10, 3);
    function wpmudev_app_custom_subject($sujet, $r, $app_id){
    
    	global $appointments;
    	global $wpdb;
    	$sujet =  "MY OWN SUBJECT";
    
    	return $sujet;
    
    }

    More information about what I succeeded tonight :
    I have Sales Rep. On my website customers can asked to be called by those sales rep.
    The first time a Customer register and logs in, he has no dedicated sales rep. So the schedule displayed is the one that mixes all my sales rep schedules.

    The first time an appointment is confirmed, we assign a sales rep to that client.

    The client is supposed to ask for new calls (= appointments). So when he goes back on the "Make an appointment page", it displays only the schedule from the sales rep that he is assigned to (through a custom field on the client's back-office's screen).

    When the client asks for a new call, I wanted the name of the sales rep be included in the "An appointment requires your approval" not to make mistakes and to let my sales rep check quickly if they are related to the new appointment.

    Then I figured it out that it would even be better if the mail related to an appointment they are already assigned to could be sent to them directly, it would even be better !
    So here is how I did this :wink:

    1/ Customize the mail address regarding the sales rep.
    I always keep a general contact and appointment mail address, because if there is no sales rep assigned I don't want to miss a new client !

    (all codes needs to be included in a functions.php theme file or even better in a specific plugin)

    // First I add the filter related to the mail address where the notification mail is sent
    add_filter('app_notification_email', 'wpmudev_app_custom_mail',10, 3);
    
    // Then I call the function
    function wpmudev_app_custom_mail($mails, $r, $app_id){
    
             // Basic appointment and Wordpress database inclusion
    	global $appointments;
    	global $wpdb;
    
            // Specific inclusion to be sure that Appointment information are included and available to be used
    	$r = $wpdb->get_row( $wpdb->prepare("SELECT * FROM <code>wp_app_appointments</code> WHERE ID=%d", $app_id) );
    
            // Get the ID of my sales rep for this appointment to use it
    	$id_salesrep = $r->worker;
    
            // Find the sales rep information through his ID
    	$salesrep = get_user_by( 'id', $id_salesrep  );
    
             // Define the list of mails that will receive this notification
    	$mails =  "appointments@mywebsite.com,". $salesrep->email ."";
    
             // Return that list of mail in the right field
    	return $mails;
    
    }

    2/ Customize the subject of the notification mail
    What would be great, is that in the subject of the mail, I could add the first name of my sales rep. That's for more proximity with them, and to check from one eye in my appointment@mywebsite.com mailbox which sales rep is related to which request.

    (I will not comment all lines as I did it in my previous code portion)

    add_filter('app-messages-notification-subject', 'wpmudev_app_custom_subject',10, 3);
    function wpmudev_app_custom_subject($sujet, $r, $app_id){
    
    	global $appointments;
    	global $wpdb;
    	$r = $wpdb->get_row( $wpdb->prepare("SELECT * FROM <code>wp_app_appointments</code> WHERE ID=%d", $app_id) );
    	$id_salesrep = $r->worker;
    	$salesrep = get_user_by( 'id', $id_salesrep  );
    	$subjet =  "". $salesrep->first_name .", someone wants to speak with you !";
    
    	return $sujet;
    
    }

    3/ Customize the message that is sent
    The basic notification mail is quite small and does not provide a lot of information. Let's improve it a little bit.

    add_filter('app-messages-notification-body', 'wpmudev_app_custom_message',10, 3);
    function wpmudev_app_custom_message($message, $r, $app_id){
    
    	global $appointments;
    	global $wpdb;
    	$r = $wpdb->get_row( $wpdb->prepare("SELECT * FROM <code>wp_app_appointments</code> WHERE ID=%d", $app_id) );
    		 $id_salesrep = $r->worker;
    		 $salesrep = get_user_by( 'id', $id_salesrep  );
    		 $client = get_user_by( 'id', $r->user );
    	$message =  "
    	Information about the request :
    		- Dedicated sales rep : <b>". $salesrep->first_name ." ". $salesrep->last_name ."</b>
    	- ID : ". $r->ID ."
    	- Client : ". $r->name ."
    	- Date : ".date_i18n( '\L\e d/m/Y', strtotime($r->start))."
            - Heure : ".date_i18n( 'H\hi', strtotime($r->start))."
    
    	To confirm or edit that appointment : <a href='http://yourwebsite.com/wp-admin/admin.php?page=appointments&type=pending'>http://yourwebsite.com/wp-admin/admin.php?page=appointments&type=pending</a>";
    
    	return $message;
    
    }

    Be careful : I format time to my language (French) so it might not fit your needs, just adjust the strtotime function !

    As WPMUDEV brings me a lot, I hope this will help at least one person :wink:

    Cheers !
    Guillaume

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.