Appointments+ additional email recipient

In a thread Hakan posted the following code to be added to functions.php:

wp_mail( 'number@phonecompany.com', 'Subject', $body );

to send an email via a phone company SMS gateway.

I need to change the address to "phone@smsprovider.com" where "phone" is the phone number entered by the client in Appointments+ when they made the appointment.

I have tried changing it to

wp_mail( $phone'@phonecompany.com', 'Subject', $body );

but that does not work. I am not sure if $phone is the right variable. How do I change the code to do this? TIA, Mike

  • Alexander

    Hi @mikedino,

    Just taking a quick look at the code, you need to concatenate the phone number. Notice the dot after $phone:

    wp_mail( $phone . '@phonecompany.com', 'Subject', $body );

    Using double quotes, you can use string interpolation.

    wp_mail( "$phone@phonecompany.com", 'Subject', $body );

    All that to say, this is actually a rather difficult solution to implement well, and isn't the best wayto do SMS. For example, you'll need to lookup each phone number to find out what carrier is needed, and there's not much guarantee that it will go through.

    We do have another plugin who was able to get SMS integration working out using some of Appointments+ action and filter hooks. Here's a link: https://premium.wpmudev.org/forums/topic/appointments-integration-with-sms

    You could post there to perhaps get in touch with him. An overall better SMS solution would be to use those hooks and connect to an actual SMS provided like Twillio: https://www.twilio.com/

    Best regards,

  • mikedino

    Hi Alexander,

    Many thanks for your reply and the link. I see that the SMS plug-in by that other user has not been released yet, but will try to contact him to keep up to date. I am using an email to SMS service in England called TextMarketer and the little bit of code should work fine (the service doesn't need to know which telco the end user is on). I can't get your suggestions to work though and I think it just isn't putting the phone number in properly.

    // Change 'Subject' as you wish
    function app_forward_mail( $body, $r, $app_id ) {
    wp_mail( $phone . '@textmarketer.biz', 'smstest', $body );
    // Remove comments for the below line if you want SMS for notification too
    // add_action( 'app_notification_sent', 'app_forward_mail', 10, 3 );
    add_action( 'app_confirmation_sent', 'app_forward_mail', 10, 3 );

    Even if I just try to get it to email me normally and I change the line to read:

    wp_mail( $phone . '@mydomain.com', 'smstest', $body );

    it doesn't work even though my domain will receive emails to any user. I have also tried it with the double quotes as you suggested. I know it should work because if I actually insert my real phone number as '1234567@mydomain.com' and delete "$phone"I do get the text message.

    So it seems the $phone variable insertion isn't working. Do I need to include it in the function line? (line 2). I'm hoping to launch on Monday so this is very important to me. Many thanks again, Mike

  • Alexander

    Hi @mikedino,

    Maybe there's something off with the phone number. Can you try doing a var_dump($phone) to see what that variable actually contains? This would help debug this, as you'd know if it's somehow not a phone number.

    You can also use this code to enforce a more strict validation on the phone number

    add_filter(‘app_phone_check’,'validate_app_phone’,10,2);
    function validate_app_phone($val,$phone){
    return !preg_match(‘/[^0-9\-.]/’,$phone) ? $val : false;
    }

    Best regards,

  • mikedino

    Thanks for that Alexander.

    I am not sure how to do a var_dump as I'm mainly a graphical designer. I have run the following code in functions.php and am using a hosting company so don't have direct access to the server (but do have Softaculous if that helps) but the code generates a text file:

    ob_start();
    var_dump($phone);
    if ($handle = fopen("appointerror.log", "w")) {
    	fwrite($handle, ob_get_contents());
    }
    fclose($handle);
    ob_end_clean();

    The result I get from this in the file appointerror.log is "NULL" although as I say I'm not sure I'm doing it right.

    Is there an easier way I can do a var_dump? DO we have the right variable name of "$phone" for Appointments+?

    Thanks again, Mike

  • mikedino

    Hi Alexander,

    Just to say that I've also tried changing the address line to send to my own domain and to use the $name variable rather than $phone and it still doesn't work; as follows:

    // Change 'Subject' as you wish
    function app_forward_mail( $body, $r, $app_id ) {
    wp_mail( $name . '@mydomain.com', 'smstest', $body );
    // Remove comments for the below line if you want SMS for notification too
    // add_action( 'app_notification_sent', 'app_forward_mail', 10, 3 );
    add_action( 'app_confirmation_sent', 'app_forward_mail', 10, 3 );

    The appointment booking takes place successfully but no email is received at $name@mydomain.com. Doesn't work with $phone@mydomain.com either.

    I suspect therefore that it simply isn't picking up the variable and inserting it into the email address.

  • mikedino

    The code so far is as follows, with change being addition of $phone in line 2:

    // Change 'Subject' as you wish
    function app_forward_mail( $body, $r, $phone, $app_id ) {
    wp_mail( $name . '@mydomain.com', 'smstest', $body );
    // Remove comments for the below line if you want SMS for notification too
    // add_action( 'app_notification_sent', 'app_forward_mail', 10, 3 );
    add_action( 'app_confirmation_sent', 'app_forward_mail', 10, 3 );

    This ends up going to appointmentID@mydomain.com. eg. 86@mydomain.com. I'm sure it's just calling the right variable for the phone number. Any idea what it is?

  • Alexander

    Hi @mikedino,

    Thanks for the update and good to hear you have it working. Good job working out the var_dump call, and how to view the variables.

    In case this helps, looking at your last bit of code, you've got an issue with your method parameters.

    When this action is called, it only passes three parameters:

    do_action( 'app_notification_sent', $body, $r, $app_id );

    You have four in your function, so $name in your function is actually going to be $app_id from the main code.

    $r is the table row for that appointment, so you can access any required data from there. Yet again, I'd recommend just doing a var_dump on $r to see what it contains, that way you can easily discover what properties you have to work with in the exact context you're using them.

    Best regards,

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.