<?php
// +---------------------------------------------------------------------------+
// | This file is part of the %%PROJECT_NAME%% project. |
// | Copyright (C) Lemieux Bedard Communication |
// | |
// | For the full copyright and license information, please view the LICENSE |
// | file that was distributed with this source code. |
// +---------------------------------------------------------------------------+
/**
* LemieuxMail is a class which used to make the process of sending mails using
* the eazycomponents mail class easier. This class use templates to create
* the mail content.
* @package core
* @subpackage mail
* @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
* @copyright Lemieux Bedard Communication (jean-philippe.dery@lemieuxbedard.com)
* @since 1.0.0
* @version 1.0.0
*/
class LemieuxMail extends AgaviAttributeHolder
{
/**
* @var object The agavi context.
*/
protected $context = null;
/**
* @var object The template parser object.
*/
protected $template = null;
/**
* @var object The swift email sender object.
*/
protected $swift = null;
/**
* @var string The receiver.
*/
protected $to = null;
/**
* @var string The sender.
*/
protected $from = null;
/**
* @var string The subject.
*/
protected $subject = null;
/**
* @var string CC and BCC.
*/
protected $cc, $bcc = null;
/**
* Constructor. Set the template object which render the message to be send
* to the user. Since the email is sent using ezcomponents mail classes, the
* content type of the teplate determine wheteher or not the nature of the
* mail composer object.
* @param object The agavi context.
* @param string The template name.
* @param string The receiver email address.
* @param string The receiver email name.
* @return void
* @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
* @since 1.0.0
*/
function __construct(AgaviContext $context, $template, $subject, $toName, $toEmail, $fromName, $fromEmail)
{
$this->context = $context;
$this->template = $template;
$this->subject = $subject;
$this->setFrom($fromName, $fromEmail);
$this->setTo($toName, $toEmail);
$this->swift = new Swift($this->getSwiftConnection());
}
/**
* Set the receiver email address. This method accepts only email address
* without the receiver's name. Please note that this parameter may contains
* agavi config variables.
* @param string The email address.
* @return void
* @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
* @since 1.0.0
*/
public function setTo($name, $address)
{
$this->to = new LemieuxMailAddress($address, $name);
}
/**
* Set the receiver email address. This method accepts only email address
* without the receiver's name. Please note that this parameter may contains
* agavi config variables.
* @param string The email address.
* @param string The encoding.
* @return void.
* @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
* @since 1.0.0
*/
public function setCC($name, $address)
{
$this->cc = new LemieuxMailAddress($address, $name);
}
/**
* Clear the CC value.
* @return void.
* @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
* @since 1.0.0
*/
public function clearCC()
{
$this->cc = null;
}
/**
* Set the receiver email address. This method accepts only email address
* without the receiver's name. Please note that this parameter may contains
* agavi config variables.
* @param string The email address.
* @param string The email name.
* @param string The encoding.
* @return void.
* @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
* @since 1.0.0
*/
public function setFrom($name, $address)
{
$this->from = new LemieuxMailAddress($address, $name);
}
/**
* Set the subject of the mail address. Please note that this parameter
* may contains agavi config variables.
* @param string The subject.
* @return void.
* @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
* @since 1.0.0
*/
public function setSubject($subject)
{
$this->subject = $subject;
}
/**
* Return the swift connection object.
* @return object The swift connection object.
* @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
* @since 1.0.0
*/
protected function getSwiftConnection()
{
return AgaviConfig::get('core.debug') ? return new Swift_Connection_Sendmail() : return new Swift_Connection_SMTP('mail.lemieuxbedard.com');
}
/**
* Return the swift message object.
* @return object The swift connection object.
* @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
* @since 1.0.0
*/
protected function getSwiftMessage()
{
return new Swift_Message($this->subject, $this->render());
}
/**
* Start the rendering process by loading the template file under an output
* buffer and caching the results and display it. The common object used in
* most agavi template will be created too.
* @return string The rendered result.
* @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
* @since 1.0.0
*/
public function render()
{
$layer = new LemieuxMailTemplateLayer();
$layer->initialize($this->context);
$layer->setParameter('template', $this->template);
$layer->setParameter('extension', '.php');
$renderer = new LemieuxMailRenderer();
$renderer->initialize($this->context, array('assigns' => array('routing' => 'ro', 'translation_manager' => 'tm')));
return $renderer->render($layer, $this->getAttributes());
}
/**
* Send the email. Replace all the variable for the value and send the
* email to the receiver.
* @return void.
* @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
* @since 1.0.0
* @todo Make better.
*/
public function send()
{
$message = $this->getSwiftMessage();
// add the cc and bcc if they have been set. Only the sender and receiver
// informations are required
if ($this->cc != null) $message->setCC($this->cc->mail, $this->cc->name);
if ($this->bcc != null) $message->setBcc($this->bcc->mail, $this->bcc->name);
// at this point we are ready to send the email
return $this->swift->send($message,
new Swift_Address($this->to->mail, $this->to->name),
new Swift_Address($this->from->mail, $this->from->name)
);
}
}
/**
* LemieuxMailAddress is a simple component to store an email address with the
* name of the owner.
* @package core
* @subpackage mail
* @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
* @copyright Lemieux Bedard Communication (jean-philippe.dery@lemieuxbedard.com)
* @since 1.0.0
* @version 1.0.0
*/
class LemieuxMailAddress
{
public $name = '';
public $mail = '';
public function __construct($mail, $name = '') {
$this->name = $name;
$this->mail = $mail;
}
}
?>
<?php
// +---------------------------------------------------------------------------+
// | This file is part of a ImNoDesigner project. |
// | Copyright (C) Lemieux Bedard Communication |
// | |
// | For the full copyright and license information, please view the LICENSE |
// | file that was distributed with this source code. |
// +---------------------------------------------------------------------------+
/**
* A renderer produces the output as defined by a View.
* @package core
* @subpackage control
* @author David Zülke <dz@bitxtender.com>
* @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
* @copyright Lemieux Bedard Communication (jean-philippe.dery@lemieuxbedard.com)
* @since 1.0.0
* @version 1.0.0
*/
class LemieuxMailRenderer extends AgaviPhpRenderer
{
protected $moreAssigns = array();
}
?>
<?php
// +---------------------------------------------------------------------------+
// | This file is part of a ImNoDesigner project. |
// | Copyright (C) Lemieux Bedard Communication |
// | |
// | For the full copyright and license information, please view the LICENSE |
// | file that was distributed with this source code. |
// +---------------------------------------------------------------------------+
/**
* Template layer implementation for templates fetched using a PHP stream.
* @package core
* @subpackage control
* @author David Zülke <dz@bitxtender.com>
* @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
* @copyright Jean-Philippe Dery
* @since 1.0.0
* @version 1.0.0
*/
class LemieuxMailTemplateLayer extends AgaviStreamTemplateLayer
{
/**
* Constructor
* @param array Initial parameters.
* @author David Zülke <dz@bitxtender.com>
* @since 0.11.0
*/
public function __construct(array $parameters = array())
{
$targets = array();
if (AgaviConfig::get('core.use_translation')) {
$targets[] = '${directory}/${locale}/${template}${extension}';
$targets[] = '${directory}/${template}.${locale}${extension}';
}
$targets[] = '${directory}/${template}${extension}';
parent::__construct(array_merge(array(
'directory' => AgaviConfig::get('core.template_dir') . '/mails',
'scheme' => 'file',
'check' => true,
'targets' => $targets,
), $parameters));
}
}
?>