// shortcode custom page
add_shortcode( 'whatever-sms', 'shortcode_to_receive_sms_to_email' );
// Shortcode implementation function
function shortcode_to_receive_sms_to_email() {
$json = file_get_contents('php://input');
// Let's convert it into a PHP object:
$data = json_decode($json);
// Let's feed the JSON data into variables:
if ( !empty($data->From) && !empty($data->To[0]) && !empty($data->Message) ) {
// Let's determine the IP address of the gateway which rallied this SMS to our server:
// whether ip is from share internet
/**if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip_address = $_SERVER['HTTP_CLIENT_IP'];
}
// whether ip is from proxy
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
// whether ip is from remote address
else {
$ip_address = $_SERVER['REMOTE_ADDR'];
}*/
$from_phone_number = preg_replace( '/[^0-9]/', '', $data->From );
$to_phone_number = preg_replace( '/[^0-9]/', '', $data->To[0] );
$message = sanitize_text_field( urldecode($data->Message) );
//below email to can either be hardcoded or pulled from database. I have a list of phone numbers and matching emails store in database.
$email_to = ; '[email protected]'//this is searched from database, it depends how you have strored phone number and matching email
$from_email = '[email protected]';
$email_subject = "SMS From:".$from_phone_number."_To:".$to_phone_number; //SMS_GWIP:".$ip_address."_
if( !empty($email_to) && !empty($from_phone_number) && !empty($to_phone_number) && !empty($message) ) {
$success = wp_mail($email_to, $email_subject, $message, $email_headers);
if($success) {
echo "OK";
//below is Wordpress specific to insert every sent sms into database for log purpose
global $wpdb;
$sms_table = $wpdb->prefix . 'table_name';
$currentdate = new DateTime( 'NOW', wp_timezone() );
$current_date = $currentdate->format('Y-m-d H:i:d');
$success = empty($success)?0:1;
$success = $wpdb->insert( $sms_table, array( 'from_number' => $from_phone_number,
'to_number' => $to_phone_number,
'message' => $message,
'receive_date' => $current_date,
'email_sent' => $success ),
array( '%s', '%s', '%s', '%s', '%d' )
);
if( !$success || $wpdb->last_error != '' ) {
// $this->wpdb->print_error();
//wp_mail($from_email, $email_subject, 'error'.$wpdb->last_error, $email_headers);
?>
<div class="error notice is-dismissible"><p><strong><?php _e( 'Oops, Data failed to save! Check settings or data you provided!', 'text-domain' ); ?></strong></p></div> <?php
} else {
//wp_mail($from_email, $email_subject, 'success', $email_headers);
?>
<div class="updated notice is-dismissible"><p><strong><?php _e( 'Success! A record has been added!', 'text-domain' ); ?></strong></p></div> <?php
}
} else {
echo "ERROR";
}
} else {
echo "ERROR";
//die( 'No script kiddies please!' );
echo "Ready to process valid numbers.";
} // End else
} else {
echo "ERROR";
//die( 'No script kiddies please!' );
echo "Ready to process valid numbers.";
} // End else
}
Link up your team and customers Phone System
Live Chat
Video Conferencing
Hosted or Self-managed. Up to 10 users free forever. No credit card. Try risk free.
Check your inbox!
We’ve sent you an email. Click on the button in the email body to verify your email address – (if you can not find it, check your spam folder).
Upon verification you will be directed to the 3CX setup wizard.