《TP5.0学习笔记---发送邮件服务封装》

13 篇文章 1 订阅

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 that
header("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 errors
if (!$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/email
public function email(){
        //测试
        \phpmailer\Email\::send('1843704293@qq.com','tp5-email','test');
        return "发送邮件成功";
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值