PHP-Tpthink5学习记录2(地图&邮件)

百度地图应用封装

根据地质获取经纬度类的封装

1.获取第三方接口
进入http://lbsyun.baidu.com/->api控制台->设置AK
注册并创建项目,获取AK

在这里插入图片描述

2.编写map类库
在项目根目录/extend/目录下创建Map.php

在这里插入图片描述
在这里插入图片描述
创建编写地图相关配置文件,在项目根目录/application/目录下创建extra目录/map.php文件

在这里插入图片描述

3.测试

在这里插入图片描述

在这里插入图片描述

根据经纬度或者地址调取百度地图

1.获取第三方接口
http://api.map.baidu.com/staticimage/v2?ak=IAAoqyOUa5wpvo7rADI3xVekcNSLcdjR&mcode=666666&center=116.403874,39.914888&width=300&height=200&zoom=11

2.编写staticimage方法
在这里插入图片描述

3.测试

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

打造属于TP5自己的发送邮件服务

获取开源的phpmail类

在这里插入图片描述
引入文件到项目的extend目录下
在这里插入图片描述

开启stmp服务

  • 登录126/163邮箱->设置

在这里插入图片描述
在这里插入图片描述

测试发送邮件

phpmailer/testemail.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 'mtp.php';
date_default_timezone_set('PRC');//set time

//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 = "smtp.163.com"; //smtp服务地址
//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 = "[email protected]";
//Password to use for SMTP authentication
$mail->Password = "开启smtp时候的授权密码"; //开启smtp时候的授权密码
//Set who the message is to be sent from
$mail->setFrom('[email protected]', 'admin'); //邮箱谁发送的
//Set an alternative reply-to address
//$mail->addReplyTo('[email protected]', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('[email protected]', 'tracy'); //邮件发送给谁
//Set the subject line
$mail->Subject = 'PHPMailer SMTP test';
//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('o2o-test-email');
//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!";
}

打开终端

cd 项目根目录/extend/phpmailer 
//执行 
php testemail.php

在这里插入图片描述

在这里插入图片描述

打造属于tp5的phpmailer类库

修改第三方类库文件

PHPMailer.php
增加 namespace

<?php

namespace phpmailer;
use phpmailer\SMTP;

class phpmailerException extends \Exception
{
    /**
     * Prettify error message output
     * @return string
     */
    public function errorMessage()
    {
        $errorMsg = '<strong>' . $this->getMessage() . "</strong><br />\n";
        return $errorMsg;
    }
}

SMTP.php

<?php

namespace phpmailer; 

封装email类库

创建Email.php文件

<?php
/**
 * 发送邮件类库
 */
namespace phpmailer;
use think\Exception;
use phpmailer\Phpmailer;

class Email {
    /**
     * @param $to
     * @param $title
     * @param $content
     * @return bool
     */
    public  static function send($to, $title, $content) {
        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();

            $mail->Debugoutput = 'html';
            //Set the hostname of the mail server
            $mail->Host = config('email.host');
            //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'), 'singwa');
            //Set an alternative reply-to address
            //$mail->addReplyTo('[email protected]', '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;
            }
        }catch(phpmailerException $e) {
            return false;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/wtdask/article/details/101422921