thinkphp5如何发送邮件

2025-11-12 20:00:59

1、thinkphp官方下载thinkphp5,官方网址请百度搜索

thinkphp5如何发送邮件

2、安装PHP运行环境,推荐使用phpstydy,phpstydy官方地址请百度搜索

thinkphp5如何发送邮件

3、安装phpstydy并启用,参考下面的链接

4、解压thinkphp5到网站根目录,如图,指向为访问地址

thinkphp5如何发送邮件

5、下载phpmailer并解压,将phpmailer放至 vendor/下

thinkphp5如何发送邮件

6、在\application\index\controller下index.php 使用自定义函数sendMail('接收邮箱', '标题', '内容');

thinkphp5如何发送邮件

7、打开函数文件common.php ,添加函数sendMail

代码如下

function sendMail($email, $title, $content){

  vendor('phpmailer.PHPMailerAutoload');

  $config = \think\Db::name('system_config')->select();

    $mail = new PHPMailer;

  $mail->CharSet = 'UTF-8';

    $mail->isSMTP();

    $mail->SMTPDebug = 0;

    if($config['is_ssl']){

    $mail->SMTPSecure = 'ssl';

    }

    $mail->Debugoutput = 'html';

    $mail->Host = $config['smtp'];

    $mail->Port = $config['email_port'];

    $mail->SMTPAuth = true;

    $mail->Username = $config['emailuser'];

    $mail->Password = $config['emailpass'];

    $mail->setFrom($config['from_email']);

    $mail->addAddress($email);

    $mail->FromName = $config['from_name'];

    $mail->Subject = $title;

    $mail->Body = $content;

    if (!$mail->send()) {

      // echo "发送失败: " . $mail->ErrorInfo;

      return false;

    } else {

      // echo "发送成功";

      return true;

    }

}

thinkphp5如何发送邮件

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢