Skip to content

Instantly share code, notes, and snippets.

@enapupe
Last active December 23, 2015 10:59
Show Gist options
  • Select an option

  • Save enapupe/6625082 to your computer and use it in GitHub Desktop.

Select an option

Save enapupe/6625082 to your computer and use it in GitHub Desktop.
CREATE TABLE IF NOT EXISTS `email_queue` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`recipients` text NOT NULL,
`final_body` text,
`headers` text,
`send_after` datetime DEFAULT NULL,
`subject` varchar(255) DEFAULT NULL,
`priority` tinyint(4) NOT NULL DEFAULT '3',
`ip_address` varchar(16) DEFAULT NULL,
`spam_status` tinyint(4) NOT NULL DEFAULT '0',
`sent_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=19 ;
<?php
//sending e-mail (adding to queue)
$this->load->library('email');
$this->email->initialize();
$this->email->from('john@doe');
$this->email->set_protocol("queue");
$this->email->send_at("+1 day");//strtotime format
$this->email->to('foo@bar');
$this->email->subject('subject');
$this->email->message('msg');
$queue_id = $this->email->send();
//fetching queue
$this->load->library('email');
$this->email->initialize();
if($this->email->setup_email_from_queue()) // or $this->email->setup_email_from_queue($queue_id);
var_dump($this->email->send()); //bool
<?php
/**
* CodeIgniter
*
* An open source application development framework for PHP 5.2.4 or newer
*
* NOTICE OF LICENSE
*
* Licensed under the Open Software License version 3.0
*
* This source file is subject to the Open Software License (OSL 3.0) that is
* bundled with this package in the files license.txt / license.rst. It is
* also available through the world wide web at this URL:
* http://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to obtain it
* through the world wide web, please send an email to
* licensing@ellislab.com so we can send you a copy immediately.
*
* @package CodeIgniter
* @author EllisLab Dev Team
* @copyright Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/)
* @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* @link http://codeigniter.com
* @since Version 1.0
* @filesource
*/
defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Email extends CI_Email {
/**
* Valid $protocol values
*
* @see CI_Email::$protocol
* @var string[]
*/
protected $_send_after = null;
protected $_protocols = array('mail', 'sendmail', 'smtp', 'queue');
protected $_queue_table = 'email_queue';
protected $_queue_id = null;
/**
* Set Email Queue Table name
*
* @param string
* @return MY_Email
*/
public function set_queue_table($table) {
$CI =& get_instance();
if ($CI->db->table_exists($table)) {
$this->_table = $table;
}
$this->_set_error_message('lang:email_queue_table_not_exists', $table);
return $this;
}
/**
* Set Email Subject
*
* @param string
* @return MY_Email
*/
public function subject($subject) {
$subject = $this->_prep_q_encoding($subject);
$this->set_header('Subject', $subject);
$this->_subject = $subject;
return $this;
}
/**
* Schedule Email sending
*
* @param string/datetime
* @return MY_Email
*/
public function send_after($datetime) {
$time = strtotime($datetime);
if (!$time || $time === -1) {
$this->_set_error_message('lang:email_invalid_strtotime_string_for_send_after', $datetime);
return false;
}
$this->_send_after = strftime("%Y-%m-%d %H:%M:%S", $time);
return $this;
}
/**
* Send using queue table() protocol
*
* @return insert_id;
*/
protected function _send_with_queue() {
$this->_recipients = serialize($this->_recipients);
$CI =& get_instance();
if ($CI->db->insert($this->_queue_table, array('recipients' => $this->_recipients, 'subject' => $this->_subject,
'final_body' => $this->_finalbody, 'headers' => serialize($this->_headers),
'priority' => $this->priority, 'ip_address' => $CI->input->ip_address(),
'send_after' => $this->_send_after, 'body_hash' => hash("sha256", $this->_finalbody))))
return $CI->db->insert_id();
}
/**
* Fetches next queue email from email queue table based upon priority, send_after
* TODO: check send_after, spam possibility, etc
*
* @return E-mail data;
*/
protected function _fetch_next_email_from_queue() {
$CI =& get_instance();
$CI->db->where('sent_at', null);
$CI->db->order_by('created_at', 'ASC');
$CI->db->limit(1);
$email = $CI->db->get($this->_queue_table);
if ($email->num_rows())
return $email->row();
}
/**
* Fetches queued email based on its ID, else fetches next email
*
* @return E-mail data;
*/
protected function _fetch_email_from_queue($queue_id) {
$CI =& get_instance();
$queue_id = (int) $queue_id;
if ($queue_id) {
$CI->db->where("id", $queue_id);
$email = $CI->db->get($this->_queue_table);
if ($email->num_rows())
return $email->row();
} else {
return $this->_fetch_next_email_from_queue();
}
}
/**
* Sets up e-mail using queue table()
*
* @return MY_Email;
*/
public function setup_email_from_queue($queue_id = false) {
$this->clear();
$email_data = $this->_fetch_email_from_queue($queue_id);
if ($email_data) {
$this->_queue_id = $email_data->id;
$this->_headers = unserialize($email_data->headers);
$this->_final_body = $email_data->final_body;
$this->_recipients = unserialize($email_data->recipients);
$this->_subject = $email_data->subject;
return true;
}
}
public function clear($clear_attachments = false) {
$this->_send_after = null;
$this->_queue_id = null;
parent::clear();
}
/**
* Spool mail to the mail server
*
* @return bool
*/
protected function _spool_email() {
$this->_unwrap_specials();
$method = '_send_with_' . $this->_get_protocol();
if (!$result = $this->$method()) {
$this->_set_error_message('lang:email_send_failure_' . ($this->_get_protocol() === 'mail' ? 'phpmail' : $this->_get_protocol()));
return FALSE;
}
//update sent_at now() on queue table
if ($this->_queue_id) {
$CI =& get_instance();
$CI->db->where("id", $this->_queue_id);
$CI->db->update($this->_queue_table, array('sent_at' => strftime("%Y-%m-%d %H:%M:%S", time())));
}
$this->_set_error_message('lang:email_sent', $this->_get_protocol());
return $result;
}
}
/* End of file MY_Email.php */
/* Location: ./application/libraries/MY_Email.php */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment