【问题标题】:How to integrate PHPMailer with Codeigniter 3如何将 PHPMailer 与 Codeigniter 3 集成
【发布时间】:2017-12-04 05:27:43
【问题描述】:

您好,我正在尝试在我的 Codeigniter 应用程序中使用来自 Github 的 PHPMailer Library

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

现在我创建了一个名为 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;
    }
}

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

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

我得到这个错误:

遇到错误

无法加载请求的类:Bizmailer

所以请指导我如何在 Codeigniter 中加载或集成这个库。

【问题讨论】:

  • 只是出于好奇-您是否有理由要使用 phpmailer 而不是 codeigniter 的标准邮件库?
  • @sintakonte 是的,实际上这允许我为我在 Codeigniter 的库中找不到的所有不同电子邮件帐户设置别名 $mail-&gt;setFrom('from@example.com', 'Mailer');
  • @wolfgang1983 我已经尝试过 Codeigniter 原生库,它工作正常,但是因为我有需要为我的电子邮件设置别名的地方。就像我的实际电子邮件是 rajan@example.com 但我希望他们显示从 info@example.com 收到的邮件

标签: php codeigniter phpmailer


【解决方案1】:

这是一个指南

1。安装 PHP Mailer

从 Github 下载最新的 PHPMailer Build。 可以找到项目here

现在单击“克隆或下载”并将其下载为 zip - 如下图所示。

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

2。 PHP 邮件程序库

恕我直言,最好创建一个处理您的 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。在您的控制器、模型等之一中使用此库。

class Welcome extends CI_Controller {


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

我认为这应该可以完成这项工作。 如果您有任何问题,请不要犹豫;)


2018 年 6 月 25 日更新

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

1.) 通过作曲家

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

$config['composer_autoload'] = true;

更多信息请看here

之后 - 像这样运行作曲家

composer require phpmailer/phpmailer

您现在应该在您的application/vendor 文件夹中拥有phpmailer 文件。

库应该是这样的

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

其他一切都应该保持不变

【讨论】:

  • 这是完美的,只有一个问题,我可以在 PHPMailer_Library.php 构造函数中声明我的全局设置吗?通过其他控制器,我可以使用这些吗?
  • 当然你可以——尽管你必须知道——如果你想要其他设置,你需要在你的库中添加一个新函数;)
  • 嗨,很抱歉这么晚才回复,但我收到错误无法加载类 PHPMailer_Library。这是为什么呢?
  • autoloader 现在从 PHPMailer 中消失了:stackoverflow.com/a/49121147/1246870
  • @JianYA Phpmailer_library.php 必须位于库文件夹中。
【解决方案2】:

**

2019 年 8 月更新

**

首先,下载最新的PHPMailer library files,并将所有文件放在你的CodeIgniter应用程序的application/third_party/文件夹中。

现在,创建一个库 (application/libraries/Phpmailer_lib.php) 来处理 PHPMailer 对象。

  • 包括 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;
    }
    }
    

现在使用此代码从您的控制器使用 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';
        }
    }

}

【讨论】:

  • 嘿,我如何调用控制器发送电子邮件?我试过了,但不知道。
【解决方案3】:

Codeigniter 2/3 有 php mailer 库,你可以查看thisthis

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-26
    • 2011-06-04
    • 1970-01-01
    • 1970-01-01
    • 2012-03-07
    • 2010-11-18
    • 1970-01-01
    相关资源
    最近更新 更多