thinkphp5发送邮件

折腾了一天终于从一个小测试解决了一个小问题,哎爬坑累,
我是自己给自己发邮件,可以改为发给其他人
1.PHPMailer下载地址:https://github.com/PHPMailer/PHPMailer
2.C:\xampp\php找到php.in把extension=php_sockets.dll前的;去掉,
3.解压PHPMailer压缩包,直接复制src到php5项目下的extend下,把src命名为PHPMailer。
4.将里面所有的命名空间“namespace PHPMailer\PHPMailer;”改为“namespace PHPMailer;”,如果赋值过去不改名字,命名为“namespace src;”,记住所有都要改。(强迫症)
5.在application/index/controller下创建一个php文件为Email.php,内容如下:

Email.php
<?php
/**
 * Created by PhpStorm.
 * User: insist
 * Date: 2019/1/22
 * Time: 10:25
 */
/**
 * Created by PhpStorm.
 * User: insist
 * Date: 2018/12/4
 * Time: 16:31
 */
namespace app\index\controller;
//use \PHPMailer\PHPMailer\PHPMailer;

use PHPMailer\PHPMailer;
use PHPMailer\Exception;
use think\Controller;



class Email extends Controller
{
    private  $user = '[email protected]';//邮箱
    private $apwd = 'jfrXXXlhabg';//邮箱密码


    public function index(){
        $mail = new PHPMailer();
        try {
            // 服务器设置
            $mail->SMTPDebug = 2;                                // 开启Debug
            $mail->isSMTP();                                     // 使用SMTP
            $mail->CharSet = "utf8";                             // 编码格式为utf8,不设置编码的话,中文会出现乱码
            $mail->Host = 'smtp.163.com';                        // 服务器地址,列入发送方为163=》$mail->Host = 'smtp.163.com',,,,,,qq为:$mail->Host = 'smtp.qq.com'
            $mail->SMTPAuth = true;                              // 开启SMTP验证
            $mail->Username = $this->user;                       // SMTP 用户名(你要使用的邮件发送账号)
            $mail->Password = $this->apwd;                       // SMTP 密码
            $mail->SMTPSecure = 'tls';                           // 开启TLS 可选
            $mail->Port = 25;                                    // 端口
            $mail->setFrom($this->user, '笑口袋');            // 来自
            $mail->addAddress($this->user);                      // 可以只传邮箱地址
            $mail->addReplyTo($this->user, '笑口袋');         // 回复地址
            $mail->isHTML(true);                                 // 设置邮件格式为HTML
            $mail->Subject = '今天晚上有大新闻';
            $mail->Body    = '今天晚上有大新闻';
            $mail->AltBody = '今天晚上有大新闻';
            $mail->send();
            echo '邮件发送成功。<br>';
        } catch (Exception $e) {
            echo '邮件发送失败。<br>';
            echo 'Mailer Error: ' . $mail->ErrorInfo;
        }
    }
}

截图效果如下:
、复制的文件如下, 在这里插入图片描述
创建发送邮件控制器
在这里插入图片描述
发送了的邮件如下
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_28761593/article/details/86598754