Skip to content

Instantly share code, notes, and snippets.

@aktuba aktuba/gist:7541241
Created Nov 19, 2013

Embed
What would you like to do?
Mail library for phalcon
<?php
namespace Phalcon\Mail;
class Mail extends \Phalcon\Mvc\User\Component
{
/**
* @var array
*/
protected $_config;
/**
* @var \Swift
*/
protected $_swift;
public function __construct(array $config = NULL)
{
if (empty($config))
{
throw new \Phalcon\Exception('Config for mailer not found', 500);
}
$this->_config = $config;
$config = !empty($this->_config['options']) ? $this->_config['options'] : array();
if (!class_exists('\Swift', FALSE))
{
if (!empty($this->_config['swift']))
{
require $this->_config['swift'].'/swift_required.php';
}
else
{
throw new \Phalcon\Exception('Swift library not found', 500);
}
}
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, $err);
}
public function sendView($to, $subject, $view, array $data = NULL)
{
list($controller, $action) = explode('/', $view);
$path = $this->view->getViewsDir();
$content = $this->view->getRender($controller, $action, $data, function ($view) use ($path)
{
$view->setViewsDir($path);
$view->setRenderLevel(\Phalcon\Mvc\View::LEVEL_ACTION_VIEW);
});
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.