[Amazon SES] SES ๋ฉ”์ผ ๋ฐœ์†ก ์‹œ ํ•œ๊ธ€ ๋ฐœ์‹ ์ž ์ด๋ฆ„ ๊นจ์ง ํ˜„์ƒ

2023. 8. 20. 20:49ใ†AWS/troubleshooting

โ˜„๏ธ ๋ฌธ์ œ

AWS SES API SendEmail๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฉ”์ผ์„ ๋ณด๋‚ด๋Š”๋ฐ ์•„๋ž˜์™€ ๊ฐ™์ด ํ•œ๊ธ€๋กœ ์ž…๋ ฅํ•œ ๋ฐœ์‹ ์ž ์ด๋ฆ„์ด ์ •์ƒ์ ์œผ๋กœ ์ถœ๋ ฅ๋˜์ง€ ์•Š์•˜๋‹ค.

 

๐ŸŒŽ ํ™˜๊ฒฝ

  • ์ฝ”๋“œ ์‹คํ–‰ ํ™˜๊ฒฝ
    • AWS Systems Automation Document
      • Action : aws:executeScript
    • Runtime : python3.7
    • library : boto3
def sendEmail(sender_email, sender_name, user_email, user_name):
  SENDER = f"{sender_name} <{sender_email}>"
  RECIPIENT = user_email
  AWS_REGION = "ap-northeast-2"
  SUBJECT = "Amazon SES Test (SDK for Python)"
  BODY_TEXT = ("Amazon SES Test (Python)\r\n"
             "This email was sent with Amazon SES using the "
             "AWS SDK for Python (Boto).")
  BODY_HTML = """<html>
		<head></head>
		<body>
		  <h1>Amazon SES Test (SDK for Python)</h1>
		  <p>This email was sent with
		    <a href='https://aws.amazon.com/ses/'>Amazon SES</a> using the
		    <a href='https://aws.amazon.com/sdk-for-python/'>
		      AWS SDK for Python (Boto)</a>.</p>
		</body>
		</html>"""
  CHARSET = "UTF-8"

  client = boto3.client('ses',region_name=AWS_REGION)
  
  try:
    response = client.send_email(
      Destination={
        'ToAddresses': [
          RECIPIENT,
        ],
      },
      Message={
        'Body': {
          'Html': {
            'Charset': CHARSET,
            'Data': BODY_HTML,
          },
          'Text': {
            'Charset': CHARSET,
            'Data': BODY_TEXT,
          },
        },
        'Subject': {
          'Charset': CHARSET,
          'Data': SUBJECT,
        },
      },
      Source=SENDER
    )
  except ClientError as e:
    print(e.response['Error']['Message'])
    raise e
  else:
    print("Email sent! Message ID:"),
    print(json.dumps(response, indent=2))

 

 

โœ๏ธ ์›์ธ

AWS SES๋Š” SMTPUTF8 ํ™•์žฅ์„ ์ง€์›ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ „์ž ๋ฉ”์ผ ์ฃผ์†Œ ๋ฌธ์ž์—ด์€ 7๋น„ํŠธ ASCII์—ฌ์•ผ๋งŒ ํ•œ๋‹ค. ๋ฐœ์‹ ์ž ์ด๋ฆ„(frindely from ๋ถ€๋ถ„)์€ MIME(Multipurpose Internet Mail Extensions) ์ธ์ฝ”๋”ฉ๋œ ๋‹จ์–ด ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

⇒ Ref. Amazon SES API Docs: SendEmail

⇒ Ref. Amazon SES ๊ฐœ๋ฐœ์ž ์•ˆ๋‚ด์„œ > ์ด๋ฉ”์ผ ์ „์†ก ์„ค์ • > MIME ์‚ฌ์šฉ

 

 


 

๐Ÿ”ซ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

import base64

sendername_bytes = sender_name.encode(CHARSET)
sendername_base64 = base64.b64encode(sendername_bytes)
SENDER_NAME = f"=?{CHARSET}?B?{sendername_base64}?="
SENDER = f"{SENDER_NAME} <{sender_email}>"

Base64 ์ธ์ฝ”๋”ฉ์„ ์ด์šฉํ•˜์—ฌ ์œ„ ๋ถ€๋ถ„์„ ์ถ”๊ฐ€ํ•ด๋„ ๊ณ„์† ํ•œ๊ธ€ ๊นจ์ง ํ˜„์ƒ์ด ๋ฐœ์ƒํ•œ๋‹ค.

์ด์œ ๋Š” sender_name์ด “์•ˆ๋ƒ์„ธ์šค”์ธ ๊ฒฝ์šฐ, sendername_base64๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด b'7JWI64OQ7IS47Jqk’์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. sendername_base64์—์„œ b’ ‘๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ์ธ์ฝ”๋”ฉ๋œ ๊ตฌ๋ฌธ๋งŒ ๋‚จ๊ธฐ๋Š” ์ž‘์—…์ด ๋” ํ•„์š”ํ•˜๋‹ค.

SENDER_NAME = f"=?{CHARSET}?B?{sendername_b64_str[2:-1]}?="

SENDER_NAME์„ ์œ„์™€ ๊ฐ™์ด ๋ณ€๊ฒฝํ•˜์—ฌ sendername_base64์˜ ํƒ€์ž…์„ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พธ๊ณ  b’์˜ ๋’ค๋ถ€ํ„ฐ ‘ ์ „๊นŒ์ง€์˜ ๋ฌธ์ž๋งŒ ๋‚จ๊ธฐ๋„๋ก ํ•œ๋‹ค.

 


ํ•ด๊ฒฐ ์„ฑ๊ณต~! ๐Ÿ˜‡