4

How to print each string in a new line in an email using AWS SNS service.

If I print a message in Python output all strings is in new lines:

Started copying.. snapshot_id: snap-000000aeaada0000 from: region_src to: region_dst with new snapshot_id: new_snapshot_id
Started copying.. snapshot_id: snap-000000aeaada0001 from: region_src to: region_dst with new snapshot_id: new_snapshot_id

but in an email it is all in one line:

Started copying.. snapshot_id: snap-05b3834aeaada5a02 from: region_src to: region_dst with new snapshot_id: new_snapshot_id Started copying.. snapshot_id: snap-012d3db747de08d1f from: region_src to: region_dst with new snapshot_id: new_snapshot_id
import boto3

region_src = 'us-east-1'
sns_arn = "arn:aws:sns:us-east-1:000000000099:AWS_LAMBDA_NOTIFICATIONS"

def copy_snapshot_src_to_dst(snapshot_id):
    message = ("Started copying.. snapshot_id: " + str(snapshot_id) + " from: " + "region_src" + " to: " + "region_dst" + " with new snapshot_id: " + "new_snapshot_id")
    #print(message)

    return message

def lambda_handler():
    snapshots_id = [('snap-000000aeaada0000', [{'Key': 'Backup_Type', 'Value': 'Demo'}, {'Key': 'Backup Initiator Rule', 'Value': 'Daily-6d-retention'}, {'Key': 'Disaster_Recovery', 'Value': 'Full'}, {'Key': 'aws:backup:source-resource', 'Value': 'FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF'}, {'Key': 'Name', 'Value': 'HOSTNAME'}]), ('snap-000000aeaada0001', [{'Key': 'Name', 'Value': 'HOSTNAME'}, {'Key': 'Backup Initiator Rule', 'Value': 'Daily-6d-retention'}, {'Key': 'Backup_Type', 'Value': 'Demo'}, {'Key': 'Disaster_Recovery', 'Value': 'Full'}, {'Key': 'aws:backup:source-resource', 'Value': 'FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF'}])]
    message = ""

    for i in snapshots_id:
        snapshot_id = i[0]
        message += copy_snapshot_src_to_dst(snapshot_id) + '\n'
    print(message)
    send_sns(message)

def send_sns(message):
    if sns_arn:
        print("Sending SNS alert")
        sns = boto3.client("sns", region_name=region_src)
        response = sns.publish(
            TargetArn=sns_arn,
            Subject=("AWS LAMBDA NOTIFICATION"),
            Message=(message)
        )


lambda_handler()
Orest Gulman
  • 422
  • 1
  • 8
  • 25

2 Answers2

4

replace '\n' by ".\n" and after that in an email - each string is in a new line.

Orest Gulman
  • 422
  • 1
  • 8
  • 25
  • Found the [reason](https://stackoverflow.com/questions/136052/how-do-i-format-a-string-in-an-email-so-outlook-will-print-the-line-breaks) for others interested. Based on that link, I used "\t\n" which works just as well and doesn't leave you with the visible period. – Rob Davis Jun 22 '21 at 03:16
2

Check out this: AWS SNS how to add line breaks in message

An option to consider is using SES to send email instead. See https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ses.html#SES.Client.send_email. Then you could send an email with HTML formatting, which makes it easy to use a table or break tags to format things:

client = boto3.client('ses', region_name='us-east-1')    
response = client.send_email(
Source='string',
Destination={
    'ToAddresses': [
        'string',
    ],
    'CcAddresses': [
        'string',
    ],
    'BccAddresses': [
        'string',
    ]
},
Message={
    'Subject': {
        'Data': 'string',
        'Charset': 'string'
    },
    'Body': {
        'Text': {
            'Data': 'string',
            'Charset': 'string'
        },
        'Html': {
            'Data': 'string',
            'Charset': 'string'
        }
    }
},
ReplyToAddresses=[
    'string',
],
ReturnPath='string',
SourceArn='string',
ReturnPathArn='string',
Tags=[
    {
        'Name': 'string',
        'Value': 'string'
    },
],
ConfigurationSetName='string'

)

Per "How is Amazon SES different from Amazon SNS?" at https://aws.amazon.com/ses/faqs/

"Amazon SES is for applications that need to send communications via email. Amazon SES supports custom email header fields, and many MIME types.

By contrast, Amazon Simple Notification Service (Amazon SNS) is for messaging-oriented applications, with multiple subscribers requesting and receiving "push" notifications of time-critical messages via a choice of transport protocols, including HTTP, Amazon SQS, and email. The body of an Amazon SNS notification is limited to 8192 characters of UTF-8 strings, and isn't intended to support multimedia content."

Shawn
  • 8,374
  • 5
  • 37
  • 60