如何将PHPMailer与Codeigniter 3集成 [英] How to integrate PHPMailer with Codeigniter 3

查看:76
本文介绍了如何将PHPMailer与Codeigniter 3集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Codeigniter应用程序中使用GitHUB的 PHPMailer库.

Hi I am trying to use PHPMailer Library from GitHUB in my Codeigniter application.

我下载了代码并解压缩到我的application\library文件夹中. 因此,我有一个名为 vendor 的文件夹,其中包含PHPMailer的源代码.

I downloaded the code and unzipped in my application\library folder. So there I have a folder called vendor inside which resides the source code for PHPMailer.

现在,我创建了一个名为Bizmailer_Controller.php的文件.

Now I created a File named Bizmailer_Controller.php.

<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
* 
*/
class Bizmailer_Controller extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();

        require "vendor\phpmailer\phpmailer\PHPMailerAutoload";
        $this->Bizmailer = new PHPMailer();
        //Set the required config parameters
        $this->Bizmailer->isSMTP();                                      // Set mailer to use SMTP
        $this->Bizmailer->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
        $this->Bizmailer->SMTPAuth = true;                               // Enable SMTP authentication
        $this->Bizmailer->Username = 'user@example.com';                 // SMTP username
        $this->Bizmailer->Password = 'secret';                           // SMTP password
        $this->Bizmailer->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
        $this->Bizmailer->Port = 465;    
        //return $api;
    }
}

现在在我的控制器中,我试图像这样加载它:

Now in my controllers I am trying to load it like this :

$this->load->library('Bizmailer');
$mail = new Bizmailer();

我得到这个错误:

遇到错误

无法加载请求的类:Bizmailer

Unable to load the requested class: Bizmailer

因此,请指导我如何在Codeigniter中加载或集成此库.

So Please guide me how I can load or integrate this library in Codeigniter.

推荐答案

这是指南

从Github下载最新的PHPMailer Build. 您可以在此处

Download the latest PHPMailer Build from Github. You can find the project here

现在单击克隆或下载"并以zip格式下载-如下图所示.

Click now on "clone or download" and download it as zip - as in the image below is shown.

zip中的文件夹称为PHPMailer-master. 将其解压缩到您的application/third_party/文件夹中,然后将该文件夹重命名为phpmailer.你应该看到这样的东西

The folder in the zip is called PHPMailer-master. Unzip this in your application/third_party/ folder and rename the folder to phpmailer. You should see something like this

我最好创建一个库来处理您的PHPMailer对象(Phpmailer_library.php) 这个库看起来像

Imho its best to create a library which handles your PHPMailer Object (Phpmailer_library.php) This library could look like

class Phpmailer_library
{
    public function __construct()
    {
        log_message('Debug', 'PHPMailer class is loaded.');
    }

    public function load()
    {
        require_once(APPPATH."third_party/phpmailer/PHPMailerAutoload.php");
        $objMail = new PHPMailer;
        return $objMail;
    }
}

3.在您的控制器,模型等之一中使用此库.

class Welcome extends CI_Controller {


    public function index()
    {
        $this->load->library("phpmailer_library");
        $objMail = $this->phpmailer_library->load();
    }
}

我认为这应该可以完成工作. 如果您有任何麻烦,请随时询问;)

i think this should pretty much do the job. If you've any troubles, don't hesitate to ask ;)

自从PHPMailer伙计们删除了自动加载器以来,您现在有了两个选择:

Since the PHPMailer guys removed the autoloader you've two options now:

1.)通过Composer

对于那些不知道的人-Codeigniter支持Composer-您只需激活自动加载-您可以在config.php中找到它

for those who didn't know - Codeigniter supports Composer - you simply have to activate the autoload - you can find this in your config.php

$config['composer_autoload'] = true;

有关更多信息,请此处

之后-像这样运行作曲家

After that - run composer like

composer require phpmailer/phpmailer

您现在应该在application/vendor文件夹中包含phpmailer文件.

You now should have within your application/vendor folder the phpmailer files.

库应该看起来像

class Phpmailer_library
{
    public function __construct()
    {
        log_message('Debug', 'PHPMailer class is loaded.');
    }

    public function load()
    {
        $objMail = new PHPMailer\PHPMailer\PHPMailer();
        return $objMail;
    }
}

2.)下载

按照步骤1

库应该看起来像

class Phpmailer_library
{
    public function __construct()
    {
        log_message('Debug', 'PHPMailer class is loaded.');
    }

    public function load()
    {
        require_once(APPPATH.'third_party/phpmailer/src/PHPMailer.php');
        require_once(APPPATH.'third_party/phpmailer/src/SMTP.php');

        $objMail = new PHPMailer\PHPMailer\PHPMailer();
        return $objMail;
    }
}

,其他所有内容都应保持不变

and everything else should remain the same

这篇关于如何将PHPMailer与Codeigniter 3集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆