Have you implemented a feature that sends emails or short messages from your application? I also implemented such a feature the other day. Of course, using AWS SES and SNS. I wanted to write it in Node.js, but for various reasons I wrote it in Python. SES, SNS, SMS are complicated! I implemented it while doing so, but there were some restrictions and so on, so I left it in the article for the future ...
SES (Simple Email Service) is an email platform that allows users to send and receive emails in a cost-effective and easy way using their email addresses and domains. For the time being, a service that allows you to send and receive emails with AWS services. I'm sorry for being appropriate ...
SNS (Simple Notification Service) is a web service that coordinates and manages the delivery or sending of messages to subscribing endpoints. You can use Lambda, SQS, SMS, etc. as a subscriber. This time I will send a short message using SMS.
Since there were multiple callers such as the application side and other AWS services, I decided to write it in Lambda. It's basically like kicking Lambda and sending an SES or SMS.
SES You can send to multiple addresses by passing the destination email address in an array. Basically, when using SES, you can send and receive only the addresses registered and permitted by SES in advance. But then you have to register and authorize SES every time you have more addresses, so contact support and have them move out of the sandbox environment. Then you don't have to register as an email recipient. (Note that the sender's address must be registered)
import boto3
def lambda_handler(event, context):
try:
ses = boto3.client("ses", region_name = "us-west-2")
ses.send_email(
Source = "from_mailaddress",
Destination = {
"ToAddresses": "to_mailaddress"
},
Message = {
"Subject": {
"Data": "subject_title",
"Charset": "UTF-8"
},
"Body": {
"Text": {
"Data": "body_message",
"Charset": "UTF-8"
}
}
}
)
except Exception as e:
print e
SNS(SMS) It seems that the destination phone number can only be sent to an international phone number (sometimes with +81). It's also a short message, so if you don't make the content of the message moderate, you will get an error. Also, the default usage charge limit is 1 $ / month, and you can only send dozens of messages as it is. Moreover, you cannot raise the limit (you will get an error message saying that you are not allowed to raise it), so please contact support as well as SES so that you can raise the limit.
import boto3
def lambda_handler(event, context):
try:
sns = boto3.client("sns")
phoneNumber = "+818012345678"
message = "send message"
sns.publish(
PhoneNumber = phoneNumber
Message = message
)
except Exception as e:
print e
I was a little stuck with the restrictions of SES and SNS (SMS), so I wrote this article this time. If you use AWS-SDK etc., you can easily send a message from the application side. It's convenient. If you give the Lambda event the destination address, phone number, message content, etc. and refer to it, you can reuse it with one Lambda. It's convenient (second time).
see you!
Recommended Posts