SMS in Drupal

By shane
Fri, 2011-03-11 23:40
12 comments

Share with Others

Recently I created the website that gives users an automated way to periodically check their websites to make sure they are still up and running. If the website is down or returns any type of error code, then it will send either an email or SMS notification to the user.

Up until this point I had never set up SMS notifications or SMS messaging on Drupal so here is some information on how I got it to work.

Download and Install Modules

Here are the modules that I used to get SMS notifications to work correctly:

I turned on the following modules on admin/build/modules page (you can also use drush if you have it installed... if you don't have it installed, you should try it!):

  • Messaging
  • Simple Mail
  • SMS Messaging
  • SMS Framework
  • SMS User
  • Tracking - useful for debugging your first few SMS messages if something goes wrong
  • Twilio - my gateway of choice for SMS (you can also use the Email Gateway but see the note below). You will need to download the Twilio PHP library for this to work.


Note: You can get started sending SMS for free if you do not want to use a service such as Twilio. You can simply turn on the Email gateway module and you will not need any additional SMS service to relay the message. However, as of last week when I tried this, the SMS framework's email gateway and the messaging module were not playing nicely together. Basically any of my notifications sent through the messaging module using SMS were not being delivered. There are a few issues in the drupal.org issue queue on this. If you are looking to send SMS notifications using the messaging module you will either need to find another service or spend some time trying to find a solution to the issue.

Module Configuration

Now you will need to configure the Carrier configuration, Gateway configuration, and SMS User sections in the SMS administration menu. If you are using Twilio, you will need to add your Twilio API settings. If you do not have a Twilio account you will need to create one. When I created my Twilio account it gave me $30 free and it costs $0.02 per sent SMS text message. In my case I only needed to send SMS and did not need to receive SMS messages from the website (although it could be set up to receive and act on SMS messages).

Next I modified the user permissions to allow certain roles to receive SMS messages. In the sms_user module permissions there are the following permissions to set:

  • edit own sms number
  • receive sms

Testing SMS

You will now be able to go to your "My Account" page, click Edit, then click Mobile. You should be able to enter in your area code and phone number (I used all numbers with no spaces or dashes), and click Confirm Number. If all goes well you should be getting an SMS text message within a few seconds containing a validation code. You will be able to enter this validation code in your My Account page to confirm your mobile number.

Messaging integration

Now we have confirmed that SMS is working, however in order for it to be useful, you may need it to send out SMS notifications. I accomplished this by using the Messaging module. I created a custom module implementing hook_messaging() but you could also use the notifications module to schedule SMS notifications to be sent (The SMS Send to phone module that comes with the SMS Framework may also allow you to do this). You just need to modify your account settings to set the default notification type from Mail to SMS.

That is about all there is to getting SMS setup on a Drupal website. Quick and easy.

I will create another post with some of my code for implementing hook_messaging in the near future. It has been useful for me in many situations in which I need to send out custom notifications using the messaging module.

If you have any questions or need help, let me know in the comments.

Comments

Thank you for the post. Good job and well explained as always.How can we do it on drupal 7?

I got lost at the part where it was demanding a Twilio number. I'm wanting to try this first before paying for it, so I wasn't sure what number to put in there. I put in Twilio's sandbox # plus the 8 digit PIN. No dice though!

Got any suggestions?

I believe when I set up my Twilio account, they gave me a certain amount of SMS texts free. This way I was able to fully test without actually paying for SMS messages. The other thing that I also ran into issues with was how the number was input into the text fields. I believe it needs to be inserted without any spaces. I also think you need to create the account to get the Account SID and API ID but I could be wrong on that one.

Let me know if that helps.

I managed to get it to send me text messages yesterday (the sandbox number w/o the PIN) and even configured Rules to send a message when someone signs up for an event. I now need to see if I can use tokens in the rule so the message will say, "Thank you for registering for [token]."

I wish I had found your entry sooner since I spent 3 days trying to get something like this going. With your entry, I was able to send text messages within an hour.

In the Twilio configuration page there are three fields: Account SID, API ID and Number. Where can I find the number I have to enter on the third field?

You should be able to get the Twilio number after you create your Twilio account. I remember that when I initially created my account I had to look around a little to figure out how to get the number set up. Basically you need to set up a telephone number within the Twilio account to use on the Drupal site. After you set up the number on your Twilio Account, you can put it in the third field on the SMS Twilio configuration page (without any spaces or parenthesis).

Let me know if you still are having trouble.

Thank you for your post. I got it installed. I am able to send sms messages from my website, but can we send messages from cell phone to the website? I installed an incoming patch that starts to do this, but the messages just go to a hook and nothing happens. Does anyone know if incoming works. Thank you in advance.

Thank you for the post. Good job and well explained as always.How can we do it on drupal 7?

Post new comment