PHP 需要一個已安裝且正在執行的郵件系統,以便使郵件函數mail()可用。所使用的郵件系統程式透過在 php.ini 檔案中進行設定。
用以下程式碼傳送郵件,可總是發送失敗。
[PHP] view plaincopyprint?
<?php $to = "xxxxxxxx@qq.com"; $subject = "Test mail"; $message = "Hello! This is a simple email message."; mail($to,$subject,$message); ?>
經查,PHP 需要一個已安裝且正在執行的郵件系統,以便使郵件函數mail()可用。所使用的郵件系統程式透過在 php.ini 檔案中進行設定。具體解決方法如下:
1.安裝sendmail
sudo apt-get install sendmail
sudo apt-get install sendmail-cf
sudo apt-get install mailutils
2.設定sendmail
#開啟/etc/mail/sendmail.mc,找到如下行:
DAEMON_OPTIONS('Family=inet, Name=MTA-v4, Port=smtp, Addr=127.0.0.1')dnl
將127.0.0.1改為0.0.0.0
執行如下指令備份舊的設定檔,產生新的設定檔:
cd /etc/mail
mv sendmail.cf sendmail.cf~
m4 sendmail.mc > sendmail.cf
3.測試
echo "This is a test mail." | mail -s "test" xxxxxxxx@qq.com
注意:該郵件會被當作垃圾郵件接收,另外如果使用163郵箱可能會被直接過濾掉而無法收到。
4.設定php.ini檔案
開啟/etc/php5/apache2/php.ini,找到如下行:
; sendmail_path =
將該行改為:sendmail_path = /usr/sbin /sendmail -t
這裡特別需要注意的是「-t"參數必不可少,如果缺少了它,當透過瀏覽器執行原始php檔案時,mail()函數會執行失敗,但是如果直接在伺服器端使用php命令列執行原php檔案則不會有問題。
5.重啟apache
sudo /etc/init.d/apache2 restart
以上是php無法使用mail函數原因分析及解決方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

tomakephpapplicationsfaster,關注台詞:1)useopcodeCachingLikeLikeLikeLikeLikePachetoStorePreciledScompiledScriptbyTecode.2)MinimimiedAtabaseSqueriSegrieSqueriSegeriSybysequeryCachingandeffeftExting.3)Leveragephp7 leveragephp7 leveragephp7 leveragephpphp7功能forbettercodeefficy.4)

到ImprovephPapplicationspeed,關注台詞:1)啟用opcodeCachingwithapCutoredUcescriptexecutiontime.2)實現databasequerycachingingusingpdotominiminimizedatabasehits.3)usehttp/2tomultiplexrequlexrequestsandreduceconnection.4 limitesclection.4.4

依赖注入(DI)通过显式传递依赖关系,显著提升了PHP代码的可测试性。1)DI解耦类与具体实现,使测试和维护更灵活。2)三种类型中,构造函数注入明确表达依赖,保持状态一致。3)使用DI容器管理复杂依赖,提升代码质量和开发效率。

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

phpisusedforsenderemailsduetoitsbuilt-inmail()函數andsupportivelibrariesLikePhpMailerAndSwiftMailer.1)usethemail()functionForbasiceMails,butithasimails.2)butithasimail.2)

依賴性(di)inphpenhancesCodeFlexibility andTestability by decouplingClassesscyclasses fromtheippentencies.1)UseConstructorientoctionTopAssDopassDectiesViactructors Viactructors

推薦Pimple用於簡單項目,Symfony的DependencyInjection用於復雜項目。 1)Pimple適合小型項目,因其簡單和靈活。 2)Symfony的DependencyInjection適合大型項目,因其功能強大。選擇時需考慮項目規模、性能需求和學習曲線。

依賴性注射(DI)InphpisadesignpatternwhereClassDepentenciesArepassedtotosedTosedTosedTotratherThancReateDinterally,增強codemodemodularityAndTestabily.itimprovessoftwarequalitybyby By:1)增強tosestabilityTestabilityTestabilityThroughityThroughEasyDepentyDepententymydependentymocking,2)增強Flexibilybya


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

記事本++7.3.1
好用且免費的程式碼編輯器

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境