好多人在用php的mail()函數發送郵件時不成功,折騰半天也沒找到什么問題。
我昨天也遇到這情況了,現在記錄一下,以便以后再遇到這問題。
1:要用mail首先你要把sendMail擴展打開,我用的phpstudy,打開很簡單,把勾打上就可以了。
2:然后打開php.ini然后搜索“[mail function]”,找到下面文本塊,把我加紅加粗的前面的;去掉,然后修改成你的郵件服務器,我用的163的,qq的比較麻煩,所以用的163。
sendmail_path="D:\phpStudy\tools\sendmail\sendmail.exe -t" //這個后面的地址就是你的sendmail.exe的地址,這樣如果你配置php環境時沒有安裝直接下載sendmail安裝一下就可以了。
; For Win32 only. ; http://php.net/smtp SMTP = smtp.163.com ; http://php.net/smtp-port smtp_port = 25 ; For Win32 only. ; http://php.net/sendmail-from sendmail_from = intrwins@163.com ; For Unix only. You may supply arguments as well (default: "sendmail -t -i"). ; http://php.net/sendmail-path ; Force the addition of the specified parameters to be passed as extra parameters ; to the sendmail binary. These parameters will always replace the value of ; the 5th parameter to mail(), even in safe mode. ;mail.force_extra_parameters = ; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename mail.add_x_header = On
sendmail_path="D:\phpStudy\tools\sendmail\sendmail.exe -t"
3:配置sendmail.ini
然后找到你的sendmail文件夾,找到sendmail.ini文件,搜索“[sendmail]”,配置相關信息。
4:同樣的設置一下,看紅色加粗部分POP3_這三個是設置接取郵件的配置,需要的也可以設置下,我們現在設置的是發送的配置。
smtp_server=smtp.163.com ; smtp port (normally 25) smtp_port=25 ; 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=auto
auth_username=****** //你的用戶名
auth_password=********** //你的郵件密碼
; if your smtp server uses pop3 before smtp authentication, modify the
; following three lines. do not enable unless it is required.
pop3_server=
pop3_username=
pop3_password=
; force the sender to always be the following email address
; this will only affect the "MAIL FROM" command, it won't modify
; the "From: " header of the message content
force_sender=intrwins@163.com
5:重啟你的php環境,然后再試試應該就可以了。
<?php $to = "zzndc@qq.com"; $subject = "我就是在測試php的mail函數,請163別給我退回"; $message = "我就是在測試php的mail函數,請163別給我退回"; $from = "intrwins@163.com"; $headers = "From: $from"; if(mail($to,$subject,$message,$headers)){ echo "發送成功"; }else{ echo "發送失敗"; } ?>