my.sendSMS

Call this API to send a SMS to contacts.

  • Supported SDK versions:
    • Android: 2.67.0
    • iOS: 2.77.0
  • Supported types of mini programs: DSL,H5+

Sample code

See the following JavaScript sample code:

DSL

copy
my.sendSMS({
  mobile: '138*****0**',    // contact mobile number(optional)
  content: 'verification code: 1234',  // message contenat(optional)
  success: (res) => {
    console.log('Message send result:', res.status);
  },
  fail: (err) => {
    console.log('Failed to send:', err.errorMessage);
  }
});

H5+

copy
AlipayJSBridge.call('sendSMS', {mobile:'138*****0**',content: 'verification code: 1234',}, function (data) {
    alert('Result' + ':\n' + JSON.stringify(data));
});

Parameters

The incoming parameter is of the Object type with the following properties:

Property

Data type

Required

Description

mobile

String

No

The contact's mobile phone number.

Leave it empty if not provided.

content

String

No

The SMS content.

Leave it empty if not provided.

success

Function

No

The callback function to be executed when the request is successful. For more information, see success callback function.

fail

Function

No

The callback function to be executed when the request is failed.

complete

Function

No

The callback function to be executed when the request is completed (to be executed whether the request is successful or failed).

success/fail/complete callback function

The following table provides the properties in the success/fail/complete callback function:

Property

Data type

Description

status

String

This parameter indicates the status of the SMS delivery:
Success: SMS sent successfully
Cancelled: User cancelled the send request.
Failed: SMS sending failed.

Note: For Android environment, Both successful sending and user cancellation trigger the success callback with parameter { success: true }, without including the status field.

Error codes

Error code

Error message

Further action

2

Invalid parameters

Check parameter types to ensure data is of dictionary type (Object type).

3

Device not supported

The mobile device does not support SMS functionality. Need to remind the user accordingly.

22

User cancelled

The user actively cancelled the SMS sending request.

FAQs

Why did the my.sendSMS JSAPI test fail on the simulator?

For details, see FAQ.

How to prevent users from sending message repeatedly?

For details, see FAQ.

Why is the mobile number parameter (mobile) ignored?

For details, see FAQ.