How to send an SMS with custom sender ID with Amazon SNS and Python and boto3

☆樱花仙子☆ 提交于 2019-12-03 05:34:19

The sender id must be 1-11 alpha-numeric characters, no spaces; for example:

  • THISISME - ✅
  • TestForSO - ✅
  • StackOverflow - 🛑 (too long. max 11 chars)
  • Some one - 🛑 (no spaces)

As others mentioned, the sender id customization depends on the country / cellular provider so make sure to test it.

Example snippet

import boto3

access_key = '....'
secret = '....'
region = "us-east-1"

number = '+972...<your number>'

sender_id = 'TestForSO'
sms_message = 'Your code: 123456'

sns = boto3.client('sns', aws_access_key_id=access_key, aws_secret_access_key=secret, region_name=region)
sns.publish(PhoneNumber=number, Message=sms_message, MessageAttributes={'AWS.SNS.SMS.SenderID': {'DataType': 'String', 'StringValue': sender_id}, 'AWS.SNS.SMS.SMSType': {'DataType': 'String', 'StringValue': 'Promotional'}})

Check if your destination country supports sender IDs

http://docs.aws.amazon.com/sns/latest/dg/sms_supported-countries.html

JaminaBee

As noted by Adam Owczarczyk, some countries don't allow you to transmit a sender ID in a text message. The API will take your number and replace it with a string in this case to allow your message to get delivered. Attempting to work around it usually just gets the number blacklisted. You can test this by entering a short descriptive string for your tester ID, and seeing if that gets through.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!