Sending One Time Password via SMS Using Rails3 and SMSAPI

Hi friends,

My need was send one time password via e-mail/sms ,who registered with their E-mail/mobile no.

Lets see “How to send SMS using Rails3 application and smsAPI”

At first we need a smsapi to send SMS via Rails3 application. There is a site, which provides FREE API to send SMS via programming. So, here we need to register with your Domain and mail-id. They also provide codes for various languages 🙂

EX code for RUBY:

require 'net/http'
require 'uri'
Net::HTTP.get_print URI.parse('')

We just modify the code, what ever our application needs,

require 'net/http'
require 'uri'
pass = "Hi+This+is+ur+one+time+passwd:" + rand(999999).to_s
mobile = ARGV.shift
url = "{pass}&senderid=slash&recipient=#{mobile}"
Net::HTTP.get_print URI.parse(url)
Then create a new rails project using,
  $ rails new sms
Go to project directory using,
  $ cd sms
Create scoffold using,
  $ rails generate scaffold name:string phone:string
Now, we have to edit the user controller
  $ gedit /app/controller/users_controller.rb
# POST /users
# POST /users.xml
def create
@user =[:user])


respond_to do |format|
format.html { redirect_to(@user, :notice => 'User was successfully created.') }
format.xml  { render :xml => @user, :status => :created, :location => @user }
format.html { render :action => "new" }
format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
Now, we have to write a small def function into that “usres_controller.rb” file
# POST /users
# POST /users.xml
def create
@user =[:user])

smspasswd  #Note this Function

respond_to do |format|
format.html { redirect_to(@user, :notice => 'User was successfully created.') }
format.xml  { render :xml => @user, :status => :created, :location => @user }
format.html { render :action => "new" }
format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }

def smspasswd
name =
pass = "Hi+#{name},+This+is+ur+one+time+passwd:" + rand(999999).to_s
phone =
url = "{pass}&senderid=slash&recipient=#{phone}"
Net::HTTP.get_print URI.parse(url)

That’s all
Sample Application:
Download from GitHub link:
      But, This “freesmsapi” does not work with the numbers who activated DND support. 😦

Power of PYTHON and RUBY languages

Hi friends,

Since last week i had worked with Send SMS via rails application. After long search, i found a site called “” which provides API and sends SMS. This site  is very much helpful in sending automated SMS.

They also provide  codes for different languages like “Python, Ruby, PHP, Perl, Java, C, C#, VB.NET”

When i compared the same code for different languages, I was surprised (shocked 😛 )to see that PYTHON and RUBY has the minimal and very efficient code. 🙂


require 'net/http'
require 'uri'
Net::HTTP.get_print URI.parse('<a href=""></a>')


from urllib
import urlopen
print urlopen('').read()

Here are the codes for other languages.


echo file_get_contents("".urlencode('YOUR MESSAGE')."&senderid=YOUR_SENDERID&recipient=MOBILE_NUMBERS");


use strict;

use LWP::UserAgent;

my $ua = new LWP::UserAgent;
my $url='';
my $request = new HTTP::Request('GET', $url);
my $response = $ua->request($request);
my $content = $response->content();
print $content;



public class JavaGetUrl {
public static void main(String[] args) throws Exception {
URL myurl = new URL("");
BufferedReader in = new BufferedReader(
new InputStreamReader(

String inputLine;

while ((inputLine = in.readLine()) != null)



#include <curl/curl.h>

void main() {

long http_code = 0;
CURL *curl;
CURLcode res;

//Initializing the CURL module
curl = curl_easy_init();

if(curl) {

curl_easy_setopt(curl, CURLOPT_URL, "");
res = curl_easy_perform(curl);

if(CURLE_OK == res) {
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);

if(http_code == 200) {
puts("Received 200 status code");
} else {
puts("Did not received 200 status code");
} else {
puts("Could not initialize curl");


using System.IO;
using System.Net;
string connectionString = "";
System.IO.Stream SourceStream = null;
System.Net.HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(connectionString);
myRequest.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse webResponse = (HttpWebResponse)myRequest.GetResponse();
SourceStream = webResponse.GetResponseStream();
StreamReader reader = new StreamReader(webResponse.GetResponseStream());
string str = reader.ReadLine();
catch (Exception ex)
Imports System.IO
Imports System.Net
Dim connectionString As String = ""
Dim SourceStream As System.IO.Stream
Dim myRequest As System.Net.HttpWebRequest = WebRequest.Create(connectionString)
myRequest.Credentials = CredentialCache.DefaultCredentials
Dim webResponse As WebResponse = myRequest.GetResponse
SourceStream = webResponse.GetResponseStream()
Dim reader As StreamReader = New StreamReader(webResponse.GetResponseStream())
Dim str As String = reader.ReadLine()

Catch ex As Exception
End Try


Sending SMS via “”

Hi friends,

I found a site which sends sms via website and programs.

They provides their own API and programing codes in ruby,,c,c#., etc.,

First we have to signup with our own domain and register with mail-id and phone number. that’s all. 🙂

Ex code in ruby:

require 'net/http'
require 'uri'
Net::HTTP.get_print URI.parse('<a href="" target="_blank">http://s1.<wbr><wbr>skey=<wbr>8c0185641435ac8a43cf10230a1417<wbr>ac&message=Password:7876&<wbr>senderid=fsmsapi&recipient=<wbr>9943239066</wbr></wbr></wbr></wbr></wbr></wbr></a>')

Now i’m trying to integrate this code into rails3 🙂

How to Send a mail using Gmail account with Rails3, ActionMailer and SMTP

Hi friends,

Action Mailer:

Action Mailer is a framework for building e-mail services. You can use Action Mailer to receive and process incoming email and send simple plain text or complex multipart emails based on flexible templates.

Lets see how to setup the actionmailer with Gmail SMTP.

First we have to create a rails application using

   $ rails new action_mailer

Next we’ll generate a scaffold for a User model with name and email attributes to act as a simple user-registration page.

$ rails genetate scaffold user name:string email:string

Now we have to migrate with DB.

$ rake db:migrate

The generated scaffolding code includes a page for creating users. We want to create a new user and then send them a confirmation email when the form on this page is submitted.

Now, just run the rails server command,

$ rails s

Then, open the “http://localhost:3000

Adding New User

The first thing we’ll do is create a new initializer file called setup_mail.rb and put some configuration options in it. ActionMailer will use sendmail if it’s set up on your machine but we can instead specify SMTP settings in the initializer.

  $ cd /config/initializers/

  $ gedit setup_mail.rb

ActionMailer::Base.smtp_settings = {  
      :address              => "",  
      :port                 => 587,  
      :domain               => "",  
     :user_name            => "thasuresh", #Your user name
      :password             => "actionmailer", # Your password
      :authentication       => "plain",  
      :enable_starttls_auto => true  

You’ll probably want to use a different approach in a production application but this is a good enough approach while our application is in development. Obviously you’ll want to change the domain, user_name and password options to suit your own Gmail account.

Now that we’ve completed the configuration we can generate a new mailer with the following code:

  $ rails generate mailer user_mailer

$ cd /app/mailer

  $ gedit user_mailer.rb

class UserMailer < ActionMailer::Base
default :from => ""

def registration_confirmation(user)
mail(:to =>, :subject => "Registered")

Now we have to create a file called registration_confirmation.text.erb under app/views/user_mailer.

  $ gedit app/views/user_mailer/registration_confirmation.text.erb

Thank you for registering!

/app/controllers/users_controller.rb  (Note the 6th line)

def create
@user =[:user])

respond_to do |format|
UserMailer.registration_confirmation(@user).deliver  #include this line in your users_controller.rb file
format.html { redirect_to(@user, :notice => 'User was successfully created.') }
format.xml  { render :xml => @user, :status => :created, :location => @user }
format.html { render :action => "new" }
format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }

mail sent by "ActionMailer"

If you want add user name at the mail, add the following line into registration_confirmation.text.erb


<%= %>,

Thank you for registering!

Added User name

Now if you want add any attachments in the mail add this line into /app/mailer/user_mailer.rb

def registration_confirmation(user)
@user = user
attachments["rails.png"] ="#{Rails.root}/public/images/rails.png")
mail(:to => "#{} <#{}>", :subject => "Registered")

Here my need is send a numeric password, when a user subscribe with his mail id. The password will send by actionmailer. So i added the a single ruby command into

Hi <%= %>,

This is the one time password, You can login into our site using the password given below

Password: <%=rand(999999)%>

Thank You For Registering!

Final output 🙂

That’s all !!!

Here sample “actionmailer” application and which deployed in heroku

GitHub Account link:


Hi frinds,

I created a module for random password generation using “Actionmailer” in rails 3  🙂

And also I’m trying to find a solution for sending password to mobile via sms.

but i didn’t succeed so for. 😦

I think their is no sms provider in india. If anyone know, pls let me know  !

check this link

I’ll tell the simple procedure to play wit actionmailer on my next post 🙂