[ Index ]

PHP Cross Reference of Akelos Framework

title

Body

[close]

/ -> AkActionMailer.php (source)

   1  <?php
   2  /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
   3  
   4  // +----------------------------------------------------------------------+
   5  // | Akelos Framework - http://www.akelos.org                             |
   6  // +----------------------------------------------------------------------+
   7  // | Copyright (c) 2002-2006, Akelos Media, S.L.  & Bermi Ferrer Martinez |
   8  // | Released under the GNU Lesser General Public License, see LICENSE.txt|
   9  // +----------------------------------------------------------------------+
  10  
  11  /**
  12   * @package AkelosFramework
  13   * @subpackage AkActionMailer
  14   * @author Bermi Ferrer <bermi a.t akelos c.om>
  15   * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
  16   * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
  17   */
  18  
  19  require_once (AK_LIB_DIR.DS.'AkBaseModel.php');
  20  require_once (AK_LIB_DIR.DS.'AkActionMailer'.DS.'AkMailMessage.php');
  21  require_once (AK_LIB_DIR.DS.'AkActionMailer'.DS.'AkMailParser.php');
  22  require_once (AK_LIB_DIR.DS.'AkActionMailer'.DS.'AkActionMailerQuoting.php');
  23  require_once (AK_LIB_DIR.DS.'AkActionMailer'.DS.'AkMailComposer.php');
  24  
  25  ak_define('MAIL_EMBED_IMAGES_AUTOMATICALLY_ON_EMAILS', false);
  26  ak_define('ACTION_MAILER_DEFAULT_CHARSET', AK_CHARSET);
  27  ak_define('ACTION_MAILER_EOL', "\r\n");
  28  ak_define('ACTION_MAILER_EMAIL_REGULAR_EXPRESSION', "([a-z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-z0-9\-]+\.)+))([a-z]{2,4}|[0-9]{1,3})(\]?)");
  29  ak_define('ACTION_MAILER_RFC_2822_DATE_REGULAR_EXPRESSION', "(?:(Mon|Tue|Wed|Thu|Fri|Sat|Sun), *)?(\d\d?) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) (\d\d\d\d) (\d{2}:\d{2}(?::\d\d)) (UT|GMT|EST|EDT|CST|CDT|MST|MDT|PST|PDT|[A-Z]|(?:\+|\-)\d{4})");
  30  
  31  /**
  32  * AkActionMailer allows you to send email from your application using a mailer model and views.
  33  *
  34  * = Mailer Models
  35  *
  36  * To use AkActionMailer, you need to create a mailer model.
  37  *   
  38  *   $ ./script/generate mailer Notifier
  39  *
  40  * The generated model inherits from AkActionMailer. Emails are defined by 
  41  * creating methods within the model which are then used to set variables to be 
  42  * used in the mail template, to change options on the mail, or to add attachments.
  43  *
  44  * Examples:
  45  *
  46  *  class Notifier extends AkActionMailer
  47  *  {
  48  *    function signupNotification($Recipient)
  49  *    {
  50  *      $this->setRecipients($Recipient->getEmailAddressWithName());
  51  *      $this->setFrom("system@example.com");
  52  *      $this->setSubject("New account information");
  53  *      $this->setBody(array('account' => $Recipient ));
  54  *    }
  55  *  }
  56  *
  57  * Mailer methods have the following configuration methods available.
  58  *
  59  * * <tt>setRecipients</tt> - Takes one or more email addresses. These addresses are where your email will be delivered to. Sets the <tt>To:</tt> header.
  60  * * <tt>setSubject</tt> - The subject of your email. Sets the <tt>Subject:</tt> header.
  61  * * <tt>setFrom</tt> - Who the email you are sending is from. Sets the <tt>From:</tt> header.
  62  * * <tt>setCc</tt> - Takes one or more email addresses. These addresses will receive a carbon copy of your email. Sets the <tt>Cc:</tt> header.
  63  * * <tt>setBcc</tt> - Takes one or more email address. These addresses will receive a blind carbon copy of your email. Sets the <tt>Bcc</tt> header.
  64  * * <tt>setSentOn</tt> - The date on which the message was sent. If not set, the header wil be set by the delivery agent.
  65  * * <tt>setContentType</tt> - Specify the content type of the message. Defaults to <tt>text/plain</tt>.
  66  * * <tt>setHeaders</tt> - Specify additional headers to be set for the message, e.g. <tt>$this->setHeaders(array('X-Mail-Count' => 107370));</tt>.
  67  *
  68  * The <tt>setBody</tt> method has special behavior. It takes an array which generates an instance variable
  69  * named after each key in the array containing the value that that key points to.
  70  *
  71  * So, for example, <tt>setBody(array("account" => $Recipient));</tt> would result
  72  * in an instance variable <tt>$account</tt> with the value of <tt>$Recipient</tt> being accessible in the 
  73  * view.
  74  *
  75  *
  76  * = Mailer views
  77  *
  78  * Like AkAkActionController, each mailer class has a corresponding view directory
  79  * in which each method of the class looks for a template with its name.
  80  * To define a template to be used with a mailing, create an <tt>.tpl</tt> file with the same name as the method
  81  * in your mailer model. For example, in the mailer defined above, the template at 
  82  * <tt>app/views/notifier/signup_notification.tpl</tt> would be used to generate the email.
  83  *
  84  * Variables defined in the model are accessible as instance variables in the view.
  85  *
  86  * Emails by default are sent in plain text, so a sample view for our model example might look like this:
  87  *
  88  *   Hi {account.name},
  89  *   Thanks for joining our service! Please check back often.
  90  *
  91  * You can even use Action View helpers in these views. For example:
  92  *
  93  *   You got a new note!
  94  *   <?=$text_helper->truncate($note->body, 25);?>
  95  * 
  96  *
  97  * = Generating URLs for mailer views
  98  *
  99  * If your view includes URLs from the application, you need to use Ak::urlFor in 
 100  * the mailing method instead of the view.
 101  * Unlike controllers from Action View, the mailer instance doesn't have any 
 102  * context about the incoming request. That's why you need to jump this little 
 103  * hoop and supply all the details needed for the URL. 
 104  * 
 105  * Example:
 106  *
 107  *   function signupNotification($Recipient)
 108  *   {
 109  *       // This is the same as calling each individual setter
 110  *       $this->setAttributes(array(
 111  *           'recipients' => $Recipient->getEmailAddressWithName(),
 112  *           'from'       => "system@example.com",
 113  *           'subject'    => "New account information",
 114  *           'body'       => array(
 115  *               'account'   => $Recipient,
 116  *               'home_page' => Ak::urlFor(array('host' => "example.com", 'controller' => "welcome", 'action' => "greeting"))
 117  *           )
 118  *       ));
 119  *   }
 120  *
 121  * You can now access @home_page in the template and get http://example.com/welcome/greeting.
 122  *
 123  * = Sending mail
 124  *
 125  * Once a mailer action and template are defined, you can deliver your message or 
 126  * create it and save it for delivery later:
 127  *
 128  *   Notifier::deliver('signup_notification', $David); // sends the email
 129  *   $Message = Notifier::create('signup_notification', $David); // => A PEAR::Mail object
 130  *   Notifier::deliver($Message);
 131  * 
 132  * You never instantiate your mailer class. Rather, your delivery instance
 133  * methods are automatically wrapped in class methods that are called statically
 134  * The <tt>signup_notification</tt> method defined above is delivered by invoking 
 135  * <tt>$Notifier =& new Notifier(); $Notifier->signupNotification(); $Notifier->deliver();</tt>.
 136  *
 137  *
 138  * = HTML email
 139  *
 140  * To send mail as HTML, make sure your view (the <tt>.tpl</tt> file) generates HTML and
 141  * set the content type to html.
 142  *
 143  *   class ApplicationMailer extends AkActionMailer
 144  *   {
 145  *       function signupNotification($Recipient)
 146  *       {
 147  *           $this->setAttributes(array(
 148  *               'recipients' => $Recipient->getEmailAddressWithName(),
 149  *               'from'       => "system@example.com",
 150  *               'subject'    => "New account information",
 151  *               'body'       => array('account'   => $Recipient),
 152  *               'content_type' => text/html" //    Here's where the magic happens 
 153  *           ));
 154  *       }
 155  *   }
 156  *
 157  *
 158  * = Multipart email
 159  *
 160  * You can explicitly specify multipart messages:
 161  *
 162  *   class ApplicationMailer extends AkActionMailer
 163  *   {
 164  *       function signupNotification($Recipient)
 165  *       {
 166  *           $this->setAttributes(array(
 167  *               'recipients' => $Recipient->getEmailAddressWithName(),
 168  *               'from'       => "system@example.com",
 169  *               'subject'    => "New account information"
 170  *           ));
 171  * 
 172  *           $this->addPart(array(
 173  *               'content_type' => "text/html",
 174  *               'body' => $this->renderMessage('signup-as-html', 'account' => $recipient)));
 175  * 
 176  *           $this->addPart("text/plain", array(
 177  *               'transfer_encoding' = "base64",
 178  *               'body' => $this->renderMessage('signup-as-plain', 'account' => $recipient)));
 179  *       }
 180  *   }
 181  *  
 182  * Multipart messages can also be used implicitly because AkActionMailer will automatically
 183  * detect and use multipart templates, where each template is named after the name of the action, followed
 184  * by the content type. Each such detected template will be added as separate part to the message.
 185  * 
 186  * For example, if the following templates existed:
 187  * * signup_notification.text.plain.tpl
 188  * * signup_notification.text.html.tpl
 189  *  
 190  * Each would be rendered and added as a separate part to the message,
 191  * with the corresponding content type. The same body array is passed to
 192  * each template.
 193  *
 194  *
 195  * = Attachments
 196  *
 197  * Attachments can be added by using the +addAttachment+ method.
 198  *
 199  * Example:
 200  *
 201  *   class ApplicationMailer extends AkActionMailer
 202  *   {
 203  *       // attachments
 204  *       function signupNotification($Recipient)
 205  *       {
 206  *           $this->setAttributes(array(
 207  *               'recipients' => $Recipient->getEmailAddressWithName(),
 208  *               'from'       => "system@example.com",
 209  *               'subject'    => "New account information"
 210  *           ));
 211  *
 212  *           $this->addAttachment(array(
 213  *               'content_type' => 'image/jpeg',
 214  *               'body' => Ak::file_get_contents("an-image.jpg")));
 215  * 
 216  *           $this->addAttachment('application/pdf', generate_your_pdf_here());
 217  *       }
 218  *   }
 219  *
 220  *
 221  * = Configuration options
 222  *
 223  * These options are specified on the class level, as class attriibutes 
 224  * <tt>$AkActionMailerInstance->templateRoot = "/my/templates";</tt>
 225  *
 226  * * <tt>templateRoot</tt> - template root determines the base from which template references will be made.
 227  *
 228  * * <tt>server_settings</tt> -  Allows detailed configuration of the server:
 229  *   * <tt>address</tt> Allows you to use a remote mail server. Just change it 
 230  *       from its default "localhost" setting.
 231  *   * <tt>port</tt> On the off chance that your mail server doesn't run on port 25, you can change it.
 232  *   * <tt>domain</tt> If you need to specify a HELO domain, you can do it here.
 233  *   * <tt>user_name</tt> If your mail server requires authentication, set the username in this setting.
 234  *   * <tt>password</tt> If your mail server requires authentication, set the password in this setting.
 235  *   * <tt>authentication</tt> If your mail server requires authentication, you need to specify the authentication type here. 
 236  *     Options are: plain, login, cram_md5
 237  *
 238  * * <tt>delivery_method</tt> - Defines a delivery method. Possible values are 'php' (default), 'smtp', and 'test'.
 239  *
 240  * * <tt>perform_deliveries</tt> - Determines whether AkActionMailer::deliver(*) methods are actually carried out. By default they are,
 241  *   but this can be turned off to help functional testing.
 242  *
 243  * * <tt>deliveries</tt> - Keeps an array of all the emails sent out through the Action Mailer with delivery_method 'test'. Most useful
 244  *   for unit and functional testing.
 245  *
 246  * * <tt>default_charset</tt> - The default charset used for the body and to encode the subject. Defaults to UTF-8. You can also 
 247  *   pick a different charset from inside a method with <tt>$this->charset</tt>.
 248  * * <tt>default_content_type</tt> - The default content type used for the main part of the message. Defaults to "text/plain". You
 249  *   can also pick a different content type from inside a method with <tt>$this->content_type</tt>. 
 250  * * <tt>default_mime_version</tt> - The default mime version used for the message. Defaults to "1.0". You
 251  *   can also pick a different value from inside a method with <tt>$this->mime_version</tt>.
 252  * * <tt>default_implicit_parts_order</tt> - When a message is built implicitly (i.e. multiple parts are assembled from templates
 253  *   which specify the content type in their filenames) this variable controls how the parts are ordered. Defaults to
 254  *   array("multipart/alternative", "text/html", "text/enriched", "text/plain"). Items that appear first in the array have higher priority in the mail client
 255  *   and appear last in the mime encoded message. You can also pick a different order from inside a method with
 256  *   <tt>$this->implicit_parts_order</tt>.
 257  */
 258  class AkActionMailer extends AkBaseModel
 259  {
 260      var $templateRoot;
 261      var $server_settings = array(
 262      'address'        => 'localhost',
 263      'port'           => 25,
 264      'domain'         => 'localhost.localdomain',
 265      'user_name'      => null,
 266      'password'       => null,
 267      'authentication' => null
 268      );
 269  
 270      var $delivery_method = 'php';
 271      var $perform_deliveries = true;
 272      var $deliveries = array();
 273      var $default_charset = AK_ACTION_MAILER_DEFAULT_CHARSET;
 274      var $default_content_type = 'text/plain';
 275      var $default_mime_version = '1.0';
 276      var $default_implicit_parts_order = array('multipart/alternative', 'text/html', 'text/enriched', 'text/plain');
 277      var $Message;
 278      var $Composer;
 279      var $_defaultMailDriverName = 'AkMailMessage';
 280  
 281      function __construct($Driver = null)
 282      {
 283          $this->loadSettings();
 284          if(empty($Driver)){
 285              $this->Message =& new $this->_defaultMailDriverName();
 286          }else{
 287              $this->Message =& $Driver;
 288          }
 289      }
 290  
 291      function loadSettings()
 292      {
 293          if($settings = Ak::getSettings('mailer', false)){
 294              foreach ($settings as $k=>$v){
 295                  $this->$k = $v;
 296              }
 297          }
 298      }
 299  
 300      /**
 301      * Specify the template name to use for current message. This is the "base"
 302      * template name, without the extension or directory, and may be used to
 303      * have multiple mailer methods share the same template.
 304      */
 305      function setTemplate($template_name)
 306      {
 307          $this->template = $template_name;
 308      }
 309  
 310      /**
 311      * Override the mailer name, which defaults to an inflected version of the
 312      * mailer's class name. If you want to use a template in a non-standard
 313      * location, you can use this to specify that location.
 314      */
 315      function setMailerName($mailerName)
 316      {
 317          $this->mailerName = $mailerName;
 318      }
 319  
 320      // Mail object specific setters
 321  
 322  
 323      /**
 324      * Define the body of the message. This is either an array (in which case it
 325      * specifies the variables to pass to the template when it is rendered),
 326      * or a string, in which case it specifies the actual text of the message.
 327      */
 328      function setBody($body)
 329      {
 330          if(is_array($body) && count($body) == 1 && array_key_exists(0,$body)){
 331              $body = $body[0];
 332          }
 333          $this->Message->setBody($body);
 334      }
 335  
 336      /**
 337      * Specify the CC addresses for the message.
 338      */
 339      function setCc($cc)
 340      {
 341          $this->Message->setCc($cc);
 342      }
 343  
 344      /**
 345      * Specify the BCC addresses for the message.
 346      */    
 347      function setBcc($bcc)
 348      {
 349          $this->Message->setBcc($bcc);
 350      }
 351      /**
 352       * Specify the charset to use for the message. This defaults to the
 353       *  +default_charset+ specified for AkActionMailer.
 354       */
 355      function setCharset($charset)
 356      {
 357          $this->Message->setCharset($charset);
 358      }
 359  
 360      /**
 361       * Specify the content type for the message. This defaults to <tt>text/plain</tt>
 362       * in most cases, but can be automatically set in some situations.
 363       */
 364      function setContentType($content_type)
 365      {
 366          $this->Message->setContentType($content_type);
 367      }
 368  
 369      /**
 370       * Specify the from address for the message.
 371       */
 372      function setFrom($from)
 373      {
 374          $this->Message->setFrom($from);
 375      }
 376  
 377      /**
 378       * Specify additional headers to be added to the message.
 379       */
 380      function setHeaders($headers)
 381      {
 382          $this->Message->setHeaders($headers);
 383      }
 384  
 385      /**
 386      * Specify the order in which parts should be sorted, based on content-type.
 387      * This defaults to the value for the +default_implicit_parts_order+.
 388      */
 389      function setImplicitPartsOrder($implicit_parts_order)
 390      {
 391          $this->Message->setImplicitPartsOrder($implicit_parts_order);
 392      }
 393  
 394  
 395      /**
 396       * Defaults to "1.0", but may be explicitly given if needed.
 397       */
 398      function setMimeVersion($mime_version)
 399      {
 400          $this->Message->setMimeVersion($mime_version);
 401      }
 402  
 403      /**
 404       * The recipient addresses for the message, either as a string (for a single
 405       * address) or an array (for multiple addresses).
 406       */
 407      function setRecipients($recipients)
 408      {
 409          $this->Message->setRecipients($recipients);
 410      }
 411  
 412      /**
 413      * The date on which the message was sent. If not set (the default), the
 414      * header will be set by the delivery agent.
 415      */
 416      function setSentOn($date)
 417      {
 418          $this->Message->setSentOn($date);
 419      }
 420  
 421  
 422      /**
 423       * Specify the subject of the message.
 424       */
 425      function setSubject($subject)
 426      {
 427          $this->Message->setSubject($subject);
 428      }
 429  
 430      /**
 431      * Add an attachment to the message.
 432      *
 433      * Example:
 434      *
 435      *   class ApplicationMailer extends AkActionMailer
 436      *   {
 437      *       // attachments
 438      *       function signupNotification($Recipient)
 439      *       {
 440      *           $this->setAttributes(array(
 441      *               'recipients' => $Recipient->getEmailAddressWithName(),
 442      *               'from'       => "system@example.com",
 443      *               'subject'    => "New account information"
 444      *           ));
 445      *
 446      *           $this->addAttachment(array(
 447      *               'content_type' => 'image/jpeg',
 448      *               'body' => Ak::file_get_contents("an-image.jpg")));
 449      * 
 450      *           $this->addAttachment('application/pdf', generate_your_pdf_here());
 451      *       }
 452      *   }
 453      *
 454      *
 455       */
 456      function addAttachment()
 457      {
 458          $args = func_get_args();
 459          return call_user_func_array(array(&$this->Message, 'setAttachment'), $args);
 460      }
 461  
 462      /**
 463       * Generic setter
 464       * 
 465       * Calling $this->set(array('body'=>'Hello World', 'subject' => 'First subject'));
 466       * is the same as calling $this->setBody('Hello World'); and $this->setSubject('First Subject');
 467       * 
 468       * This simplifies creating mail objects from datasources.
 469       * 
 470       * If the method does not exists the parameter will be added to the body.
 471       */
 472      function set($attributes = array())
 473      {
 474          if(!empty($attributes['template'])){
 475              $this->setTemplate($attributes['template']);
 476              unset($attributes['template']);
 477          }
 478          $this->Message->set($attributes);
 479          
 480          $this->_setter_has_been_called = true;
 481      }
 482  
 483  
 484      /**
 485       * Gets a well formed mail in plain text
 486       */
 487      function getEncoded()
 488      {
 489          $this->Message->getEncoded();
 490      }
 491  
 492      /**
 493       * The mail object instance referenced by this mailer.
 494       */
 495      function &getMail()
 496      {
 497          return $this->Message;
 498      }
 499  
 500  
 501      /**
 502       * Receives a raw email, parses it into an email object, decodes it,
 503       * instantiates a new mailer, and passes the email object to the mailer
 504       * object's #receive method. If you want your mailer to be able to
 505       * process incoming messages, you'll need to implement a #receive
 506       * method that accepts the email object as a parameter and then call
 507       * the AkActionMailer::recieve method using "parent::recieve($Message);"
 508       * 
 509       *
 510       *   class MyMailer extends AkActionMailer{
 511       *     function receive($Message){
 512       *          parent::receive($Message);
 513       *       ...
 514       *     }
 515       *   }
 516       */
 517      function &receive($raw_mail)
 518      {
 519          $this->Message =& AkMailBase::parse($raw_mail);
 520          return $this->Message;
 521      }
 522  
 523  
 524      /**
 525       * Deliver the given mail object directly. This can be used to deliver
 526       * a preconstructed mail object, like:
 527       *
 528       *   $email =& $MyMailer->createSomeMail($parameters);
 529       *   $email->setHeader("frobnicate");
 530       *   MyMailer::deliver($email);
 531       */
 532      function deliverDirectly(&$Message)
 533      {
 534          $Message =& new $this->_defaultMailDriverName ($Message);
 535          $Message->send();
 536      }
 537  
 538      function getRawMessage()
 539      {
 540          if(empty($this->Message->_has_been_created_by_mailer)){
 541              trigger_error(Ak::t('You need to create() a message before getting it as raw text.'),E_USER_ERROR);
 542              return false;
 543          }
 544          $Composer =& $this->getComposer();
 545          return $Composer->getRawMessage();
 546      }
 547  
 548  
 549      /**
 550       * Initialize the mailer via the given +method_name+. The body will be
 551       * rendered and a new AkMailMessage object created.
 552       */
 553      function &create($method_name, $parameters, $content_type = '')
 554      {
 555          $Composer =& $this->getComposer();
 556          $args = func_get_args();
 557          call_user_func_array(array(&$Composer, 'build'), $args);
 558          $this->Message->_has_been_created_by_mailer = true;
 559          return $this->Message;
 560      }
 561  
 562  
 563      /**
 564      * Delivers an AkMailMessage object. By default, it delivers the cached mail
 565      * object (from the AkActionMailer::create method). If no cached mail object exists, and
 566      * no alternate has been given as the parameter, this will fail.
 567      */
 568      function deliver($method_name, $parameters = null, $Message = null)
 569      {
 570          if(empty($Message) &&
 571          (empty($this->Message) || (!empty($this->Message) && get_class($this->Message) != get_class($this)))){
 572              $this->create($method_name, $parameters);
 573          }elseif(!empty($Message)){
 574              $this->Message =& $Message;
 575          }
 576          
 577          !empty($this->Message) or trigger_error(Ak::t('No mail object available for delivery!'), E_USER_ERROR);
 578          if(!empty($this->perform_deliveries)){
 579              $this->{"perform".ucfirst(strtolower($this->delivery_method))."Delivery"}($this->Message);
 580          }
 581          $this->Message->_has_been_delivered_by_mailer = true;
 582          return $this->Message;
 583      }
 584  
 585      function performSmtpDelivery(&$Message, $settings = array())
 586      {
 587          $default_settings = array(
 588          'host'     =>  @$this->server_settings['address'],
 589          'localhost'     =>  @$this->server_settings['domain'],
 590          'port'     =>  @$this->server_settings['port'],
 591          'username'     =>  @$this->server_settings['user_name'],
 592          'password'     =>  @$this->server_settings['password'],
 593          'auth'     =>  (!empty($this->server_settings['user_name']) || @$this->server_settings['authentication']),
 594          //'debug'    =>  true
 595          );
 596          $settings = array_merge($default_settings, $settings);
 597  
 598          return $this->_deliverUsingMailDeliveryMethod('Smtp', $Message, $settings);
 599  
 600      }
 601  
 602      function performPhpDelivery(&$Message, $settings = array())
 603      {
 604          return $this->_deliverUsingMailDeliveryMethod('PhpMail', $Message, $settings);
 605      }
 606  
 607      function performTestDelivery(&$Message)
 608      {
 609          return $this->_deliverUsingMailDeliveryMethod('Test', $Message, array('ActionMailer'=>&$this));
 610  
 611      }
 612  
 613  
 614      function renderMessage($method_name, $body, $options = array())
 615      {
 616          return $this->render(array_merge($options, array('file' => $method_name, 'body' => $body)));
 617      }
 618  
 619      function render($options = array())
 620      {
 621          $body = $options['body'];
 622          unset($options['body']);
 623          $Template =& $this->_initializeTemplateClass($body);
 624          $options['locals'] = array_merge((array)@$options['locals'], $this->getHelpers());
 625          $options['locals'] = array_merge($options['locals'], array('mailer'=>&$this));
 626          return $Template->render($options);
 627      }
 628  
 629      function getTemplatePath()
 630      {
 631          return $this->templateRoot.DS.$this->mailerName;
 632      }
 633  
 634  
 635  
 636      /**
 637      * Set up the default values for the various instance variables of this
 638      * mailer. Subclasses may override this method to provide different
 639      * defaults.
 640      */
 641      function initializeDefaults($method_name)
 642      {
 643          foreach (array('charset','content_type','implicit_parts_order', 'mime_version') as $attribute) {
 644              $method = 'set'.AkInflector::camelize($attribute);
 645              $this->Message->$method(empty($this->$attribute) ? $this->{'default_'.$attribute} : $this->$attribute);
 646          }
 647          foreach (array('parts','headers','body') as $attribute) {
 648              $method = 'set'.AkInflector::camelize($attribute);
 649              $this->Message->$method(empty($this->$attribute) ? array() : $this->$attribute);
 650          }
 651  
 652          $this->templateRoot = empty($this->templateRoot) ? AK_APP_DIR.DS.'views' : $this->templateRoot;
 653          $this->template = empty($this->template) ? $method_name : $this->template;
 654          $this->mailerName = empty($this->mailerName) ? AkInflector::underscore($this->getModelName()) : $this->mailerName;
 655      }
 656  
 657  
 658      function &_initializeTemplateClass($assigns)
 659      {
 660          require_once (AK_LIB_DIR.DS.'AkActionView.php');
 661          $TemplateInstance =& new AkActionView($this->getTemplatePath(), $assigns, $this);
 662          require_once  (AK_LIB_DIR.DS.'AkActionView'.DS.'AkPhpTemplateHandler.php');
 663          $TemplateInstance->_registerTemplateHandler('tpl','AkPhpTemplateHandler');
 664          return $TemplateInstance;
 665      }
 666  
 667      function &getComposer()
 668      {
 669          if(empty($this->Composer)){
 670              $this->setComposer();
 671          }
 672          return $this->Composer;
 673      }
 674  
 675      function setComposer($Composer = null)
 676      {
 677          if(!empty($Composer)){
 678              $this->Composer = $Composer;
 679          }else{
 680              $this->Composer =& new AkMailComposer();
 681              $this->Composer->init($this);
 682          }
 683      }
 684  
 685  
 686      /**
 687       * Alias for getModelName
 688       */
 689      function getMailerName()
 690      {
 691          return $this->getModelName();
 692      }
 693  
 694  
 695      /**
 696       * Workarround for limited support of helpers on ActionMailer Views
 697       * 
 698       * @todo refactor helpers to be controller agnostic
 699       */
 700      function getControllerName()
 701      {
 702          return $this->getModelName();
 703      }
 704  
 705      /**
 706       * This is the url_for version for helpers and emails.
 707       * 
 708       * As we do not have the context of a host being requested, we need to know
 709       * the base_url like http://example.com in oder to add it to the generated URL
 710       */
 711      function urlFor()
 712      {
 713          $args = func_get_args();
 714          $base_url = '';
 715          if(isset($args[0]['base_url'])){
 716              $base_url = preg_replace('/^(?!http[s]?:\/\/)(.+)/','http://$1', (isset($args[0]['base_url'])?rtrim($args[0]['base_url'],'/'):Ak::getSetting('mailer', 'base_url', AK_HOST)));
 717              unset($args[0]['base_url']);
 718          }
 719         
 720          unset($args[0]['only_path'], $args[0]['base_url']);
 721  
 722          return $base_url.call_user_func_array(array('Ak','toUrl'), $args);
 723      }
 724  
 725      /**
 726       * Creates an instance of each available helper and links it into into current mailer.
 727       * 
 728       * Mailer helpers work as Controller helpers but without the Request context
 729       */
 730      function getHelpers()
 731      {
 732          require_once (AK_LIB_DIR.DS.'AkActionView'.DS.'AkHelperLoader.php');
 733          $HelperLoader = new AkHelperLoader();
 734          $HelperLoader->setHandler(&$this);
 735          return $HelperLoader->getHelpersForMailer();
 736      }
 737  
 738      function _deliverUsingMailDeliveryMethod($method, &$Message, $options)
 739      {
 740          $handler_name = 'Ak'.AkInflector::camelize(Ak::sanitize_include($method, 'paranoid')).'Delivery';
 741          $handler_path = AK_LIB_DIR.DS.'AkActionMailer'.DS.'AkMailDelivery'.DS.$handler_name.'.php';
 742          if(file_exists($handler_path)){
 743              require_once($handler_path);
 744          }
 745  
 746          if(!class_exists($handler_name)){
 747              trigger_error(Ak::t('Could not find message handler %handler_name', array('%handler_name'=>$handler_name)), E_USER_ERROR);
 748              return false;
 749          }
 750          $DeliveryHandler = new $handler_name();
 751          $this->Message =& $Message;
 752          return $DeliveryHandler->deliver($this, $options);
 753      }
 754  
 755  
 756  
 757  }
 758  
 759  ?>


Generated: Mon Oct 27 12:43:49 2008 Cross-referenced by PHPXref 0.6