🤖
[python] motoでsesのテスト実行時、Email address not verified XXXのエラーを解決
事象
motoでsesのテストを実行時、以下のエラーが発生。
Exception: SES send mail error: An error occurred (MessageRejected) when calling the SendEmail operation: Email address not verified source@example.com
解決方法
verify_email_identity
メソッドで送信元メールアドレスを検証する。
def send_email(source: str, destination: str, subject: str, body: str) -> Any:
"""テスト対象関数"""
ses = boto3.client(
"ses",
region_name="us-east-1",
)
ses.verify_email_identity(EmailAddress=source) # 送信元メールアドレスを検証する
try:
response = ses.send_email(
Source=source,
Destination={"ToAddresses": [destination]},
Message={
"Subject": {"Data": subject, "Charset": "UTF-8"},
"Body": {"Text": {"Data": body, "Charset": "UTF-8"}},
},
)
except Exception as e:
raise Exception(f"SES send mail error: {str(e)}")
return response
以下はテストコード
import os
import pytest
from moto import mock_aws
@pytest.fixture
def setup():
# 環境変数のセットアップ
os.environ.pop("AWS_PROFILE", None)
@mock_aws
def test_send_email_success(setup):
# 準備
source = "source@example.com"
destination = "destination@example.com"
subject = "test title"
body = "test body"
# 実行
response = send_email(source, destination, subject, body)
# 検証
assert response["ResponseMetadata"]["HTTPStatusCode"] == 200
Discussion