Scotch BoxでPHPメール送信(Postfix)


Scotch Box(https://box.scotch.io/)において、Postfixを用いてPHPからメール送信する方法について記します。
注意: PHPでPostfixを利用する場合、Scotch Boxにデフォルトで入っているPHPモジュール(mailcatcher)を取り除く必要があります。詳細は下記を参照ください。

環境

  • 仮想環境:vagrant(VirtualBox)
  • Box:Scotch Box(UbuntuのLAMP環境のBox)

手順

目次

  • Postfixインストール
  • Postfix設定
  • Postfix自動起動
  • PHPモジュール削除(mailcatcher)
  • Apache再起動

Postfixインストール

vagrant@scotchbox:~$ sudo apt-get install -y postfix
vagrant@scotchbox:~$ sudo apt-get install -y mailutils

Postfix設定

vagrant@scotchbox:~$ sudo cp /usr/lib/postfix/main.cf /etc/postfix/main.cf
vagrant@scotchbox:~$ sudo vi /etc/postfix/main.cf
# 59行目:コメント解除
mail_owner = postfix
# 76行目:コメント解除しホスト名指定
myhostname = mail.server.world
# 83行目:コメント解除しドメイン名指定
mydomain = server.world
# 104行目:コメント解除
myorigin = $mydomain
# 118行目:コメント解除
inet_interfaces = all
# 166行目:コメント解除
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
# 209行目:コメント解除
local_recipient_maps = unix:passwd.byname $alias_maps
# 268行目:自ネットワーク追記
mynetworks = 127.0.0.0/8, 10.0.0.0/24
# 388行目:コメント解除
alias_maps = hash:/etc/aliases
# 399行目:コメント解除
alias_database = hash:/etc/aliases
# 421行目:コメント解除 ( Maildir形式へ移行 )
home_mailbox = Maildir/
# 557行目:コメントにしてその下に追記
#smtpd_banner = $myhostname ESMTP $mail_name (@@DISTRO@@)
smtpd_banner = $myhostname ESMTP
# 631行目:追記
sendmail_path = /usr/sbin/postfix
# 636行目:追記
newaliases_path = /usr/bin/newaliases
# 641行目:追記
mailq_path = /usr/bin/mailq
# 647行目:追記
setgid_group = postdrop
# 651行目:コメント化
#html_directory =
# 655行目:コメント化
#manpage_directory =
# 660行目:コメント化
#sample_directory =
# 664行目:コメント化
#readme_directory =
# 最終行へ追記:送受信メールサイズを10Mに制限
message_size_limit = 10485760
# メールボックスサイズを1Gに制限
mailbox_size_limit = 1073741824
# 以下SMTP-Auth用
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
smtpd_sasl_local_domain = $myhostname
smtpd_recipient_restrictions = permit_mynetworks,permit_auth_destination,permit_sasl_authenticated,reject
vagrant@scotchbox:~$ sudo newaliases
vagrant@scotchbox:~$ sudo service postfix restart

Postfix自動起動

vagrant@scotchbox:~$ sudo sysv-rc-conf postfix on
vagrant@scotchbox:~$ sudo sysv-rc-conf mailutils on

PHPモジュール削除(mailcatcher)

Sctoch Boxには、mailcatcherというダミーのsmtpが付いています。このmailcatcherのモジュールを削除します。

vagrant@scotchbox:~$ sudo rm /etc/php5/apache2/conf.d/20-mailcatcher.ini
vagrant@scotchbox:~$ sudo rm /etc/php5/mods-available/mailcatcher.ini

Apache再起動

vagrant@scotchbox:/tmp$ sudo service apache2 restart

余談

PHPモジュール(mailcatcher)を削除しないと、下記のエラーが出ます。

vagrant@scotchbox:/tmp$ cat ./mailtest.php
<?php
$to='[email protected]';
$subject='TEST MAIL';
$body='THIS IS TEST';
echo mail($to,$subject,$body);
?>

vagrant@scotchbox:/tmp$ php -f ./mailtest.php
/home/vagrant/.rbenv/versions/2.2.2/lib/ruby/2.2.0/net/smtp.rb:541:in `initialize': Connection refused - connect(2) for "127.0.0.1" port 1025 (Errno::ECONNREFUSED)
    from /home/vagrant/.rbenv/versions/2.2.2/lib/ruby/2.2.0/net/smtp.rb:541:in `open'
    from /home/vagrant/.rbenv/versions/2.2.2/lib/ruby/2.2.0/net/smtp.rb:541:in `tcp_socket'
    from /home/vagrant/.rbenv/versions/2.2.2/lib/ruby/2.2.0/net/smtp.rb:551:in `block in do_start'
    from /home/vagrant/.rbenv/versions/2.2.2/lib/ruby/2.2.0/timeout.rb:89:in `block in timeout'
    from /home/vagrant/.rbenv/versions/2.2.2/lib/ruby/2.2.0/timeout.rb:99:in `call'
    from /home/vagrant/.rbenv/versions/2.2.2/lib/ruby/2.2.0/timeout.rb:99:in `timeout'
    from /home/vagrant/.rbenv/versions/2.2.2/lib/ruby/2.2.0/net/smtp.rb:550:in `do_start'
    from /home/vagrant/.rbenv/versions/2.2.2/lib/ruby/2.2.0/net/smtp.rb:520:in `start'
    from /home/vagrant/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/mail-2.6.3/lib/mail/network/delivery_methods/smtp.rb:112:in `deliver!'
    from /home/vagrant/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/mail-2.6.3/lib/mail/message.rb:2141:in `do_delivery'
    from /home/vagrant/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/mail-2.6.3/lib/mail/message.rb:238:in `deliver'
    from /home/vagrant/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/mailcatcher-0.6.1/bin/catchmail:71:in `<top (required)>'
    from /home/vagrant/.rbenv/versions/2.2.2/bin/catchmail:23:in `load'
    from /home/vagrant/.rbenv/versions/2.2.2/bin/catchmail:23:in `<main>'

参考資料

http://d.hatena.ne.jp/psquare/20140108/p1
http://blog.shimabox.net/2015/01/30/mailcatcher_not_work/
https://github.com/sj26/mailcatcher/issues/103#issuecomment-22605099
http://www.server-world.info/query?os=Ubuntu_14.04&p=mail