Send Twilio sms to user based on the users phone number (field)

Public

Set up the Twilio client and library in your module file.

</> CopyGet raw version
php
  1. require 'twilio/Twilio.php';
  2.  
  3. function send_sms($to, $message, $from = '555-555-5555') {
  4. $AccountSid = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
  5. $AuthToken = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
  6. $twilio_client = new Services_Twilio($AccountSid, $AuthToken);
  7. $sms = $twilio_client->account->messages->sendMessage(
  8. $from,
  9. $to,
  10. $message
  11. );
  12. }

Fire the function based on a users field_phone

</> CopyGet raw version
php
  1. $user = user_load($uid);
  2. $to = $user->field_phone['und'][0]['value'];
  3. $message = 'This is my message yo! I learned this on Dropbucket!';
  4. send_sms($to, $message);

Remove all formatting from phone number

</> CopyGet raw version
php
  1. function phone_clean_check($user) {
  2. $phone = $user->field_phone['und'][0]['value'];
  3. if(!is_numeric($phone)) {
  4. $phone = preg_replace('/\D+/', '', $phone);
  5. (int)$phone;
  6. $edit['field_phone']['und'][0]['value'] = $phone;
  7. user_save($user, $edit);
  8. }
  9. return $phone;
  10. }