LoginSignup
0
0

AWS Cloud9環境で mailpit を使う方法(Laravel10+SES+mailpit)

Posted at

前提

  • PHPフレームワークはLaravel10を使用する
  • PHPバージョンは8.2である
  • メールサーバは AWS SESを使用する
    • SESのセットアップは事前に完了しているものとする
  • メール送信ライブラリは mailpit を使用する

AWS SDKをインストール(SES Clientを使用するため)

cd /path/to/project/
composer require aws/aws-sdk-php:~3.0

.env の設定

.env
MAIL_MAILER=smtp
#MAIL_HOST=mailpit
# MAIL_HOST を localhost にすることを忘れないように
MAIL_HOST=localhost
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="noreply@example.com"
MAIL_FROM_NAME="${APP_NAME}"

# SESのアクセスキーを入力
AWS_ACCESS_KEY_ID=AK...6M
# SESのシークレットキーを入力
AWS_SECRET_ACCESS_KEY=PS...Qm
# 必要であればリージョンを適切なものに変更すること
AWS_DEFAULT_REGION=ap-northeast-1
#AWS_BUCKET=
#AWS_USE_PATH_STYLE_ENDPOINT=false

mailpitをインストール

cd ~/environment/
# ライブラリをダウンロード
wget https://github.com/axllent/mailpit/releases/download/v1.18.4/mailpit-linux-amd64.tar.gz
# 解凍する
tar xzf mailpit-linux-amd64.tar.gz 
# 実行権限を付与
chmod +x mailpit
# PATHを通す
sudo mv mailpit /usr/local/bin/

# メールサーバのプロトコルをEC2のセキュリティグループに追加する
MY_INSTANCE_ID=$(curl http://169.254.169.254/latest/meta-data/instance-id) # Get the ID of the instance for the environment, and store it temporarily.
MY_SECURITY_GROUP_ID=$(aws ec2 describe-instances --instance-id $MY_INSTANCE_ID --query 'Reservations[].Instances[0].SecurityGroups[0].GroupId' --output text) # Get the ID of the security group associated with the instance, and store it temporarily.
# ポート 8025 HTTP(Mailpitの管理画面インターフェース)
aws ec2 authorize-security-group-ingress --group-id $MY_SECURITY_GROUP_ID --protocol tcp --port 8025 --cidr 0.0.0.0/0
# ポート 1025 SMTP(MailpitのSMTPサーバー)
aws ec2 authorize-security-group-ingress --group-id $MY_SECURITY_GROUP_ID --protocol tcp --port 1025 --cidr 0.0.0.0/0

メールクライアントを起動する

# メールサーバを起動する(バックグラウンド実行するために & を付ける)
# Mailpitの管理画面は以下のURLからアクセスできる
# http://localhost:8025/ 
mailpit &

# メールサーバを終了したい場合、fg でフォアグラウンドにした後、Ctrl+Cでプロセス終了できる
#fg
0
0
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
0
0