2
1

More than 3 years have passed since last update.

Lambda (Python) でメールを送信してみる

Posted at

■ はじめに

963f9ca9395c096c234f891ad8877b28.png

Lambda からメールを送信したかったので、やってみました。

ほぼほぼ、こちらの記事の通りにやっただけですか…

ありがとうございました!
感謝 ♪♪♪

🙇‍♂️🙇‍♂️🙇‍♂️

■ コード

コードは、以下。

import os
import json
import smtplib
from email import message

smtp_host = '[メールホスト]'
smtp_port = [ポート番号]

def lambda_handler(event, context):
    body = json.dumps('Hello from Lambda!')
    try:
        print('event :', event)
        #print(context.__dict__)
        smtp_account_id = os.environ.get('SmtpAccountID', '')
        print('SmtpAccountID :', smtp_account_id)
        if len(smtp_account_id) == 0:
            raise Exception('The content is incorrect. - SmtpAccountID is None.')
        smtp_account_pass = os.environ.get('SmtpAccountPass', '')
        print('SmtpAccountPass :', '****')
        if len(smtp_account_pass) == 0:
            raise Exception('The content is incorrect. - SmtpAccountPass is None.')
        from_mail = event.get('FromMail', '')
        #print('FromMail : ', from_mail)
        if type(from_mail) is not str:
            raise Exception('The content is incorrect. - FromMail is not str.')
        if len(from_mail) == 0:
            raise Exception('The content is incorrect. - FromMail is None.')
        to_mail = event.get('ToMail', '')
        #print('ToMail :', to_mail)
        if type(to_mail) is not str:
            raise Exception('The content is incorrect. - ToMail is not str.')
        if len(to_mail) == 0:
            raise Exception('The content is incorrect. - ToMail is None.')
        subject = event.get('Subject', '')
        #print('Subject :', subject)
        if type(subject) is not str:
            raise Exception('The content is incorrect. - Subject is not str.')
        if len(subject) == 0:
            raise Exception('The content is incorrect. - Subject is None.')
        letter_body = event.get('LetterBody', '')
        #print('LetterBody :', letter_body)
        if type(letter_body) is not str:
            raise Exception('The content is incorrect. - LetterBody is not str.')
        if len(letter_body) == 0:
            raise Exception('The content is incorrect. - LetterBody is None.')
        msg = message.EmailMessage()
        msg.set_content(letter_body);
        msg['Subject'] = subject
        msg['From'] = from_mail
        msg['To'] = to_mail
        print(msg)
        server = smtplib.SMTP(smtp_host, smtp_port, timeout=10)
        server.ehlo()
        server.starttls()
        server.ehlo()
        server.login(smtp_account_id, smtp_account_pass)
        server.send_message(msg)
        server.quit()
    except Exception as e:
        print(e)
        return {
            'statusCode': 400,
            'error': {
                'code': 400,
                'message': 'Bad Request.'
            }
        }
    else:
        return {
            'statusCode': 200,
            'body': body
        }

■ Lambda への引数

{
  "FromMail": "[送信元メールアドレス]",
  "ToMail": "[送信先メールアドレス]",
  "Subject": "[メールタイトル]",
  "LetterBody": "[メール本文]"
}

■ Lambda に設定する環境変数

  • SmtpAccountID : [SMTPアカウントID]
  • SmtpAccountPass : [SMTPアカウントパス]

■ ハマりポイント

  • 特にありませんでした。

  • とりあえず、何とか設定できて良かったです ♪♪♪

😆😆😆

■ まとめ

参考になれば ♪♪♪

👋👋👋

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1