Last active
October 16, 2020 14:59
-
-
Save portapipe/c969aba518d78bfe37faa0287f172df2 to your computer and use it in GitHub Desktop.
PHPMailer Library for Codeigniter - Keep the native email structure!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* PHPMailer for Codeigniter - Keep the native email structure! | |
* | |
* @package CodeIgniter | |
* @subpackage Libraries | |
* @category Libraries | |
* @author https://github.com/portapipe | |
* | |
* @version 1.0 | |
*/ | |
use PHPMailer\PHPMailer\PHPMailer; | |
use PHPMailer\PHPMailer\Exception; | |
require_once(APPPATH."third_party/PHPMailer/src/Exception.php"); | |
require_once(APPPATH."third_party/PHPMailer/src/PHPMailer.php"); | |
require_once(APPPATH."third_party/PHPMailer/src/SMTP.php"); | |
class PHPMail | |
{ | |
public $config_file = "email"; //Config file to be used as configuration | |
public $config_name = "email"; //Config name to be used in the config file | |
private $debug = 2; //Enable verbose debug output | |
public $smtp_port = 25; //Default SMTP port | |
public $smtp_crypto = 'ssl'; | |
public $mailtype = 'html'; | |
function __construct($config = array()) { | |
$this->CI =& get_instance(); | |
$this->CI->load->config($this->config_file); | |
$this->config = $this->CI->config->item($this->config_name); | |
foreach($this->config as $k=>$v){ | |
$this->{$k} = $v; | |
} | |
$this->initialize($config); | |
} | |
public function load() | |
{ | |
$objMail = new \PHPMailer\PHPMailer\PHPMailer; | |
return $objMail; | |
} | |
public function initialize($config=false){ | |
if($config!=false){ | |
foreach($config as $k=>$v){ | |
$this->{$k} = $v; | |
} | |
} | |
$mail = $this->load(); | |
//Server settings | |
if($this->protocol=="smtp"){ // Set mailer to use SMTP | |
$mail->SMTPDebug = $this->debug; | |
$mail->isSMTP(); | |
$mail->SMTPAuth = true; // Enable SMTP authentication | |
$mail->Host = $this->smtp_host; // Specify main and backup SMTP servers | |
$mail->Username = $this->smtp_user; // SMTP username | |
$mail->Password = $this->smtp_pass; // SMTP password | |
$mail->SMTPSecure = $this->smtp_crypto; // Enable TLS encryption, `ssl` also accepted | |
$mail->Port = $this->smtp_port; // TCP port to connect to | |
} | |
$this->mail = $mail; | |
return $this; | |
}public function init(){return $this->initialize();} | |
public function from($email,$name=""){ | |
$this->from = [$email,$name]; | |
return $this; | |
} | |
public function reply_to($email,$name=""){ | |
$this->reply_to = [$email,$name]; | |
return $this; | |
} | |
public function to($email){ | |
$this->to = $email; | |
return $this; | |
} | |
public function cc($email){ | |
$this->cc = $email; | |
return $this; | |
} | |
public function bcc($email){ | |
$this->bcc = $email; | |
return $this; | |
} | |
public function subject($subject){ | |
$this->subject = $subject; | |
return $this; | |
} | |
public function message($message){ | |
$this->message = $message; | |
return $this; | |
} | |
public function set_alt_message($message){ | |
$this->set_alt_message = $message; | |
return $this; | |
} | |
public function set_header($header,$value){ | |
$this->set_header[] = [$header=>$value]; | |
return $this; | |
} | |
public function clear($clear_attachments = FALSE){ | |
$this->clear = $clear_attachments; | |
return $this; | |
} | |
public function attach($filename){ | |
$this->attachments[] = $filename; | |
return $this; | |
} | |
public function print_debugger($include=[]){ | |
$this->debugger = $include; | |
// return $this; | |
} | |
//Replace USERNAME, PASSWORD, and other variables accordingly | |
//$receiver can be a string or an array of [%name% => %email% , ...] | |
public function send($auto_clear=TRUE){ | |
$mail = $this->mail; | |
try { | |
//Recipients | |
$mail->setFrom($this->from[0],$this->from[1]); | |
if(is_string($this->to)) | |
$mail->addAddress($this->to); // Add a recipient | |
else{ | |
foreach($this->to as $k=>$v){ | |
if(is_numeric($k)) | |
$mail->addAddress($v); // Add a recipient | |
else | |
$mail->addAddress($v,$k); // Add a recipient | |
} | |
} | |
if(isset($this->reply_to)) // Name is optional | |
$mail->addReplyTo($this->reply_to[0], $this->reply_to[1]); | |
if(isset($this->cc)) | |
$mail->addCC($this->cc); | |
if(isset($this->bcc)) | |
$mail->addBCC($this->bcc); | |
//Attachments | |
if(isset($this->attachments)){ | |
foreach($this->attachments as $file){ | |
$mail->addAttachment($file); // Add attachments | |
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name | |
} | |
} | |
//Content | |
if(strtolower($this->mailtype)=="html") | |
$mail->isHTML(true); // Set email format to HTML | |
$mail->Subject = $this->subject; | |
$mail->Body = $this->message; | |
$mail->AltBody = (isset($this->set_alt_message)?$this->set_alt_message:strip_tags($this->message)); | |
$mail->SMTPAutoTLS = false; | |
// echo '<p><b>Linea '.__LINE__.'</b></p><pre>';print_r($mail);echo '</pre><p><i>File: '.__FILE__.'</i></p>';die; | |
$mail->send(); | |
echo 'Message has been sent'; | |
return true; | |
} catch (Exception $e) { | |
log_message("error","[".__CLASS__."] L".__LINE__." ".__FUNCTION__."(".implode(",",func_get_args()).") - PHPMailer error: ".$mail->ErrorInfo); | |
echo 'Message could not be sent.'; | |
echo 'Mailer Error: ' . $mail->ErrorInfo; | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment