Home php教程 php手册 PHP學習筆記一:誰動了你的mail(),PHP?

PHP學習筆記一:誰動了你的mail(),PHP?

Jun 13, 2016 am 10:50 AM
mail php one of

 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 .打開郵件的內容,和我在頁面輸入的內容完全一致,而且有些內容還是從本地的數據庫中讀出來的。 

作者:田浩宇

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles