Skip to content

Instantly share code, notes, and snippets.

@shiro01
Last active November 30, 2018 07:12
Show Gist options
  • Save shiro01/cbd784046f47386981d1afac29c4a2dc to your computer and use it in GitHub Desktop.
Save shiro01/cbd784046f47386981d1afac29c4a2dc to your computer and use it in GitHub Desktop.
AWS SESを使用したメール送信テスト用コード。
# https://docs.aws.amazon.com/ja_jp/ses/latest/DeveloperGuide/send-using-sdk-python.html
# https://dev.classmethod.jp/cloud/aws/lambda-to-ses/
# ロールアタッチポリシー:AmazonSESFullAccess
import boto3
from botocore.exceptions import ClientError
import json
SRC_MAIL = "[email protected]"
DST_MAIL = "[email protected]"
REGION = "us-east-1"
def lambda_handler(event, context):
email_subject = "test mail send"
message = json.dumps(event, indent = 4)
email_send_result = send_email(SRC_MAIL, DST_MAIL, email_subject, message)
return email_send_result
def send_email(source, to, subject, body):
client = boto3.client('ses', region_name=REGION)
try:
response = client.send_email(
Source=source,
Destination={
'ToAddresses': [
to,
]
},
Message={
'Subject': {
'Data': subject,
},
'Body': {
'Text': {
'Data': body,
},
}
}
)
# Display an error if something goes wrong.
except ClientError as e:
print(e.response['Error']['Message'])
else:
print("Email sent! Message ID:"),
print(response['ResponseMetadata']['RequestId'])
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment