Skip to content

Instantly share code, notes, and snippets.

@aktuba aktuba/gist:7503437
Created Nov 16, 2013

Embed
What would you like to do?
<?php
namespace Library;
class Mail
{
/**
* @var \Phalcon\DiInterface
*/
protected $_di;
/**
* @var array
*/
protected $_config;
/**
* @var \Swift
*/
protected $_swift;
public function __construct(array $config = null)
{
if (!class_exists('\Swift', false))
{
require ROOT_PATH.'/vendors/Swift-5.0.1/lib/swift_required.php';
}
$this->_di = \Phalcon\DI::getDefault();
$_config = $this->_di->getShared('config')->email;
foreach ($_config as $key => $value)
{
$this->_config[$key] = $value;
}
if (!empty($config))
{
$this->_config = array_merge($this->_config, $config);
}
$config = !empty($this->_config['options']) ? $this->_config['options'] : array();
switch ($this->_config['transport'])
{
case 'smtp':
$transport = \Swift_SmtpTransport::newInstance(
$config['host'],
!empty($config['port']) ? (int) $config['port'] : 25,
!empty($config['encryption']) ? $config['encryption'] : null
);
// Do authentication, if part of the DSN
empty($config['username']) or $transport->setUsername($config['username']);
empty($config['password']) or $transport->setPassword($config['password']);
// Set the timeout to 5 seconds
$transport->setTimeout(empty($config['timeout']) ? 5 : (int) $config['timeout']);
break;
case 'sendmail':
// Create a sendmail connection
$transport = \Swift_SendmailTransport::newInstance(empty($config) ? '/usr/sbin/sendmail -bs' : $config);
break;
default:
// Use the native connection
$transport = \Swift_MailTransport::newInstance($config);
break;
}
$this->_swift = \Swift_Mailer::newInstance($transport);
}
public function send($to, $subject, $message, $html = false)
{
$html = ($html === true) ? 'text/html' : 'text/plain';
// Create the message
$message = \Swift_Message::newInstance($subject, $message, $html, 'utf-8');
if (is_string($to))
{
$message->setTo($to);
}
elseif (is_array($to))
{
if (isset($to[0]) AND isset($to[1]))
{
$to = array('to' => $to);
}
foreach ($to as $method => $set)
{
if ( ! in_array($method, array('to', 'cc', 'bcc'), true))
{
$method = 'to';
}
$method = 'add'.ucfirst($method);
if (is_array($set))
{
// Add a recipient with name
$message->$method($set[0], $set[1]);
}
else
{
// Add a recipient without name
$message->$method($set);
}
}
}
if (!empty($this->_config['from']))
{
if (is_string($this->_config['from']))
{
// From without a name
$message->setFrom($this->_config['from']);
}
elseif (is_array($this->_config['from']))
{
// From with a name
$message->setFrom($this->_config['from'][0], $this->_config['from'][1]);
}
}
return $this->_swift->send($message);
}
public function sendView($to, $subject, $view, array $data = null)
{
list($controller, $action) = explode('/', $view);
$view = new \Phalcon\Mvc\View();
$view->registerEngines(array(
'.php' => 'Phalcon\Mvc\View\Engine\Php',
));
$view->setDI($this->_di);
$view->setViewsDir($this->_di->get('view')->getViewsDir());
$view->start();
$view->render($controller, $action, $data);
$view->finish();
$content = $view->getContent();
return $this->send($to, $subject, $content, true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.