How do you send a txt message to a cell phone from a Lucee web app?

Viewed 605

I would like my application to send a text message to users on certain triggers, preferably using something like a cfmail tag. I've never had to send text message from a web app before, but given the huge number of mobile devices out there today I assumed this would be built into CF/Lucee if I ever needed it. However, now that I do I'm not seeing anything in the docs or first few pages of Google.

Is it possible to send text messages directly from Lucee? I know that I could use cfmail to send messages to the carrier's gateway (ie: xxxxxxxxxx@tmomail.net), but that requires me to know and maintain a list of every carrier's address scheme and know which carrier the recipient is with, which I can't. Is what I want to do only possible with a third party service?

3 Answers

I'm not aware of any way to do it natively in ColdFusion, but I've used Twilio to send SMS messages from ColdFusion using the Twilio API: https://www.twilio.com/sms

They offer free developer accounts so you can try before you buy.

Virtually all mobile carriers can receive an email and forward it on as a text (SMS) message.

We keep a DB table populated with all current carriers and their email-to-text domains. A user can update their profile with their cell number and have to select a mobile carrier before they get an option to receive text messages.

From AT&T's site:

Text message - Compose a new email and enter the recipient's 10-digit wireless number, followed by @txt.att.net. For example, 5551234567@txt.att.net.

Picture or video message - Compose a new email and enter the recipient's 10-digit wireless number, followed by @mms.att.net. For example, 5551234567@mms.att.net.

You simply use <cfmail> to send text messages as if they were normal emails.

Twilio (https://www.twilio.com/try-twilio) makes it easy to send text messages. All you need to do is to make an HTTP POST request.

When you successfully send a message, Twilio responds with data about the process, including a message SID (System ID).

Here is some code which you can put in a .cfm page and run it to send a message. Replace the three PLACEHOLDERS with your Twilio values.

You'll find your Twilio credentials ACCOUNT_SID and AUTH_TOKEN, on your "Dashboard" after you signup/login at Twilio.

YOUR_TWILIO_PHONE_NUMBER should start with +.



<cffunction name="sendMessageWithTwilio" output="false" access="public" returnType="string">
    <cfargument name="aMessage" type="string" required="true" />
    <cfargument name="destinationNumber" type="string" required="true" />

    <cfset var twilioAccountSid = "YOUR_ACCOUNT_SID" />
    <cfset var twilioAuthToken = "YOUR_AUTH_TOKEN" />
    <cfset var twilioPhoneNumber = "YOUR_TWILIO_PHONE_NUMBER" />

    <cfhttp 
        result="result" 
        method="POST" 
        charset="utf-8" 
        url="https://api.twilio.com/2010-04-01/Accounts/#twilioAccountSid#/Messages.json"
        username="#twilioAccountSid#"
        password="#twilioAuthToken#" >

        <cfhttpparam name="From" type="formfield" value="#twilioPhoneNumber#" />
        <cfhttpparam name="Body" type="formfield" value="#arguments.aMessage#" />
        <cfhttpparam name="To" type="formfield" value="#arguments.destinationNumber#" />

    </cfhttp>

    <cfif result.Statuscode IS "201 CREATED">
        <cfreturn deserializeJSON(result.Filecontent.toString()).sid />
    <cfelse>
        <cfreturn result.Statuscode />
    </cfif>

</cffunction>

<cfdump var='#sendMessageWithTwilio(
    "This is a test message.",
    "+17775553333"
)#' />


Related