如何将PHPMailer与Codeigniter 3集成

mhd8tkvw  于 11个月前  发布在  PHP
关注(0)|答案(4)|浏览(141)

嗨,我正在尝试在我的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 = '[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;
    }
}

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

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


我得到这个错误:
遇到错误
无法加载模块:Public
所以请指导我如何在Codeigniter中加载或集成这个库。

pu82cl6c

pu82cl6c1#

这里有一个指南

1.安装PHP Mailer

从Github下载最新的PHPMailer Build。你可以找到项目here
现在点击“克隆或下载”,并下载它作为zip -如下图所示。x1c 0d1x
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();
    }
}


我想这应该差不多就可以了。如果你有任何麻烦,不要犹豫问;)

更新25.06.2018

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

1.)通过Composer

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

$config['composer_autoload'] = true;


更多信息请看here
在那之后-运行 composer 喜欢

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


其他一切都应该保持不变

iswrvxsc

iswrvxsc2#

**

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 = '[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';
        }
    }

}

kdfy810k

kdfy810k3#

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

8zzbczxx

8zzbczxx4#

把它放在助手身上怎么样?
首先,你需要有 composer 安装在您的本地。如果没有然后谷歌如何安装它。
然后通过打开cmd使用composer获取phpmailer,然后为PHPMailer文件创建一个文件夹。
下面是我的代码:
1.将phpmailer文件夹中“vendor”文件夹中的所有文件复制到“/system/application/vendor/”文件夹中的Web应用程序中
1.在“/system/application/helper/”中打开一个帮助文件(或创建新的帮助文件)
1.使用此代码

function sendemail($to,$to_name,$subject,$message){ 
    require_once APPPATH.'vendor/phpmailer/phpmailer/src/Exception.php';
    require_once APPPATH.'vendor/phpmailer/phpmailer/src/PHPMailer.php';
    require_once APPPATH.'vendor/phpmailer/phpmailer/src/SMTP.php';

    //Make instance of PHPMailer object
    $mail = new PHPMailer\PHPMailer\PHPMailer();  

    // SMTP configuration
    $mail->isSMTP();
    $mail->Host     = 'mail.yoursite.com';
    $mail->SMTPAuth = true;
    $mail->Username = '[email protected]';
    $mail->Password = 'yourpassword';
    $mail->SMTPSecure = 'ssl';  
    $mail->Port     = 465;

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

    // Add a recipient
    $mail->addAddress($to); //,$to_name

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

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

    // Email subject
    $mail->Subject = $subject; 

    // Email body content
    $mailContent = $message."<br/><br/>
        <p>Auto notification *** <br/>DO NOT REPLY TO THIS ADDRESS as it is not attended.</p>";
    $mail->Body = $mailContent;

    // Send email
    if(!$mail->send()){
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo; die;
    }else{
       //do none or celebrate may be???
    }
}

字符串
1.要调用该方法,您需要加载helper,然后像下面这样调用该方法:

$this->load->helper('yourhelper');
sendemail("[email protected]","to name","Subject","Email content ( can use html)");

相关问题