简体   繁体   English

如何将PHPMailer与Codeigniter 3集成

[英]How to integrate PHPMailer with Codeigniter 3

Hi I am trying to use PHPMailer Library from GitHUB in my Codeigniter application. 嗨,我正在尝试在Codeigniter应用程序中使用GitHUB的PHPMailer库

I downloaded the code and unzipped in my application\\library folder. 我下载了代码并将其解压缩到我的application\\library文件夹中。 So there I have a folder called vendor inside which resides the source code for PHPMailer. 因此,我有一个名为vendor的文件夹,其中存放PHPMailer的源代码。

Now I created a File named Bizmailer_Controller.php . 现在,我创建了一个名为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();

And I Get this error : 我得到这个错误:

An Error Was Encountered 遇到错误

Unable to load the requested class: Bizmailer 无法加载请求的类:Bizmailer

So Please guide me how I can load or integrate this library in Codeigniter. 因此,请指导我如何在Codeigniter中加载或集成此库。

here is a guide 这是一个指南

1. installing PHP Mailer 1.安装PHP Mailer

Download the latest PHPMailer Build from Github. 从Github下载最新的PHPMailer Build。 You can find the project here 你可以在这里找到项目

Click now on "clone or download" and download it as zip - as in the image below is shown. 现在单击“克隆或下载”,然后以zip格式下载-如下图所示。 PHP Mailer下载为ZIP

The folder in the zip is called PHPMailer-master. zip中的文件夹称为PHPMailer-master。 Unzip this in your application/third_party/ folder and rename the folder to phpmailer. 将其解压缩到您的application / third_party /文件夹中,然后将该文件夹重命名为phpmailer。 You should see something like this 你应该看到这样的东西 在此处输入图片说明

2. PHP Mailer Library 2. PHP Mailer库

Imho its best to create a library which handles your PHPMailer Object (Phpmailer_library.php) This library could look like Imho最好创建一个处理PHPMailer对象的库(Phpmailer_library.php),该库看起来像

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. Using this library in one of your controllers, models etc. 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 ;) 如果您有任何麻烦,请随时询问;)


Update 25.06.2018 更新25.06.2018

Since the PHPMailer guys removed the autoloader you've two options now: 自从PHPMailer伙计们删除了自动加载器后,您现在有了两个选择:

1.) via Composer 1.)通过Composer

for those who didn't know - Codeigniter supports Composer - you simply have to activate the autoload - you can find this in your config.php 对于那些不知道的人-Codeigniter支持Composer-您只需激活自动加载-您可以在config.php中找到它

$config['composer_autoload'] = true;

For more informations take a look here 欲了解更多信息,请看这里

After that - run composer like 之后-像这样运行作曲家

composer require phpmailer/phpmailer

You now should have within your application/vendor folder the phpmailer files. 您现在应该在application/vendor文件夹中包含phpmailer文件。

The library should look like 图书馆应该看起来像

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

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

2.) download 2.)下载

follow step 1 遵循步骤1

The library should look like 图书馆应该看起来像

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 其他都应该保持不变

有Codeigniter 2/3的php邮件库,您可以检查thisthis

** **

UPDATE AUGUST 2019 2019年8月更新

** **

At first, download the latest PHPMailer library files and place all the files in the application/third_party/ folder of your CodeIgniter application. 首先,下载最新的PHPMailer库文件 ,并将所有文件放在CodeIgniter应用程序的application / third_party /文件夹中。

Now, create a library (application/libraries/Phpmailer_lib.php) to handle the PHPMailer object. 现在,创建一个库(application / libraries / Phpmailer_lib.php)来处理PHPMailer对象。

  • Include the PHPMailer library files. 包括PHPMailer库文件。
  • Initialize the PHPMailer class. 初始化PHPMailer类。
  • Return the PHPMailer object. 返回PHPMailer对象。

     use PHPMailer\\PHPMailer\\PHPMailer; use PHPMailer\\PHPMailer\\Exception; class PHPMailer_Lib { public function __construct(){ log_message('Debug', 'PHPMailer class is loaded.'); } public function load(){ // Include PHPMailer library files require_once APPPATH.'third_party/phpmailer/Exception.php'; require_once APPPATH.'third_party/phpmailer/PHPMailer.php'; require_once APPPATH.'third_party/phpmailer/SMTP.php'; $mail = new PHPMailer(true); return $mail; } } 

Now send email via SMTP server using PHPMailer from your controller using this code. 现在,使用此代码从您的控制器使用PHPMailer通过SMTP服务器发送电子邮件。

class Email extends CI_Controller{

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

    function send(){
        // Load PHPMailer library
        $this->load->library('phpmailer_lib');

        // PHPMailer object
        $mail = $this->phpmailer_lib->load();

        // SMTP configuration
        $mail->isSMTP();
        $mail->Host     = 'smtp.example.com';
        $mail->SMTPAuth = true;
        $mail->Username = 'user@example.com';
        $mail->Password = '********';
        $mail->SMTPSecure = 'ssl';
        $mail->Port     = 465;

        $mail->setFrom('info@example.com', 'CodexWorld');
        $mail->addReplyTo('info@example.com', 'CodexWorld');

        // Add a recipient
        $mail->addAddress('john.doe@gmail.com');

        // Add cc or bcc 
        $mail->addCC('cc@example.com');
        $mail->addBCC('bcc@example.com');

        // Email subject
        $mail->Subject = 'Send Email via SMTP using PHPMailer in CodeIgniter';

        // Set email format to HTML
        $mail->isHTML(true);

        // Email body content
        $mailContent = "<h1>Send HTML Email using SMTP in CodeIgniter</h1>
            <p>This is a test email sending using SMTP mail server with PHPMailer.</p>";
        $mail->Body = $mailContent;

        // Send email
        if(!$mail->send()){
            echo 'Message could not be sent.';
            echo 'Mailer Error: ' . $mail->ErrorInfo;
        }else{
            echo 'Message has been sent';
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM