第一步
我用的是163郵箱發(fā)送郵件,做一個(gè)嘗試,在嘗試之前,需要要開啟163郵箱的授權(quán)碼如圖所示,請(qǐng)記住您的授權(quán)碼,將在之后的步驟中用到
第二步
需要下載一個(gè)類PHPMailer,我有這個(gè)資源已經(jīng)上傳了,免費(fèi)的哦親,連接在這http://download.csdn.NET/detail/s371795639/9693417
下載后,解壓后此文件夾放在Vendor目錄下,Vendor目錄下有個(gè)PHPMailer文件夾,那就對(duì)了~
第三步
咱們?cè)搶懘a了
html代碼:
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(57, 57, 57); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(250, 247, 239); text-decoration-style: initial; text-decoration-color: initial;"><body>
<form action="{:U('home/login/changepwd')}" method="post" enctype="multipart/form-data">
郵箱:<input type="text" id="mail" name="mail"/>
標(biāo)題:<input type="text" id="title" name="title"/>
內(nèi)容<input type="text" id="content" name="content"/>
<input class="button" type="submit" name="submit" value="發(fā)送" style="margin: 0 auto;display: block;"/>
</form>
</body></pre>
對(duì)應(yīng)的Controller的PHP代碼:
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(57, 57, 57); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(250, 247, 239); text-decoration-style: initial; text-decoration-color: initial;">public function changepwd()//發(fā)送郵件來修改密碼
{
**if**(**isset**($_POST['submit']))
{
**if**(SendMail($_POST['mail'],$_POST['title'],$_POST['content']))
$this->success('發(fā)送成功!');
**else** $this->error('發(fā)送失敗');
}
$this->display();
}</pre>
第四步
這是關(guān)鍵
在Common下建立function.PHP輸入代碼如下
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(57, 57, 57); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(250, 247, 239); text-decoration-style: initial; text-decoration-color: initial;"><?php /*
- ** Created by PhpStorm.* ** User: Administrator* ** Date: 2016/11/25 0025* ** Time:* 上午 *11:49
- / /
- *** 郵件發(fā)送函數(shù) */ function sendMail($to, $title, $content) {
Vendor('PHPMailer.PHPMailerAutoload');
$mail = new PHPMailer(); //實(shí)例化
$mail->IsSMTP(); // 啟用SMTP
$mail->Host=C('MAIL_HOST'); //smtp服務(wù)器的名稱(這里以QQ郵箱為例)
$mail->SMTPAuth = C('MAIL_SMTPAUTH'); //啟用smtp認(rèn)證
$mail->Username = C('MAIL_USERNAME'); //你的郵箱名
$mail->Password = C('MAIL_PASSWORD') ; //郵箱密碼
$mail->From = C('MAIL_FROM'); //發(fā)件人地址(也就是你的郵箱地址)
$mail->FromName = C('MAIL_FROMNAME'); //發(fā)件人姓名
$mail->AddAddress($to,"尊敬的客戶");
$mail->WordWrap = 50; //設(shè)置每行字符長度
$mail->IsHTML(C('MAIL_ISHTML')); // 是否HTML格式郵件
$mail->CharSet=C('MAIL_CHARSET'); //設(shè)置郵件編碼
$mail->Subject =$title; //郵件主題
$mail->Body = $content; //郵件內(nèi)容
$mail->AltBody = "這是一個(gè)純文本的身體在非營利的HTML電子郵件客戶端"; //郵件正文不支持HTML的備用顯示
return($mail->Send());
}</pre>
在Conf下的config.php輸入配置如下
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(57, 57, 57); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(250, 247, 239); text-decoration-style: initial; text-decoration-color: initial;"><?php return array(
//'配置項(xiàng)'=>'配置值'
'MAIL_HOST' =>'smtp.163.com',//smtp服務(wù)器的名稱
'MAIL_SMTPAUTH' =>TRUE, //啟用smtp認(rèn)證
'MAIL_USERNAME' =>'s371795639@163.com',//你的郵箱名
'MAIL_FROM' =>'s371795639@163.com',//發(fā)件人地址
'MAIL_FROMNAME'=>'塵中客',//發(fā)件人姓名
'MAIL_PASSWORD' =>'*******',//郵箱授權(quán)碼
'MAIL_CHARSET' =>'utf-8',//設(shè)置郵件編碼
'MAIL_ISHTML' =>TRUE, // 是否HTML格式郵件
);</pre>
然后應(yīng)該就沒有問題了,至少我的是這樣。如果用QQ郵箱發(fā)送貌似一直失敗,修改上面的配置也是不行。也不知道為啥。