PHP測試成功的郵件發(fā)送案例
mail()函數(shù)的作用:連接到郵件服務(wù)器,利用smtp協(xié)議,與該服務(wù)器交互并投郵件。
注意:
1、mail函數(shù)不支持esmtp協(xié)議,---即,只能直投,不能登陸
2、由上條,我們只能直投至最終的收件服務(wù)器地址.而該地址,又是在PHP.ini中指定的,所以我們想用mail()函數(shù)往 aseoev@163.com發(fā)信的話,我們要---
1)查詢163郵件服務(wù)器的地址
2)把該地址寫到php.ini里去
php實(shí)例代碼如下:
SMTP = 163mx02.mxmail.netease.com sendmail_from = wusong@192.168.1.100 var_dump(mail('12345678@qq.com','from php mail function','very intresting'));
但是使用php自帶的mail函數(shù)發(fā)送郵件我們需要在linux中安裝一個(gè)sendmail組件才可以否則無法使用。
如果你沒有這個(gè)sendmail組件我們可以使用phpmailer函數(shù)來操作,例子代碼如下:
<?php require('./PHPMailer/class.phpmailer.php'); $phpmailer = new PHPMailer(); $phpmailer->IsSMTP(); $phpmailer->Host = 'smtp.163.com'; $phpmailer->SMTPAuth = true; $phpmailer->Username = ''; $phpmailer->Password = ''; $phpmailer->CharSet = 'utf-8'; $phpmailer->From = ''; $phpmailer->FromName = ''; $phpmailer->Subject = ''; $phpmailer->Body = ''; $phpmailer->AddAddress('never_kiss@163.com','Aseoe'); echo $phpmailer->send()?'發(fā)送成功':'發(fā)送失敗'; ?>
上面不帶內(nèi)容,面看個(gè)帶內(nèi)容的,代碼如下:
<?php /** 用PHPMailer類來發(fā)信 步驟: 0: 引入 1: 實(shí)例化 2: 配置屬性 3: 調(diào)用發(fā)送 **/ require('./PHPMailer/class.phpmailer.php'); $phpmailer = new PHPMailer(); /* 設(shè)置phpmailer發(fā)信用的方式 可用用win下mail()函數(shù)來發(fā) 可以用linux下sendmail,qmail組件來發(fā) 可以利用smtp協(xié)議登陸到某個(gè)賬戶上,來發(fā) */ $phpmailer->IsSMTP(); // 用smtp協(xié)議來發(fā) $phpmailer->Host = 'smtp.163.com'; $phpmailer->SMTPAuth = true; $phpmailer->Username = ''; //發(fā)送郵箱的賬號(hào)(用163郵箱發(fā)信的賬號(hào)) $phpmailer->Password = ''; //發(fā)送郵箱的密碼 // 可以發(fā)信了 $phpmailer->CharSet='utf-8'; $phpmailer->From = 'never_4ill@163.com'; $phpmailer->FromName = 'neverkill'; $phpmailer->Subject = 'Superstart Aseoe'; $phpmailer->Body = '腳本之家(http://www.dbjr.com.cn 專注前端開發(fā)與編程設(shè)計(jì).'; //設(shè)置收信人 $phpmailer->AddAddress('never_4ill@163.com','neverkill'); // 添加一個(gè)抄送 $phpmailer->AddCC('1234567','Aseoe'); // 發(fā)信 echo $phpmailer->send()?'ok':'fail';
補(bǔ)充一個(gè)使用上面例子的方法:
直接將phpmailer壓縮包解壓放到根目錄即可運(yùn)行,直接把文件放到本地wamp 根目錄,運(yùn)行02.php 郵件即可發(fā)出(前提php文件可執(zhí)行)-(不行的話 在根目錄建一個(gè)文件夾 重復(fù)操作一次)http://localhost/02.php。
以上就是php發(fā)送郵件的成功案例,希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
PHP開發(fā)實(shí)現(xiàn)微信退款功能示例
這篇文章主要介紹了PHP開發(fā)實(shí)現(xiàn)微信退款功能的方法,涉及php針對(duì)微信接口的相關(guān)調(diào)用操作技巧,需要的朋友可以參考下2017-11-11PHP利用MySQL保存session的實(shí)現(xiàn)思路及示例代碼
使用MySQL保存session,需要保存三個(gè)關(guān)鍵性的數(shù)據(jù):session id、session數(shù)據(jù)、session生命期,下面的示例,大家可以看看2014-09-09PHP中static關(guān)鍵字原理的學(xué)習(xí)研究分析
PHP中static關(guān)鍵字原理的學(xué)習(xí)研究分析,學(xué)習(xí)php的朋友可以參考下。2011-07-07