PHP實現郵件傳送的方法

2021-08-15 07:31:19 字數 1622 閱讀 5983

這裡簡要介紹一下在php語言中實現郵件傳送的兩種方法,分別是使用socket傳送和使用mail()函式傳送的方式。

1.使用socket傳送原理:使用fsockopen函式開啟乙個internet連線,函式的引數列表為fsockopen(string hostname,int port,int [errno],string [errstr],int [timeout]),這裡由於要使用smtp協議,所以埠號是25.在開啟連線成功後,會返回乙個socket控制代碼,使用它就可以像使用檔案控制代碼一樣,可使用的操作有fputs(),fgets(),feof(),fclose()等。該函式的返回值有以下幾種:

但是,需要注意的是使用socket傳送要保證開啟php的fsockopen()函式,你可能需要:

2.使用mail()函式傳送:mail()函式是php語言自帶的乙個郵件傳送函式,但是並不代表使用它可以直接進行郵件傳送,需配置php.ini郵件資訊、需要類似sendmail這樣的元件支援。

(2)配置php.ini,需配置內容如下:

[mail function]; for win32 only.smtp = localhostsmtp_port = 25; for win32 only.sendmail_from = [email protected]; for unix only. you may supply arguments as well (default: "sendmail -t -i").sendmail_path ="d:/sendmail/sendmail.exe -t -i"
(3)配置sendmail.ini,需配置內容如下:

smtp_server=smtp.qq.comsmtp_port=25smtp_ssl=autoerror_logfile=error.logdebug_logfile=debug.logauth_username=yourname such as [email protected]_password=yourpassword such as 123456pop3_server=pop3_username=pop3_password=
smtp_server=,這個需要填寫你想選用的smtp伺服器,可以選擇本地搭建,也可以選擇163、qq、gmail(如果是gmail需要伺服器啟用ssl_module modules)的smtp或者pop3伺服器。注意第一次使用建議把debug_logfile=debug.log前面的;去掉,這樣可以記錄每次傳送郵件的具體資訊了,如果發生錯誤可以在error.log裡面找到。auth_username=、auth_password=需要填寫你的stmp賬戶資訊(具體可以google一下),如果是pop3則對應填寫。填寫一類就可以了。

(4)測試**

$now = date("y-m-d h:i:s");$from_name='iecspace';$from_email ='[email protected]';$to='[email protected]';$headers = "from: ";$message= "這是一封來自 的測試郵件.";$subject = "[$now] 郵件測試";if (mail($to, $subject,$message,$headers))  else ?>

php實現傳送郵件

smtp.php class smtp main function function sendmail to,from,subject body mailtype,cc bcc additional headers header to to.r n if cc header from from r ...

PHP傳送郵件功能實現

第一步 我用的是163郵箱傳送郵件,做乙個嘗試,在嘗試之前,需要要開啟163郵箱的授權碼如圖所示,請記住您的授權碼,將在之後的步驟中用到 第二步 第三步咱們該寫 了 html 對應的controller的php public functionchangepwd 傳送郵件來修改密碼 this disp...

PHPMailer實現PHP郵件傳送

2.解壓 從中取出class.phpmailer.php 和 class.smtp.php 放到你的專案的資料夾,因為我們等下會引用到它們.3.建立傳送郵件的函式,其中你需要配置smtp伺服器 function postmail to,subject body else 4.使用函式 postmai...