Sending SMS messages

Sending SMS messages from a web2py application requires a third party service that can relay the messages to the receiver. Usually this is not a free service, but it differs from country to country. We have tried a few of these services with little success. Phone companies block emails originating from these services since they are eventually used as a source of spam.

A better way is to use the phone companies themselves to relay the SMS. Each phone company has an email address uniquely associated with every cell-phone number, so SMS messages can be sent as emails to the phone number.

web2py comes with a module to help in this process:

  1. from gluon.contrib.sms_utils import SMSCODES, sms_email
  2. email = sms_email('1 (111) 111-1111','T-Mobile USA (tmail)')
  3. mail.send(to=email, subject='test', message='test')

SMSCODES is a dictionary that maps names of major phone companies to the email address postfix. The sms_email function takes a phone number (as a string) and the name of a phone company and returns the email address of the phone.