| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 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 |
|
|---|
| 38 |
parent::configure(); |
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 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 |
|
|---|
| 55 |
$this->preRenderCheck(); |
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 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 |
|
|---|
| 65 |
$template = $this->getDirectory().'/'.$this->getTemplate(); |
|---|
| 66 |
$retval = $this->renderFile($template); |
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 123 |
$mail->prepare(); |
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
if (sfConfig::get($config_prefix.'deliver')) |
|---|
| 127 |
{ |
|---|
| 128 |
$mail->send(); |
|---|
| 129 |
} |
|---|
| 130 |
|
|---|
| 131 |
return $mail->getRawHeader().$mail->getRawBody(); |
|---|
| 132 |
} |
|---|
| 133 |
} |
|---|
| 134 |
|
|---|