XAMPP 是一套整合 Apache、PHP、MySQL、Mercury Mail Transport System、FileZilla FTP Server、Tomcat…等的服務應用系統,讓您可以專心在系統程式開發上,若在開發的應用系統中有需要發信時而不想自行管理 Mail Server 時,可以利用內附的 sendmail 結合 gmail 來達成。
主要修改的地方有二個設定:
第一個設定檔:修改 \xampp\php\php.ini
[mail function] ; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury ; SMTP = localhost ; smtp_port = 25; For Win32 only. ; http://php.net/sendmail-from ;sendmail_from = postmaster@localhost; XAMPP IMPORTANT NOTE (1): If XAMPP is installed in a base directory with spaces (e.g. c:\program files\xampp) fakemail and mailtodisk do not work correctly. ; XAMPP IMPORTANT NOTE (2): In this case please copy the sendmail or mailtodisk folder in your root folder (e.g. C:\sendmail) and use this for sendmail_path. ; XAMPP: Comment out this if you want to work with fakemail for forwarding to your mailbox (sendmail.exe in the sendmail folder) ; XAMPP: Comment out this if you want to work with mailToDisk, It writes all mails in the \xampp\mailoutput folder ; Force the addition of the specified parameters to be passed as extra parameters ; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename ; Log all mail() calls including the full path of the script, line #, to address and headers |
第二個設定檔:修改 \xampp\sendmail
; configuration for fake sendmail
; if this file doesn’t exist, sendmail.exe will look for the settings in [sendmail] ; you must change mail.mydomain.com to your smtp server, smtp_server=smtp.gmail.com ; smtp port (normally 25) smtp_port=465 ; SMTPS (SSL) support smtp_ssl=auto ; the default domain for this server will be read from the registry default_domain=gmail.com ; log smtp errors to error.log (defaults to same directory as sendmail.exe) error_logfile=error.log ; create debug log as debug.log (defaults to same directory as sendmail.exe) debug_logfile=debug.log ; if your smtp server requires authentication, modify the following two lines [email protected] ; if your smtp server uses pop3 before smtp authentication, modify the pop3_server= ; force the sender to always be the following email address ; force the sender to always be the following email address force_recipient= ; sendmail will use your hostname and your default_domain in the ehlo/helo hostname= |
重新起動 Apache 服務即可。
PS: Windows 8 的 sendmail 不會正常運作
另外要測試是否正常設定了 可使用
<?php $to =" [email protected] "; //收件者 $subject = "test"; //信件標題 $msg = "smtp發信測試";//信件內容 $headers = "From: [email protected]"; //寄件者 if(mail("$to", "$subject", "$msg", "$headers")): echo "信件已經發送成功。";//寄信成功就會顯示的提示訊息 else: echo "信件發送失敗!";//寄信失敗顯示的錯誤訊息 endif; ?>
參考資料來源: https://sofree.cc/php-smtp-mail/