如何将 PHPMailer 与 Codeigniter 3 集成 [英] How to integrate PHPMailer with Codeigniter 3
问题描述
我正在尝试在我的 Codeigniter 应用程序中使用来自 GitHUB 的
zip 中的文件夹名为 PHPMailer-master.将其解压缩到您的 application/third_party/文件夹中,并将该文件夹重命名为 phpmailer.你应该看到这样的
2.PHP 邮件程序库
恕我直言,最好创建一个处理 PHPMailer 对象的库 (Phpmailer_library.php)这个库可能看起来像
class Phpmailer_library{公共函数 __construct(){log_message('Debug', 'PHPMailer 类已加载.');}公共函数加载(){require_once(APPPATH."third_party/phpmailer/PHPMailerAutoload.php");$objMail = 新的 PHPMailer;返回 $objMail;}}
3.在您的控制器、模型等之一中使用此库
class Welcome 扩展 CI_Controller {公共函数索引(){$this->load->library("phpmailer_library");$objMail = $this->phpmailer_library->load();}}
我认为这应该可以完成这项工作.如果你有任何问题,不要犹豫,问;)
<小时>25.06.2018 更新
自从 PHPMailer 人删除了自动加载器,您现在有两个选择:
1.) 通过 Composer
对于那些不知道的人 - Codeigniter 支持 Composer - 你只需要激活自动加载 - 你可以在你的 config.php 中找到它
$config['composer_autoload'] = true;
有关详细信息,请查看此处
在那之后 - 像这样运行作曲家
composer 需要 phpmailer/phpmailer
您现在应该在 application/vendor
文件夹中拥有 phpmailer
文件.
图书馆应该看起来像
class Phpmailer_library{公共函数 __construct(){log_message('Debug', 'PHPMailer 类已加载.');}公共函数加载(){$objMail = new PHPMailerPHPMailerPHPMailer();返回 $objMail;}}
2.) 下载
按照步骤 1
图书馆应该看起来像
class Phpmailer_library{公共函数 __construct(){log_message('Debug', 'PHPMailer 类已加载.');}公共函数加载(){require_once(APPPATH.'third_party/phpmailer/src/PHPMailer.php');require_once(APPPATH.'third_party/phpmailer/src/SMTP.php');$objMail = new PHPMailerPHPMailerPHPMailer();返回 $objMail;}}
其他一切都应该保持不变
Hi I am trying to use PHPMailer Library from GitHUB in my Codeigniter application.
I downloaded the code and unzipped in my applicationlibrary
folder.
So there I have a folder called vendor inside which resides the source code for PHPMailer.
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 "vendorphpmailerphpmailerPHPMailerAutoload";
$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
So Please guide me how I can load or integrate this library in Codeigniter.
here is a guide
1. installing PHP Mailer
Download the latest PHPMailer Build from Github. You can find the project here
Click now on "clone or download" and download it as zip - as in the image below is shown.
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
2. PHP Mailer Library
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. Using this library in one of your controllers, models etc.
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
Since the PHPMailer guys removed the autoloader you've two options now:
1.) via 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
$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.
The library should look like
class Phpmailer_library
{
public function __construct()
{
log_message('Debug', 'PHPMailer class is loaded.');
}
public function load()
{
$objMail = new PHPMailerPHPMailerPHPMailer();
return $objMail;
}
}
2.) download
follow step 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 PHPMailerPHPMailerPHPMailer();
return $objMail;
}
}
and everything else should remain the same
这篇关于如何将 PHPMailer 与 Codeigniter 3 集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!