《TP5.0学习笔记---发送邮件服务封装》
来源:互联网 发布:淘宝 多隆 编辑:程序博客网 时间:2024/05/24 00:40
1, 获取开源的phpmail类
2, 开启stmp服务
3, 测试发送邮件
1, 获取开源的phpmail类
链接:http://pan.baidu.com/s/1hsouWBU 密码:nb5l
下载之后解压到我们相应的目录就可以使用了
我们同样是要将其放在我们TP5框架目录的extend目录下的(也就是将phpmail.zip解压到extend目录下)
2, 开启stmp服务
登陆126/163邮箱->设置
大家可以申请一个自己的邮箱,来进行测试
我这里已经申请好了
在这里我们需要进行stmp授权的设置
进入你刚刚注册的那个邮箱在顶部找到“设置->POP3/SMTP/IMAP“点击进去
然后在左边的导航栏中找到“客户端授权密码“点击进去
认证之后,它就会让你设置一个授权密码,这个密码一定要记清楚,在写邮件发送的时候需要用到
这个地址也是会在发送邮件的时候用到
明确之后我们就来做一个测试 进入刚刚解压到extend中的那个phpmail目录下找到testmail.php并打开,按照我下边的那样填写,每行代码的含义我会在旁边注释
testmail.php<?php/** * This example shows making an SMTP connection with authentication. *///SMTP needs accurate times, and the PHP time zone MUST be set//This should be done in your php.ini, but this is how to do it if you don't have access to thatheader("content-type:text/html;charset=utf-8");require 'class.phpmailer.php';require 'class.smtp.php';date_default_timezone_set('PRC');//set time//Create a new PHPMailer instance$mail = new PHPMailer; //新建一个邮件发送对象//Tell PHPMailer to use SMTP$mail->isSMTP(); //以SMTP的方式来发送邮件的//Enable SMTP debugging// 0 = off (for production use)// 1 = client messages// 2 = client and server messages$mail->SMTPDebug = 2; //开启调试模式,在邮件发送正式上线之后,这个功能就要注释掉//Ask for HTML-friendly debug output$mail->Debugoutput = 'html';//Set the hostname of the mail server$mail->Host = ""; //这个就是填写刚才的服务器地址//Set the SMTP port number - likely to be 25, 465 or 587$mail->Port = 25; //端口号//Whether to use SMTP authentication$mail->SMTPAuth = true;//Username to use for SMTP authentication$mail->Username = ""; //这个就填写你刚才注册的邮箱//Password to use for SMTP authentication$mail->Password = ""; //这个密码就填写你的客户端授权密码(不是邮箱密码)//Set who the message is to be sent from$mail->setFrom('', 'shulv');//第一个参数指是谁发送的(就填写你刚申请的邮箱地址),第二个参数就是设置一个名称//Set an alternative reply-to address//$mail->addReplyTo('replyto@example.com', 'First Last');//Set who the message is to be sent to$mail->addAddress('', 'Taylor');//第一个参数表示要把邮件发送给谁,填写那个人的邮箱地址即可,第二个参数同样是设置一个名称//Set the subject line$mail->Subject = 'Email title';//这个是邮件标题//Read an HTML message body from an external file, convert referenced images to embedded,//convert HTML into a basic plain-text alternative body$mail->msgHTML('This is a test');//这个就是填写邮件的正文//Replace the plain text body with one created manually//$mail->AltBody = 'This is a plain-text message body';//Attach an image file//$mail->addAttachment('images/phpmailer_mini.png');//send the message, check for errorsif (!$mail->send()) { echo "Mailer Error: " . $mail->ErrorInfo;} else { echo "Message sent success!";}
我们直接执行这个文件就可以测试我们的邮件是否发送成功
测试成功之后,那么我们就开始“打造属于TP5的phpmailer类库“
打造属于TP5的phpmailer类库
**1, 修改第三方类库文件
2, 封装email类库**
首先我们将解压出来的那几个文件的文件名修改一下,因为他们不符合TP5的命名规范,所做修改如下:
class.phpmailer.php -> Phpmailer.php
class.smtp.php -> Smtp.php
class.pop3.php -> Pop3.php //其实这个用不到,我们可以将它删掉
修改完成之后,打开Phpmailer.php
给这个文件添加一个命名空间
namespace phpmailer;
use phpmailer
在Smtp.php中也添加上同样的命名空间
然后拉到Phpmailer.php文件的最底部,大概4030行,将” Exception”改成“\Exception “
完成这些之后,我们开始创建一个类文件(我这里命名为Email.php),文件创建在phpmailer目录下
Email.php<?php/*发送邮件类库*/namespace phpmailer;class Email{ publuc static function send($to, $title, $content){ //这里边的内容其实就是testemail.php里边的内容(只不过多加了一个try来捕获异常) date_default_timezone_set('PRC');//set time if(empty($to)){ return false; } try{ //Create a new PHPMailer instance $mail = new PHPMailer; //Tell PHPMailer to use SMTP $mail->isSMTP(); //Enable SMTP debugging // 0 = off (for production use) // 1 = client messages // 2 = client and server messages $mail->SMTPDebug = 2; //Ask for HTML-friendly debug output $mail->Debugoutput = 'html'; //Set the hostname of the mail server $mail->Host = config('email.host'); //和封装百度地图一样,也是将配置放在单独的文件中(http://blog.csdn.net/self_realian/article/details/77982798) //Set the SMTP port number - likely to be 25, 465 or 587 $mail->Port = config('email.port'); //Whether to use SMTP authentication $mail->SMTPAuth = true; //Username to use for SMTP authentication $mail->Username = config('email.username'); //Password to use for SMTP authentication $mail->Password = config('email.password'); //Set who the message is to be sent from $mail->setFrom(config('email.username'), 'shulv');//表示谁发送的 //Set an alternative reply-to address //$mail->addReplyTo('replyto@example.com', 'First Last'); //Set who the message is to be sent to $mail->addAddress($to);//发送给谁 //Set the subject line $mail->Subject = $title;//标题 //Read an HTML message body from an external file, convert referenced images to embedded, //convert HTML into a basic plain-text alternative body $mail->msgHTML($content);//正文内容 //Replace the plain text body with one created manually //$mail->AltBody = 'This is a plain-text message body'; //Attach an image file //$mail->addAttachment('images/phpmailer_mini.png'); //send the message, check for errors if (!$mail->send()) { return false; //echo "Mailer Error: " . $mail->ErrorInfo; } else { return true; //echo "Message sent success!"; } }catch(phpmailerException $e){ return false; } }}
application/extra/email.php<?php/*发送邮件相关配置*/return [ 'host' => '服务地址', 'post' => 端口, 'username' => '邮箱',//填写你的邮箱 'password' => '授权密码',//客户端授权密码];
测试:Application/index/index/emailpublic function email(){ //测试 \phpmailer\Email\::send('1843704293@qq.com','tp5-email','test'); return "发送邮件成功"; }
- 《TP5.0学习笔记---发送邮件服务封装》
- 《TP5.0学习笔记---百度地图封装》
- Tp5.0 PHPMailer邮件发送
- Python 学习笔记 发送邮件
- 《TP5.0学习笔记---配置篇》
- 《TP5.0学习笔记---配置篇》
- Linux学习笔记 --邮件服务
- 自己封装邮件发送
- 封装JavaMail发送邮件
- Spring学习笔记-利用Spring发送邮件
- 【Python学习笔记】-自动发送邮件脚本
- 8.python学习笔记:python发送邮件
- Java发送邮件服务
- Java发送邮件服务
- Maven邮件发送服务
- TP5代码学习笔记:Loader
- Python 发送QQ邮件封装
- java发送邮件封装类
- Bone Collector(01背包模板)
- 算法预备学习--C++,java算法与数据结构
- Robot Framework自动化测试(四)--- 分层思想
- Codeforces 106D Treasure Island【思维+二维前缀和】
- Java以UTF-8编码读写文件
- 《TP5.0学习笔记---发送邮件服务封装》
- (一)顺序表和单链表的初始化、插入、删除等操作
- Redis数据备份与恢复
- 关于使用Listview,item内子项列表数据为空时解决方案
- 解决Failed to load D:\Android-Studio\sdk\build-tools\26.0.1\lib\dx.jar
- Bzoj-1131 Sta [DFS]
- Mybatis——动态SQL详解
- 上传下载
- Redis之Hash类型操作