PHP學習筆記一:誰動了你的mail(),PHP?
PHP編寫郵件發送的函數時候,會出現一個很奇怪的問題,那就是:
Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in .......
of course,這個紅色不是網頁上顯示的,只是爲了這樣說明,不過你可以設置顯示的顏色,這個嘛,這個要留給你考究了。
通過internet搜索了相關的回答,包括相關的技術博客,大家都是很簡單的闡述了解決方案.大多是只是提到修改:php.ini 中的[mail function] 函數,將smtp的相關功能進行打開即可,以下是詳細的修改:
[mail function]
SMTP = localhost
smtp_port = 25
sendmail_from = youremailUsual@domin.com
;這裡有必要註釋下,這個郵件是From方面.
sendmail_path = ""
;起初是空的,這裡在後面做說明.
mail_add_x_header = on
;這裡是默認on的 無需更改
到這一步,修改完畢,重啟apache,當然這個操作,我實在CMD下操作的,話說這個操作,還鬧了一個小笑話.How stupid i am !
重啟完畢,刷新頁面,錯誤仍然存在.
這裡陷入糾結的地步,stackoverflow 中提到了IIS(windows 默認的服務器),在控制面板-管理工具下竟然找不到這一項,由於是win7的緣故,在添加和刪除程序下,把它開啟即可,注意操作,後來可以在管理工具下找到,雙擊打開,此時看到smtp的項都很激動.雙擊打開,配置了相關的參數,如截圖所示:
更正:此處忘了補發圖片,特此道歉! 7-5號補正.
說句實話,這一步做的比較腦殘,那上面是ASP.net的選項,管你PHP毛事.[反正就是沒有成功].
痛定思痛,維基百科查了smtp的協議詳細解釋,連接Click Here:簡單郵件協議
回來繼續修改,[mail function],如下:
;以126郵箱的設置為例子
SMTP = smtp.126.com
smtp_port = 25
sendmail_from = my126EmailAddress@126.com
sendmail_path = ""
mail_add_x_header = on
同時修改了程序中$from變量的數值,為了保持一致.
[因為之前做了測試,程序中添加 phpinfo()函數,執行後,在頁面搜索 mail項,你會發現會有一個 Localhost Value 和 master Value ,當然這個很好理解,Localhost Value指的是你程序中設置的數值,master Value是php.ini的設置]
執行後,神奇的錯誤消失了,不過出現了如下的錯誤:
mail(): SMTP server response: 553 You are not authorized to send mail, authentication is required aaf353e4-8ac5-416e-9cd5-6b54fc592764
之後改了其他的郵箱,如QQ,Gamil等,錯誤都如上面提示,看了一個熟悉的單詞authorized ,就明白啥意思了.
到這一步貌似離成功不遠了,想起以前使用客戶端配置郵箱用戶的時候,最後一步都會詢問你是不是需要開啟"服務器身份驗證".最後一不小心搜到了一個叫做sendmail的文件包,下載地址如下:sendmail
之後,最好在PHP的根目錄下新建一個文件夾,取名叫做:sendmail,之後托壓縮包進去,再解壓出來,修改一個叫做sendmail.ini的文件,修改的地方如下:
;take the fomus email provider in China 126 for example
[sendmail]
smtp_server= smtp.126.com
smtp_port=25
default_domin =126.com
auth_username= strings in your email address before char '@'
auth_password= your password which allow you to login in your email-box
hostname= 126.com
之後,你就要修改 php.ini,還是老地方:
SMTP = smtp.126.com
;這裡修改為localhost 也是一樣,其他選項保持不變.
smtp_port = 25
sendmail_from = my126EmailAddress@126.com
sendmail_path = "your sendmail path/sendemail.exe -t "
;當然這個路徑的前半部分必須包括你的PHP安裝的根目錄,將"\"換成"/",我想是必須要做的.
mail_add_x_header = on
到這裡配置完成,重啟你的apache,刷新頁面,錯誤的消失,因為程序中設置的$to是自己的126和QQ郵箱,當然循環發的.
當看到桌面右下角QQ上彈出三豐郵件提示的時候,I know that I resolve it .打開郵件的內容,和我在頁面輸入的內容完全一致,而且有些內容還是從本地的數據庫中讀出來的。
作者:田浩宇

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
