boto3のインストール
pip install boto3
メールの送信
import boto3
def send_email(source, to, subject, body):
client = boto3.client('ses')
response = client.send_email(
Destination={
'ToAddresses': [
to
],
},
Message={
'Body': {
'Text': {
'Charset': 'UTF-8',
'Data': body,
},
},
'Subject': {
'Charset': 'UTF-8',
'Data': subject,
},
},
Source=source
)
return response
if __name__ == '__main__':
source = '[送信元]'
to = '[送信先]'
subject = '[タイトル]'
body = '[本文]'
r = send_email(source, to, subject, body)
print(r)
以下のようなエラーが出力される場合には、「Simple Email Service」->「Email Addresses」でメールアドレスを承認しておく。
Traceback (most recent call last): File "test.py", line 32, inr = send_email(source, to, subject, body) File "[ファイル].py", line 23, in send_email Source=source File "/usr/local/lib/python3.7/site-packages/botocore/client.py", line 357, in _api_call return self._make_api_call(operation_name, kwargs) File "/usr/local/lib/python3.7/site-packages/botocore/client.py", line 661, in _make_api_call raise error_class(parsed_response, operation_name) botocore.errorfactory.MessageRejected: An error occurred (MessageRejected) when calling the SendEmail operation: Email address is not verified. The following identities failed the check in region US-EAST-1: [メールアドレス]