Send email with SES in Python and short message with SMS on SNS

Introduction

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 ...

What is SES?

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 ...

What is SNS?

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.

Implementation method

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

at the end

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

Send email with SES in Python and short message with SMS on SNS
Send an email with Amazon SES + Python
Send email with Python
Send an email with Excel attached in Python
Email hipchat with postfix, fluentd and python on Azure
Send Japanese email with Python3
Send email in Python (Outlook)
[Automation] Send Outlook email with Python
[Python] Send an email with outlook
Send email via gmail with Python 3.4.3.
HTML email with image to send with python
Send email to multiple recipients in Python (Python 3)
A memo with Python2.7 and Python3 on CentOS
Dealing with "years and months" in Python
Create and edit spreadsheets in any folder on Google Drive with python
RabbitMQ message notification app in Python with Growl ~ with Raspberry Pi and Julius ~
Send and receive binary data via serial communication with python3 (on mac)
Send a message to LINE with Python (LINE Notify)
Send an email to Spushi's address with python
Install OpenCV 4.0 and Python 3.7 on Windows 10 with Anaconda
Send messages to Skype and Chatwork in Python
[Python] Send email
Try working with Mongo in Python on Mac
Extract email attachments received in Thunderbird with Python
Send HTTP with Basic authentication header in Python
To automatically send an email with an attachment using the Gmail API in Python
If you can't send an email with python smtplib and have trouble, command line
Install selenium on Mac and try it with python
Automatic follow on Twitter with python and selenium! (RPA)
Get comments on youtube Live with [python] and [pytchat]!
Calculate Pose and Transform differences in Python with ROS
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Start numerical calculation in Python (with Homebrew and pip)
Automate Chrome with Python and Selenium on your Chromebook
Exception message in Python
Send email with Django
Validate E-Mail with Python
Send Gmail in Python
Put Ubuntu in Raspi, put Docker on it, and control GPIO with python from the container
Design and test Verilog in Python only with Veriloggen and cocotb.
How to install NPI + send a message to line with python
[Python] Send an email from gmail with two-step verification set
I tried sending an email from Amazon SES with Python
Create a list in Python with all followers on twitter
IP spoof using tor on macOS and check with python
Test Python with Miniconda on OS X and Linux with travis-ci
Send experiment results (text and images) to slack with Python
Tips for coding short and easy to read in Python
Select file in dialog with python → Display file name in message box
Play with Mastodon's archive in Python 2 Count replies and favourites
Notes on deploying pyenv with Homebrew and managing Python versions
Install CaboCha in Ubuntu environment and call it with Python.
Get message from first offset with kafka consumer in python
How to log in to AtCoder with Python and submit automatically
Notes on reading and writing float32 TIFF images in python
Programming with Python and Tkinter
Encryption and decryption with Python
Scraping with selenium in Python
Python and hardware-Using RS232C with Python-
Python on Ruby and angry Ruby on Python
Working with LibreOffice in Python