0

I have written a code in which I am using aws sns to send email notifications to user via boto3 client (sns) library.

My issue is that I was unable to convert the Message to HTML. The recipients should receive a HTLML notification, instead of plain text. Is there away to modify my code here. I appreciate any solution Here is my code:

import boto3
import json
from json2table import convert

SNS_TOPIC_ARN_Critical = 'SNSTOPIC'

def lambda_handler(event, context):
 
  build_direction = "LEFT_TO_RIGHT"
  table_attributes = {"style" : "width:100%", "display": "table", "border-collapse": "collapse","border-color": "gray", "border": "1px solid black"}
             
  response = boto3.client('sns').publish(
      TopicArn = SNS_TOPIC,
      Message = convert(json.dumps(event, indent=2),build_direction=build_direction, table_attributes=table_attributes),
      Subject = 'Test Alert',
      MessageStructure = 'html'
    )
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
elephant
  • 1
  • 1
  • 2

2 Answers2

0

Last I checked, HTML was not a supported format option for SNS emails. You may want to look into SES, which is more feature-rich when it comes to emails.

There's also a similar question on here related to this: Sending html content in AWS SNS(Simple Notification Service) emails notifications

peter n
  • 1,210
  • 13
  • 18
  • Thank you for prompt response. Is it possible to edit code above to use SES. My only option is to consume ARNSNSTopic – elephant Jul 09 '20 at 07:07
  • Unfortunately with that limitation, you may not have any option but what SNS offers. If HTML are a top requirement, you may need to re-examine the notification solution, or as suggested in the post I linked, sending a link to prepped HTML content – peter n Jul 09 '20 at 15:25
0

As of March 23, 2023, Amazon SNS now supports custom Content-Type headers for HTTP messages delivered from topics. Here's the What's New post: https://aws.amazon.com/about-aws/whats-new/2023/03/amazon-sns-content-type-request-headers-http-s-notifications/

You will have to modify the DeliveryPolicy attribute of your Amazon SNS subscription, setting the headerContentType property to text/html, or any other value supported. You can find all values supported here: https://docs.aws.amazon.com/sns/latest/dg/sns-message-delivery-retries.html#creating-delivery-policy

{
    "healthyRetryPolicy": {
        "minDelayTarget": 1,
        "maxDelayTarget": 60,
        "numRetries": 50,
        "numNoDelayRetries": 3,
        "numMinDelayRetries": 2,
        "numMaxDelayRetries": 35,
        "backoffFunction": "exponential"
    },
    "throttlePolicy": {
        "maxReceivesPerSecond": 10
    },
    "requestPolicy": {
        "headerContentType": "text/html"
    }
}

You set the DeliveryPolicy attribute by calling either the Subscribe or the SetSubscriptionAttributes API action:

Otavio Ferreira
  • 755
  • 6
  • 11