18

I have sent confirmation e-mails to users by using nodemailer for my project. It is working fine.

Now I am looking to send verification codes to mobile numbers from node.js, but I don’t know how to do that.

Is there any module to send verification codes to mobile numbers, like nodemailer does with e-mail addresses? Or if not, how can I do this myself?

I developing my project using node.js and mongodb, JavaScript and jQuery.

Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
silvesterprabu
  • 1,417
  • 10
  • 30
  • 46

6 Answers6

5

Most carriers provide a SMS gateway to which you can send an email and have it arrive as SMS. If you want a free way of sending SMS that works with your current nodemailer implementation, this is probably your best option. Otherwise, you might want to search for paid SMS services that you can integrate with.

Here is a list of SMS gateways: http://en.wikipedia.org/wiki/List_of_SMS_gateways

From the linked Wikipedia page: For instance, to send to a number typically expressed in the USA as 987-555-0100, one would email 9875550100@SMS-gateway.

JWK
  • 670
  • 2
  • 6
  • 16
  • k thanks.but how to use that nodemailer to send sms to mobile number. – silvesterprabu Feb 09 '13 at 15:25
  • From the linked Wikipedia page: For instance, to send to a number typically expressed in the USA as 987-555-0100, one would email 9875550100@SMS-gateway. – JWK Feb 09 '13 at 20:28
  • nodemailer.createTransport("SMTP",{ service: "Gmail", auth: { user: "xxxx@gmail.com", pass: "xxxxxxxx" } }); – silvesterprabu Feb 11 '13 at 07:52
  • mailOptions = { from: "Fred Foo ✔ ", // sender address to: "number@aircel.co.in", // list of receivers subject: "Hello ✔", // Subject line text: "Hello world ✔", // plaintext body html: "Hello world ✔" // html body } i tried this..but i could not have received that msg... – silvesterprabu Feb 11 '13 at 07:54
  • I'm not sure. I used the SMS gateways for some US carriers a while back and it worked. If this free approach doesn't work for you, you might have to search for some SMS API provider. They typically charge a low fee though. – JWK Feb 11 '13 at 13:51
5

NodeJS package https://www.npmjs.com/package/springedge will be easy to send sms. You can install as

npm install springedge

Code example of sending sms:

// send sms 

var springedge = require('springedge');

var params = {
  'apikey': '', // API Key 
  'sender': 'SEDEMO', // Sender Name 
  'to': [
    '919019xxxxxxxx'  //Moblie Number 
  ],
  'message': 'test+message'
};

springedge.messages.send(params, 5000, function (err, response) {
  if (err) {
    return console.log(err);
  }
  console.log(response);
});
BSB
  • 2,270
  • 17
  • 26
3

To ensure you can reach a users mobile no matter their location and network it's likely you're going to have to look at a paid service such as Nexmo (who I work for) or Twilio.

With these services you can either build your own verification (2FA - Two Factor Authentication) workflow:

  1. User enters their phone number in a form and submits to your app
  2. Your app receives the phone number
  3. You send the user an SMS with an auth code
  4. User receives the auth code
  5. User enters auth code into a form and submits to your app
  6. Your app receives the auth code and checks that auth code against the phone number (maybe using the current session phone number)
  7. If the auth code is the one you are expected then the user is validated

Or you can use their 2FA authentication products (Nexmo - Verify or Twilio - Authy that should help simplify this workflow.

Using Nexmo verify the code would be:

Send Verification Request

var Nexmo = require('nexmo');
var nexmo = new Nexmo({apiKey: API_KEY, apiSecret: API_SECRET});

var verifyRequestId = null; // use in the check process

nexmo.verify.request({number: TO_NUMBER, brand: APP_NAME}, function(err, result) {
  if(err) { console.error(err); }
  else {
    verifyRequestId = result. request_id;
  }
});

Check Authentication Code

nexmo.verify.control({request_id: verifyRequestId, cmd: 'cancel'}, function(err, result) {
  if(err) { console.error(err); }
  else {
    console.log(result);
  }
});
leggetter
  • 15,248
  • 1
  • 55
  • 61
1

You can use MSG 91 service. It provides OTP verification service as well.

1

One easiest way to send message to any number from node js is to use a library called fast-two-sms.

This library is easy to integrate in your project and it is also very cheap and provide free credit after signup.

Here's how you can integrate this library.

First install it:

npm install fast-two-sms
or 
yarn add fast-two-sms

Then in js file:

const fast2sms = require('fast-two-sms')

var options = {authorization : YOUR_API_KEY , message : 'YOUR_MESSAGE_HERE' ,  numbers : ['9999999999','8888888888']} 
fast2sms.sendMessage(options) 

And don't forget to generate an api key from their website.

OR

You can use firebase authentication also.

-1

For a testing you should use this API https://www.fast2sms.com however you can use it as a business too. Before running the below code make sure you have generated valid API Authorization Key and for that you should register for free.

Now place receiver's mobile number for bulk message to and you can use it as a verification and OTP purpose too.

Install npm module in your project, where you want to implement an SMS system.

npm install unirest

Code for GET method:

var unirest = require("unirest");

var req = unirest("GET", "https://www.fast2sms.com/dev/bulk");

req.query({
  "authorization": "YOUR_API_KEY",
  "sender_id": "FSTSMS",
  "message": "This is a test message",
  "language": "english",
  "route": "p",
  "numbers": "9999999999,8888888888,7777777777",
});

req.headers({
  "cache-control": "no-cache"
});

req.end(function (res) {
  if (res.error) throw new Error(res.error);

  console.log(res.body);
});

Code for POST method:

var unirest = require("unirest");

var req = unirest("POST", "https://www.fast2sms.com/dev/bulk");

req.headers({
  "authorization": "YOUR_API_KEY"
});

req.form({
  "sender_id": "FSTSMS",
  "message": "This is a test message",
  "language": "english",
  "route": "p",
  "numbers": "9999999999,8888888888,7777777777",
});

req.end(function (res) {
  if (res.error) throw new Error(res.error);

  console.log(res.body);
});
karel
  • 5,489
  • 46
  • 45
  • 50