tp5发送邮箱

前期准备

1.获取开源的phpmail类

        链接:https://pan.baidu.com/s/1mabENEdQal8_qZ5gh5ZtGA 
        提取码:4fb4 

2.开启stmp服务

3.开启授权码

     注意密码一定要记住

4.把下载的phpmail类放到tp5中的extend目录中

5.配置Email.php

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

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 {
            $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;
$mail->Debugoutput = 'html';
$mail->Host = config('email.host');  //SMTP服务器: smtp.163.com
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = config('email.port');  //端口号
$mail->SMTPAuth = true;
$mail->Username = config('email.username');  //邮箱号   
$mail->Password =config('email.password');   //授权码
$mail->setFrom(config('email.username'), '官网');  
$mail->addAddress($to);                      //要发送的邮箱号 
$mail->Subject = $title;                     //发送标题
$mail->msgHTML($content);                    //发送内容
//send the message, check for errors
if (!$mail->send()) {
      return false;
} else {
   return true;
}
        }catch(phpmailerException $e) {      //抛出异常
            return false;
        }
    }
}

6.在application目录下自己定义一个发送邮件的配置文件

<?php 
 /**
  *发送邮件相关配置
  */

return [
   'host' => "smtp.163.com",    //SMTP服务器: smtp.163.com
   'port' => 25,                   //端口
   'username' => "自己写",         //邮箱号               
   'password' => '自己写',         //授权码


];

7.在需要的控制器中调用

 \phpmailer\Email::send('邮箱号','标题','内容');   return '成功';

如果视图中输出成功,那么邮箱发送成功

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个使用 thinkphp6 发送邮件验证码的示例: 1. 首先,需要在 `config/mail.php` 文件中进行邮箱配置: ```php return [ // 默认发送邮件设置 'default' => [ // 邮件服务器地址 'host' => 'smtp.163.com', // 邮件服务器端口 'port' => 465, // 发件人邮箱地址 'username' => 'your_email@example.com', // 邮箱授权码,非邮箱登录密码 'password' => 'your_email_password', // 邮箱加密方式,ssl 或 tls 'secure' => 'ssl', // 默认发件人 'from' => [ 'address' => 'your_email@example.com', 'name' => 'your_name', ], ], ]; ``` 2. 然后,创建一个 `MailService` 类,用于发送邮件: ```php <?php namespace app\service; use think\facade\Cache; use think\facade\Config; use think\facade\View; use think\facade\Lang; use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; class MailService { /** * 发送邮件验证码 * * @param string $email 邮箱地址 * @param string $type 验证码类型,例如 register、forget * @return boolean */ public function sendVerifyCode($email, $type) { // 生成验证码 $code = mt_rand(100000, 999999); // 邮件主题和内容 $subject = Lang::get('mail.' . $type . '_subject'); $body = View::fetch('mail/' . $type . '_body', ['code' => $code]); // 实例化 PHPMailer 对象 $mail = new PHPMailer(true); try { // 配置 SMTP 服务器 $mail->SMTPDebug = 0; // 调试:0 关闭,1 开启 $mail->isSMTP(); $mail->Host = Config::get('mail.default.host'); $mail->SMTPAuth = true; $mail->Username = Config::get('mail.default.username'); $mail->Password = Config::get('mail.default.password'); $mail->SMTPSecure = Config::get('mail.default.secure'); $mail->Port = Config::get('mail.default.port'); // 设置发件人、收件人、邮件主题、内容 $mail->setFrom(Config::get('mail.default.from.address'), Config::get('mail.default.from.name')); $mail->addAddress($email); $mail->Subject = $subject; $mail->Body = $body; // 发送邮件 $mail->send(); // 将验证码存入缓存,有效期为 5 分钟 Cache::set('verify_code:' . $email, $code, 300); return true; } catch (Exception $e) { return false; } } } ``` 3. 最后,在控制器中调用 `MailService` 类的 `sendVerifyCode` 方法即可: ```php <?php namespace app\controller; use app\service\MailService; use think\facade\Request; class UserController { /** * 发送注册验证码 * * @return json */ public function sendRegisterVerifyCode() { $email = Request::post('email'); if (!validate_email($email)) { return json(['code' => -1, 'msg' => '邮箱地址不正确']); } $mailService = new MailService(); if ($mailService->sendVerifyCode($email, 'register')) { return json(['code' => 0, 'msg' => '验证码已发送']); } else { return json(['code' => -1, 'msg' => '验证码发送失败']); } } } ``` 其中,`validate_email` 是一个自定义的函数,用于验证邮箱地址的合法性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值