LoginSignup
8
15

More than 3 years have passed since last update.

laravel+mailhogでメールの仮想環境を作る

Last updated at Posted at 2018-03-14

2019年10月3日追記
HomeSteadもこの記事を書いてからずいぶんバージョンアップしています。
Laravel5.8 + Homestead v9 以降(もっと前から対応していると思いますが)ならば
mailhogはセットアップ済みで.envで設定するだけで使えます。

以下の物を使用
homestead 6.5
PHP7
Nginx
Vagrant

mailhogがhomesteadに含まれているので
Laravelの仮想環境をhomesteadで作成した人は簡単にメール送信のテストができるようになります。

mailhogが動いているか確認

sudo lsofc -i:1025

結果が

COMMAND  PID    USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
mailhog 1195 vagrant    3u  IPv6  17743      0t0  TCP *:1025 (LISTEN)

となっていればmailogが動いています。

Vagrantfileに追記

config.vm.network :forwarded_port, host: 8025, guest: 8025

ここでVMが起動済みならば再起動

iptablesの追加

sudo iptables -A INPUT -p tcp -m tcp --dport 8025 -j ACCEPT
sudo /etc/init.d/iptables-persistent save

iptables-persistentがなかったら

sudo apt-get install iptables-persistent

php.iniを修正

/etc/php/7.1/fpm/php.ini
/etc/php/7.1/cli/php.ini
sendmail_path = "/usr/local/bin/mailhog sendmail test@example.org"

サービスの再起動

sudo /etc/init.d/php7.1-fpm restart
sudo /etc/init.d/nginx restart

.envの修正

MAIL_DRIVER=smtp
MAIL_HOST=localhost
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

ブラウザからアクセス

これで設定完了です。
Laravelで作成中のプログラムからmailを飛ばしてみて、
mailhog側で見れればOKです。

8
15
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
8
15