Skip to content

Instantly share code, notes, and snippets.

@shadowhand
Created August 29, 2010 20:04
Show Gist options
  • Save shadowhand/556634 to your computer and use it in GitHub Desktop.
Save shadowhand/556634 to your computer and use it in GitHub Desktop.
<?php defined('SYSPATH') or die('No direct script access.');
/**
* User to user communication wrapper. Uses Swift Mailer for email transport.
*
* @package Communicate
* @category Base
* @author Woody Gilk <woody@wingsc.com>
* @copyright (c) 2010 Woody Gilk
* @license MIT
*/
class Communicate {
protected static $mailer;
public static function mailer()
{
if ( ! static::$mailer)
{
// Use sendmail to deliver all messages
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');
// Create a new mailer instance
static::$mailer = Swift_Mailer::newInstance($transport);
}
return static::$mailer;
}
public static function factory(Model_User $to, Model_User $from, $template = NULL)
{
$message = new Communicate($to, $from);
if ($template)
{
$message->template($template);
}
return $message;
}
/**
* @var Model_User recipient
*/
protected $__to;
/**
* @var Model_User sender
*/
protected $__from;
/**
* @var string template to render the message with
*/
protected $__template;
/**
* @var array variables to render the template with
*/
protected $__variables = array();
public function __construct(Model_User $to, Model_User $from)
{
// To:
$this->__to = $to;
// From:
$this->__from = $from;
}
public function template($template = NULL)
{
if ($template === FALSE)
{
$this->__template = NULL;
}
elseif ($template)
{
$this->__template = $template;
}
return $this;
}
public function set($key, $value)
{
$this->__variables[$key] = $value;
return $this;
}
public function render($data, $type = 'email')
{
$template = Walrus::simple('layout/email', array(
'message' => "communication/{$type}/{$this->__template}",
))
->set('data', $data->as_array());
foreach ($this->__variables as $key => $value)
{
$template->set($key, $value);
}
return $template->render();
}
public function email($subject, $message, $is_html = FALSE)
{
$data = Validate::factory(array(
'to_name' => $this->__to->full_name,
'to_email' => $this->__to->email,
'from_name' => $this->__from->full_name,
'from_email' => $this->__from->email,
'subject' => $subject,
'message' => $message,
))
->filter(TRUE, 'trim')
->rules('to_name', array(
'regex' => array('/^[\pL .,]++$/u'),
))
->rules('to_email', array(
'not_empty' => NULL,
'email' => NULL,
))
->rules('from_name', array(
'regex' => array('/^[\pL .,]++$/u'),
))
->rules('from_email', array(
'not_empty' => NULL,
'email' => NULL,
))
->filter('subject', 'Security::xss_clean')
->filter('subject', 'strip_tags')
->rule('subject', 'not_empty')
->filter('message', 'Security::xss_clean')
->rule('message', 'not_empty')
;
if ( ! $is_html)
{
// Message is not supposed to be added as HTML
$data->filter('message', 'strip_tags');
}
if ( ! $data->check())
{
throw new Kohana_Validate_Exception($data);
}
if ( ! $is_html)
{
// Convert the message into simple HTML
$data['message'] = Text::auto_p(Text::auto_link($data['message']));
}
if ($this->__template)
{
// Render the message using the template
$data['message'] = $this->render($data);
}
// Get the email body in both HTML and plaintext format
$body_html = $data['message'];
$body_text = trim(strip_tags($data['message']));
// Clean up body whitespace
$body_text = str_replace("\t", '', $body_text);
$body_text = preg_replace('/\n{2,}/m', "\n\n", $body_text);
// Create a log of this message
$log = Hive::factory('user_message')
->compose('email', $this->__to, $this->__from, $subject, $body_html)
;
// To: and From:
$to = array($data['to_email'] => $data['to_name']);
$from = array($data['from_email'] => $data['from_name']);
$message = Swift_Message::newInstance($subject)
->setTo($to)
->setFrom($from)
->setBody($body_html, 'text/html')
->addPart($body_text, 'text/plain');
if ($status = static::mailer()->send($message))
{
// Create the log
$log->create();
}
return $status;
}
} // End Communicate
// Load SwiftMailer
require Kohana::find_file('vendor', 'swiftmailer/lib/swift_required');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment