第一步
我用的是163邮箱发送邮件,做一个尝试,在尝试之前,需要要开启163邮箱的授权码如图所示,请记住您的授权码,将在之后的步骤中用到 image第二步
需要下载一个类PHPMailer,我有这个资源已经上传了,免费的哦亲,连接在这http://download.csdn.NET/detail/s371795639/9693417
下载后,解压后此文件夹放在Vendor目录下,Vendor目录下有个PHPMailer文件夹,那就对了~
第三步
咱们该写代码了
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"/>
标题:<input type="text" id="title" name="title"/>
内容<input type="text" id="content" name="content"/>
<input class="button" type="submit" name="submit" value="发送" style="margin: 0 auto;display: block;"/>
</form>
</body></pre>
对应的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()//发送邮件来修改密码
{
**if**(**isset**($_POST['submit']))
{
**if**(SendMail($_POST['mail'],$_POST['title'],$_POST['content']))
$this->success('发送成功!');
**else** $this->error('发送失败');
}
$this->display();
}</pre>
第四步
这是关键
在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
- / /
- *** 邮件发送函数 */ function sendMail($to, $title, $content) {
Vendor('PHPMailer.PHPMailerAutoload');
$mail = new PHPMailer(); //实例化
$mail->IsSMTP(); // 启用SMTP
$mail->Host=C('MAIL_HOST'); //smtp服务器的名称(这里以QQ邮箱为例)
$mail->SMTPAuth = C('MAIL_SMTPAUTH'); //启用smtp认证
$mail->Username = C('MAIL_USERNAME'); //你的邮箱名
$mail->Password = C('MAIL_PASSWORD') ; //邮箱密码
$mail->From = C('MAIL_FROM'); //发件人地址(也就是你的邮箱地址)
$mail->FromName = C('MAIL_FROMNAME'); //发件人姓名
$mail->AddAddress($to,"尊敬的客户");
$mail->WordWrap = 50; //设置每行字符长度
$mail->IsHTML(C('MAIL_ISHTML')); // 是否HTML格式邮件
$mail->CharSet=C('MAIL_CHARSET'); //设置邮件编码
$mail->Subject =$title; //邮件主题
$mail->Body = $content; //邮件内容
$mail->AltBody = "这是一个纯文本的身体在非营利的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(
//'配置项'=>'配置值'
'MAIL_HOST' =>'smtp.163.com',//smtp服务器的名称
'MAIL_SMTPAUTH' =>TRUE, //启用smtp认证
'MAIL_USERNAME' =>'s371795639@163.com',//你的邮箱名
'MAIL_FROM' =>'s371795639@163.com',//发件人地址
'MAIL_FROMNAME'=>'尘中客',//发件人姓名
'MAIL_PASSWORD' =>'*******',//邮箱授权码
'MAIL_CHARSET' =>'utf-8',//设置邮件编码
'MAIL_ISHTML' =>TRUE, // 是否HTML格式邮件
);</pre>
然后应该就没有问题了,至少我的是这样。如果用QQ邮箱发送貌似一直失败,修改上面的配置也是不行。也不知道为啥。
网友评论