Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

The documentation suggests to use message attributes for that but I can't seem to figure out what attribute name to use.

This works so far:

sns = boto3.client('sns', region_name='eu-west-1')

sns.publish(
  PhoneNumber='+491701234567',
  Message='hi there',
  MessageAttributes={
    'AWS.SNS.SMS.SenderID': {
      'DataType': 'String',
      'StringValue': 'MySenderID'   
    }    
  }   
)  

The SMS is delivered but with some (random?) value in the sender id field. So it seems my setting of message attributes is silently ignored. What is the correct way to set a custom sender id?

like image 681
tgal Avatar asked Sep 06 '25 09:09

tgal


1 Answers

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'}})

enter image description here

like image 190
Jossef Harush Kadouri Avatar answered Sep 09 '25 03:09

Jossef Harush Kadouri