php 如何将 PHPMailer 与 Codeigniter 3 集成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44843305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:03:04  来源:igfitidea点击:

How to integrate PHPMailer with Codeigniter 3

phpcodeigniterphpmailer

提问by Rajan

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

嗨,我正在尝试 在我的 Codeigniter 应用程序中使用来自 GitHub 的PHPMailer 库

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

我下载了代码并解压到我的application\library文件夹中。所以我有一个名为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 = '[email protected]';                 // 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 中加载或集成这个库。

回答by sintakonte

here is a guide

这是指南

1. installing PHP Mailer

1.安装PHP Mailer

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

从 Github 下载最新的 PHPMailer Build。你可以在这里找到这个项目

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

现在单击“克隆或下载”并将其下载为 zip - 如下图所示。 PHP 邮件程序下载为 ZIP

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 enter image description here

zip 中的文件夹称为 PHPMailer-master。将其解压缩到您的 application/third_party/ 文件夹中,并将该文件夹重命名为 phpmailer。你应该看到这样的东西 在此处输入图片说明

2. PHP Mailer Library

2. PHP 邮件程序库

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

恕我直言,最好创建一个处理您的 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

2018 年 6 月 25 日更新

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

由于 PHPMailer 人员删除了自动加载器,您现在有两个选择:

1.) via Composer

1.) 通过作曲家

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/vendorfolder the phpmailerfiles.

您现在应该在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

其他一切都应该保持不变

回答by stevebaros

**

**

UPDATE AUGUST 2019

2019 年 8 月更新

**

**

At first, download the latest PHPMailer library filesand 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.
  • Initialize the PHPMailer class.
  • Return the PHPMailer object.

    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;
    }
    }
    
  • 包括 PHPMailer 库文件。
  • 初始化 PHPMailer 类。
  • 返回 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 = '[email protected]';
        $mail->Password = '********';
        $mail->SMTPSecure = 'ssl';
        $mail->Port     = 465;

        $mail->setFrom('[email protected]', 'CodexWorld');
        $mail->addReplyTo('[email protected]', 'CodexWorld');

        // Add a recipient
        $mail->addAddress('[email protected]');

        // Add cc or bcc 
        $mail->addCC('[email protected]');
        $mail->addBCC('[email protected]');

        // 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';
        }
    }

}

回答by MorganFreeFarm

There is php mailer library for Codeigniter 2/3, you can check thisand this.

Codeigniter 2/3 有 php mailer library,你可以检查这个这个