SMS in Drupal

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

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 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.