Python boto3 SNS 电子邮件格式(每个字符串换行)
Python boto3 SNS email formatting (each string in new line)
如何使用 AWS SNS 服务在电子邮件的新行中打印每个字符串。
如果我在 Python 中打印一条消息,输出所有字符串都是新行:
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
但在一封电子邮件中,所有内容都在一行中:
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()
看看这个:
要考虑的一个选项是改用 SES 发送电子邮件。参见 https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ses.html#SES.Client.send_email。然后您可以发送一封带有 HTML 格式的电子邮件,这使得使用 table 或中断标签来格式化内容变得容易:
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'
)
每 "How is Amazon SES different from Amazon SNS?" 在 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."
将 '\n'
替换为 ".\n"
,然后在电子邮件中 - 每个字符串换行。
如何使用 AWS SNS 服务在电子邮件的新行中打印每个字符串。
如果我在 Python 中打印一条消息,输出所有字符串都是新行:
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
但在一封电子邮件中,所有内容都在一行中:
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()
看看这个:
要考虑的一个选项是改用 SES 发送电子邮件。参见 https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ses.html#SES.Client.send_email。然后您可以发送一封带有 HTML 格式的电子邮件,这使得使用 table 或中断标签来格式化内容变得容易:
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'
)
每 "How is Amazon SES different from Amazon SNS?" 在 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."
将 '\n'
替换为 ".\n"
,然后在电子邮件中 - 每个字符串换行。