11
9

More than 3 years have passed since last update.

【Windows/PHP】XAMPPのsendmailでローカル環境からメールを送信する(mb_send_mail())

Last updated at Posted at 2020-08-17

概要

Windowsのローカル環境からメールを送るための設定メモです。
XAMPPを利用しており、その中にあるsendmailを使います。

今回はPHPのmb_send_mail()でメールを送るために設定します。

メールサーバーの準備

メールサーバーを持ち合わせていないので、mailtrapというサービスを利用します。
無料会員登録で簡単にメールの送受信テストが行えるサービスです。
(メールサーバーを持っている場合は、そちらの情報を使えばOKです:ok_woman:

mailtrapについては以下も参考にして下さい。

 
まず、mailtrapに登録するとSMTPサーバーの情報が与えられます。
今回はこちらを使って、ローカルからメールを送信できるようにします。
image02.png

sendmailの設定

XAMPPの中にsendmailのディレクトリがあります。
C:/xampp/sendmail

設定ファイルsendmail.iniがあるので、コピーしてオリジナルを残しておきましょう。
設定ファイルを開いて、SMTPサーバーの情報を記述します。

mailtrapの場合はSMTPサーバーなので、下記の設定で利用が可能です。

sendmail.ini
; configuration for fake sendmail

; if this file doesn't exist, sendmail.exe will look for the settings in
; the registry, under HKLM\Software\Sendmail

[sendmail]

; you must change mail.mydomain.com to your smtp server,
; or to IIS's "pickup" directory.  (generally C:\Inetpub\mailroot\Pickup)
; emails delivered via IIS's pickup directory cause sendmail to
; run quicker, but you won't get error messages back to the calling
; application.

smtp_server=smtp.mailtrap.io

; smtp port (normally 25)

smtp_port=465

; SMTPS (SSL) support
;   auto = use SSL for port 465, otherwise try to use TLS
;   ssl  = alway use SSL
;   tls  = always use TLS
;   none = never try to use SSL

smtp_ssl=TLS

; if your smtp server requires authentication, modify the following two lines

auth_username=メールサーバーのユーザー名(mailtrapで付与されたもの)
auth_password=メールサーバーのパスワード(mailtrapで付与されたもの)
:
:

 
設定ファイルに記述するのは下記の5か所です。
記述したらsendmail.iniを上書き保存します。

smtp_server=メールサーバーのホスト名(例:smtp.mailtrap.io)
smtp_port=使用するポート番号(例:465)
smtp_ssl=使用する暗号化プロトコル(例:TLS)
auth_username=メールサーバーのユーザー名
auth_password=メールサーバーのパスワード

PHPの設定

次に、PHPにsendmailを指定する設定をします。
設定ファイルC:/xampp/php/php.iniを編集します。
(パスは環境によって異なります。コピーしてオリジナルを保存しておきましょう。)

php.iniのsendmail_pathにsendmailの実行ファイルを指定します。
上書き保存して、Webサーバーを再起動させます。

php.ini
; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
;sendmail_path =
sendmail_path =""C:\xampp\sendmail\sendmail.exe" -t"

 
これで設定は完了です。

参考

まとめ

以上の設定で、PHPでmb_send_mail()を使ってメールを送ることができます。
mailtrapは送信したメールが全てmailtrapの受信箱に入るので、指定の送信先では受信されません。

テストメールが自分の受信箱に溜まることがないのでとても便利です。
(送信先をミスっても相手には届かないので、開発中は安心)

これで心置きなくメールテストを行いたいと思います。以上!

11
9
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
11
9