Development

/branches/1.2/lib/plugins/sfCompat10Plugin/lib/view/sfMailView.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/plugins/sfCompat10Plugin/lib/view/sfMailView.class.php

Revision 7691, 3.4 kB (checked in by fabien, 6 years ago)

simplified directory structure customization

  • removed *_dir_name constantsi
  • added methods to change the directory structure of a symfony application
    • setRootDir()
    • setCacheDir()
    • setLogDir()
    • setWebDir()
    • setAppDir()
  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev Date
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  *
13  * @package    symfony
14  * @subpackage view
15  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
16  * @version    SVN: $Id$
17  */
18 class sfMailView extends sfPHPView
19 {
20   /**
21    * Retrieves the template engine associated with this view.
22    *
23    * @return string sfMail
24    */
25   public function getEngine()
26   {
27     return 'sfMail';
28   }
29
30   /**
31    * Configures template for this view.
32    *
33    * @throws <b>sfActionException</b> If the configure fails
34    */
35   public function configure()
36   {
37     // view.yml configure
38     parent::configure();
39
40     // require our configuration
41     $moduleName = $this->moduleName;
42     require($this->context->getConfigCache()->checkConfig('modules/'.$this->moduleName.'/config/mailer.yml'));
43   }
44
45   /**
46    * Renders the presentation and send the email to the client.
47    *
48    * @return mixed Raw data of the mail
49    */
50   public function render()
51   {
52     $retval = null;
53
54     // execute pre-render check
55     $this->preRenderCheck();
56
57     // get sfMail object from action
58     $mail = $this->attributeHolder->get('mail');
59     if (!$mail)
60     {
61       throw new sfActionException('You must define a sfMail object named $mail ($this->mail) in your action to be able to use a sfMailView.');
62     }
63
64     // render main template
65     $template = $this->getDirectory().'/'.$this->getTemplate();
66     $retval = $this->renderFile($template);
67
68     // render main and alternate templates
69     $all_template_dir  = dirname($template);
70     $all_template_regex = preg_replace('/\\.php$/', '\..+\.php', basename($template));
71     $all_templates = sfFinder::type('file')->name('/^'.$all_template_regex.'$/')->in($all_template_dir);
72     $all_retvals = array();
73     foreach ($all_templates as $templateFile)
74     {
75       if (preg_match('/\.([^.]+?)\.php$/', $templateFile, $matches))
76       {
77         $all_retvals[$matches[1]] = $this->renderFile($templateFile);
78       }
79     }
80
81     // send email
82     if (sfConfig::get('sf_logging_enabled'))
83     {
84       $this->dispatcher->notify(new sfEvent($this, 'application.log', array('Send email to client')));
85     }
86
87     // configuration prefix
88     $config_prefix = 'sf_mailer_'.strtolower($this->moduleName).'_';
89
90     $vars = array(
91       'mailer',
92       'priority', 'content_type', 'charset', 'encoding', 'wordwrap',
93       'hostname', 'port', 'domain', 'username', 'password'
94     );
95     $defaultMail = new sfMail();
96
97     foreach ($vars as $var)
98     {
99       $setter = 'set'.sfInflector::camelize($var);
100       $getter = 'get'.sfInflector::camelize($var);
101       $value  = $mail->$getter() !== $defaultMail->$getter() ? $mail->$getter() : sfConfig::get($config_prefix.strtolower($var));
102       $mail->$setter($value);
103     }
104
105     $mail->setBody($retval);
106
107     // alternate bodies
108     $i = 0;
109     foreach ($all_retvals as $type => $retval)
110     {
111       if ($type == 'altbody' && !$mail->getAltBody())
112       {
113         $mail->setAltBody($retval);
114       }
115       else
116       {
117         ++$i;
118         $mail->addStringAttachment($retval, 'file'.$i, 'base64', 'text/'.$type);
119       }
120     }
121
122     // preparing email to be sent
123     $mail->prepare();
124
125     // send e-mail
126     if (sfConfig::get($config_prefix.'deliver'))
127     {
128       $mail->send();
129     }
130
131     return $mail->getRawHeader().$mail->getRawBody();
132   }
133 }
134
Note: See TracBrowser for help on using the browser.