Windows + XAMPP + PHP でメールを送る環境を作る(2010/04/01追記、修正)
XAMPPでメール送信テストをするためのメモ。
試したのはWindows版のXAMPP Lite Version 1.7.1です。*1
まずはphp.iniから。
バージョンによって場所が違うかもしれないけど。
「XAMPPインストールディレクトリ」の下の「php」というディレクトリにあります。
それをテキストエディタで開いて、[mail function]という項目を編集する。
多分、以下が編集前の状態だと思います。
[mail function] ; For Win32 only. SMTP = localhost smtp_port = 25 ; For Win32 only. ;sendmail_from = me@example.com ; For Unix only. You may supply arguments as well (default: "sendmail -t -i"). ;sendmail_path = "XAMPPインストールディレクトリ\sendmail\sendmail.exe -t"
SMTPとsmtp_portには、使用したいメールサーバの設定を記述する。
sendmail_fromはそのままにしておきました。どーせphpで送信する場合にはfromを設定するから。
でもって、sendmail_pathに正しくパスが設定されているか確認して、「;」を外し、有効にする。
コメントには「Unix only」とか書いてるけど、気にしないっつーか、指定しないでどうやって動かすんだ?
とは書いたものの、PHPのサイトを確認すると、Windows環境でsendmail.exeを使わないならsendmail_pathの設定は不要なようだね。*2
SMTP string
Windows 環境のみで使用されます: mail() 関数でメールを送信する際に使用する SMTP サーバのホスト名または IP アドレス。smtp_port int
http://jp.php.net/manual/ja/mail.configuration.php#ini.smtp
Windows 環境のみで使用されます: SMTP 設定で指定したサーバに接続するポート番号で、 デフォルトは 25 です。PHP 4.3.0 以降でのみ利用可能です。
なので、sendmail.exeを使わないパターンはこうなる。
[mail function] ; For Win32 only. SMTP = smtp.xxxxx.co.jp smtp_port = 25 ; For Win32 only. ;sendmail_from = me@example.com ; For Unix only. You may supply arguments as well (default: "sendmail -t -i"). ;sendmail_path = "インストールディレクトリ\sendmail\sendmail.exe -t"
sendmail.exeを使わないのなら、これで送信ができる。
PHPでメールを送信するサンプルを作って、実行してみればいい。
以降はsendmailを使う場合。
[mail function] ; For Win32 only. ;SMTP = smtp.xxxxx.co.jp ;smtp_port = 25 ; For Win32 only. ;sendmail_from = me@example.com ; For Unix only. You may supply arguments as well (default: "sendmail -t -i"). sendmail_path = "インストールディレクトリ\sendmail\sendmail.exe -t"
続いてはsendmailの設定をしたいので、xamppをインストールしたディレクトリ直下にあるsendmailディレクトリのsendmail.iniを開く。
コメントアウトしてある説明文は省きました。
[sendmail] smtp_server=localhost smtp_port=25 ;default_domain=local ;error_logfile=error.log ;debug_logfile=debug.log ;auth_username= ;auth_password= ;pop3_server= ;pop3_username= ;pop3_password= ;force_sender=me@localhost ;hostname=localhost
ここでも同じようにsmtp_serverとsmtp_portの設定を行う。php.iniの記述と合わせるのは当然のこと。php.iniの設定はあくまでもPHPで送る場合のハナシだという理解であれば、ここは合わせる必要はないワケだ。
hostnameとforce_senderを変更してみた。意味があるかどうかは判らないけど。
[sendmail] smtp_server=smtp.xxxxx.co.jp ; smtp port (normally 25) smtp_port=25 ;default_domain=local ;error_logfile=error.log ;debug_logfile=debug.log ;auth_username= ;auth_password= ;pop3_server= ;pop3_username= ;pop3_password= force_sender=XXXX@xxxxx.co.jp hostname=sendmail_test
デバッグログやエラーログが取りたい場合は、debug_logfile、error_logfileの項目のコメントを外して、有効にする。ファイル名だけだから、sendmailディレクトリにファイルが出来る。
パスを変更しての実験はしてないので、どうなるか判らない(ドキュメントを読めって?)。
XAMPP Liteの1.7.3のsendmail.iniは書式が変わっていた。
# Example for a user configuration file # Set default values for all following accounts. defaults logfile "C:\xampplite\sendmail\sendmail.log" # Mercury account Mercury host localhost from postmaster@localhost auth off # A freemail service example account Hotmail tls on tls_certcheck off host smtp.live.com from [exampleuser]@hotmail.com auth on user [exampleuser]@hotmail.com password [examplepassword] # Set a default account account default : Mercury
account defaultで使用するアカウントを指定するんだな。
以下のように追加、変更しました。*3
# 追加分 account NEW tls off tls_certcheck off host smtp.xxxx.co.jp from xxxx@xxxx.co.jp auth off # Set a default account account default : NEW
ログのとり方については、詳細不明…ってカンジだ。
とりあえずエラーの場合でも上のログファイルに書かれてるようなので、あまり気にするほどのことはないかもしれない。
念の為、xamppでapacheの再起動(停止→起動)しておく。
これでOK。
あとはPHPでメール送信プログラムを書いて、送信すればいい。
少なくとも僕の環境では正常に送信できた。
ちなみに、送信されたメールのヘッダを確認すると、「Received:」がそれぞれ異なっていた。
XAMPP Lite 1.7.1のsendmail.exeはsendmail.iniでhostnameが定義されているので、その値sendmail_testが適用されていた。
XAMPP Lite 1.7.3はsendmail.iniで特に定義していなかったので、localhostになっていた。
sendmail.exe --helpの内容やXAMPP Lite 1.7.1のsendmail.iniのコメントから色々ためしたら、domainで指定できるらしいと判った。
domain TEST
とやったらちゃんと反映されてました。
XAMPP Lite 1.7.3でsendmail.exeを使わないパターンだとPC名で送信されてきた。一応これがhostname扱いってことなのかな?
元記事では、その後テスト送信でエラーが発生したが、あまり意味はないので、割愛した。
しいて言えば、ヘッダはきちんと書きましょうということ。