| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 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 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 |
|
|---|
| 203 |
|
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 |
|
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 |
|
|---|
| 256 |
|
|---|
| 257 |
|
|---|
| 258 |
|
|---|
| 259 |
|
|---|
| 260 |
|
|---|
| 261 |
|
|---|
| 262 |
|
|---|
| 263 |
|
|---|
| 264 |
|
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 |
|
|---|
| 270 |
|
|---|
| 271 |
|
|---|
| 272 |
|
|---|
| 273 |
|
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| 276 |
|
|---|
| 277 |
|
|---|
| 278 |
|
|---|
| 279 |
|
|---|
| 280 |
|
|---|
| 281 |
|
|---|
| 282 |
|
|---|
| 283 |
|
|---|
| 284 |
|
|---|
| 285 |
|
|---|
| 286 |
class AkActionMailer extends AkBaseModel |
|---|
| 287 |
{ |
|---|
| 288 |
public $templateRoot; |
|---|
| 289 |
public $server_settings = array( |
|---|
| 290 |
'address' => 'localhost', |
|---|
| 291 |
'port' => 25, |
|---|
| 292 |
'domain' => 'localhost.localdomain', |
|---|
| 293 |
'user_name' => null, |
|---|
| 294 |
'password' => null, |
|---|
| 295 |
'authentication' => null |
|---|
| 296 |
); |
|---|
| 297 |
|
|---|
| 298 |
public $delivery_method = 'php'; |
|---|
| 299 |
public $perform_deliveries = true; |
|---|
| 300 |
public $deliveries = array(); |
|---|
| 301 |
public $default_charset = AK_ACTION_MAILER_DEFAULT_CHARSET; |
|---|
| 302 |
public $default_content_type = 'text/plain'; |
|---|
| 303 |
public $default_mime_version = '1.0'; |
|---|
| 304 |
public $default_implicit_parts_order = array('multipart/alternative', 'text/html', 'text/enriched', 'text/plain'); |
|---|
| 305 |
public $Message; |
|---|
| 306 |
public $Composer; |
|---|
| 307 |
public $_defaultMailDriverName = 'AkMailMessage'; |
|---|
| 308 |
|
|---|
| 309 |
public function __construct($Driver = null) |
|---|
| 310 |
{ |
|---|
| 311 |
$this->loadSettings(); |
|---|
| 312 |
if(empty($Driver)){ |
|---|
| 313 |
$this->Message = new $this->_defaultMailDriverName(); |
|---|
| 314 |
}else{ |
|---|
| 315 |
$this->Message =& $Driver; |
|---|
| 316 |
} |
|---|
| 317 |
} |
|---|
| 318 |
|
|---|
| 319 |
public function loadSettings() |
|---|
| 320 |
{ |
|---|
| 321 |
if($settings = Ak::getSettings('mailer', false)){ |
|---|
| 322 |
foreach ($settings as $k=>$v){ |
|---|
| 323 |
$this->$k = $v; |
|---|
| 324 |
} |
|---|
| 325 |
} |
|---|
| 326 |
} |
|---|
| 327 |
|
|---|
| 328 |
|
|---|
| 329 |
* Specify the template name to use for current message. This is the "base" |
|---|
| 330 |
* template name, without the extension or directory, and may be used to |
|---|
| 331 |
* have multiple mailer methods share the same template. |
|---|
| 332 |
*/ |
|---|
| 333 |
public function setTemplate($template_name) |
|---|
| 334 |
{ |
|---|
| 335 |
$this->template = $template_name; |
|---|
| 336 |
} |
|---|
| 337 |
|
|---|
| 338 |
|
|---|
| 339 |
* Override the mailer name, which defaults to an inflected version of the |
|---|
| 340 |
* mailer's class name. If you want to use a template in a non-standard |
|---|
| 341 |
* location, you can use this to specify that location. |
|---|
| 342 |
*/ |
|---|
| 343 |
public function setMailerName($mailerName) |
|---|
| 344 |
{ |
|---|
| 345 |
$this->mailerName = $mailerName; |
|---|
| 346 |
} |
|---|
| 347 |
|
|---|
| 348 |
|
|---|
| 349 |
|
|---|
| 350 |
|
|---|
| 351 |
/** |
|---|
| 352 |
* Define the body of the message. This is either an array (in which case it |
|---|
| 353 |
* specifies the variables to pass to the template when it is rendered), |
|---|
| 354 |
* or a string, in which case it specifies the actual text of the message. |
|---|
| 355 |
*/ |
|---|
| 356 |
public function setBody($body) |
|---|
| 357 |
{ |
|---|
| 358 |
if(is_array($body) && count($body) == 1 && array_key_exists(0,$body)){ |
|---|
| 359 |
$body = $body[0]; |
|---|
| 360 |
} |
|---|
| 361 |
$this->Message->setBody($body); |
|---|
| 362 |
} |
|---|
| 363 |
|
|---|
| 364 |
|
|---|
| 365 |
* Specify the CC addresses for the message. |
|---|
| 366 |
*/ |
|---|
| 367 |
public function setCc($cc) |
|---|
| 368 |
{ |
|---|
| 369 |
$this->Message->setCc($cc); |
|---|
| 370 |
} |
|---|
| 371 |
|
|---|
| 372 |
|
|---|
| 373 |
* Specify the BCC addresses for the message. |
|---|
| 374 |
*/ |
|---|
| 375 |
public function setBcc($bcc) |
|---|
| 376 |
{ |
|---|
| 377 |
$this->Message->setBcc($bcc); |
|---|
| 378 |
} |
|---|
| 379 |
|
|---|
| 380 |
* Specify the charset to use for the message. This defaults to the |
|---|
| 381 |
* +default_charset+ specified for AkActionMailer. |
|---|
| 382 |
*/ |
|---|
| 383 |
public function setCharset($charset) |
|---|
| 384 |
{ |
|---|
| 385 |
$this->Message->setCharset($charset); |
|---|
| 386 |
} |
|---|
| 387 |
|
|---|
| 388 |
|
|---|
| 389 |
* Specify the content type for the message. This defaults to <tt>text/plain</tt> |
|---|
| 390 |
* in most cases, but can be automatically set in some situations. |
|---|
| 391 |
*/ |
|---|
| 392 |
public function setContentType($content_type) |
|---|
| 393 |
{ |
|---|
| 394 |
$this->Message->setContentType($content_type); |
|---|
| 395 |
} |
|---|
| 396 |
|
|---|
| 397 |
|
|---|
| 398 |
* Specify the from address for the message. |
|---|
| 399 |
*/ |
|---|
| 400 |
public function setFrom($from) |
|---|
| 401 |
{ |
|---|
| 402 |
$this->Message->setFrom($from); |
|---|
| 403 |
} |
|---|
| 404 |
|
|---|
| 405 |
|
|---|
| 406 |
* Specify additional headers to be added to the message. |
|---|
| 407 |
*/ |
|---|
| 408 |
public function setHeaders($headers) |
|---|
| 409 |
{ |
|---|
| 410 |
$this->Message->setHeaders($headers); |
|---|
| 411 |
} |
|---|
| 412 |
|
|---|
| 413 |
|
|---|
| 414 |
* Specify the order in which parts should be sorted, based on content-type. |
|---|
| 415 |
* This defaults to the value for the +default_implicit_parts_order+. |
|---|
| 416 |
*/ |
|---|
| 417 |
public function setImplicitPartsOrder($implicit_parts_order) |
|---|
| 418 |
{ |
|---|
| 419 |
$this->Message->setImplicitPartsOrder($implicit_parts_order); |
|---|
| 420 |
} |
|---|
| 421 |
|
|---|
| 422 |
|
|---|
| 423 |
|
|---|
| 424 |
* Defaults to "1.0", but may be explicitly given if needed. |
|---|
| 425 |
*/ |
|---|
| 426 |
public function setMimeVersion($mime_version) |
|---|
| 427 |
{ |
|---|
| 428 |
$this->Message->setMimeVersion($mime_version); |
|---|
| 429 |
} |
|---|
| 430 |
|
|---|
| 431 |
|
|---|
| 432 |
* The recipient addresses for the message, either as a string (for a single |
|---|
| 433 |
* address) or an array (for multiple addresses). |
|---|
| 434 |
*/ |
|---|
| 435 |
public function setRecipients($recipients) |
|---|
| 436 |
{ |
|---|
| 437 |
$this->Message->setRecipients($recipients); |
|---|
| 438 |
} |
|---|
| 439 |
|
|---|
| 440 |
|
|---|
| 441 |
* The date on which the message was sent. If not set (the default), the |
|---|
| 442 |
* header will be set by the delivery agent. |
|---|
| 443 |
*/ |
|---|
| 444 |
public function setSentOn($date) |
|---|
| 445 |
{ |
|---|
| 446 |
$this->Message->setSentOn($date); |
|---|
| 447 |
} |
|---|
| 448 |
|
|---|
| 449 |
|
|---|
| 450 |
|
|---|
| 451 |
* Specify the subject of the message. |
|---|
| 452 |
*/ |
|---|
| 453 |
public function setSubject($subject) |
|---|
| 454 |
{ |
|---|
| 455 |
$this->Message->setSubject($subject); |
|---|
| 456 |
} |
|---|
| 457 |
|
|---|
| 458 |
|
|---|
| 459 |
* Add an attachment to the message. |
|---|
| 460 |
* |
|---|
| 461 |
* Example: |
|---|
| 462 |
* |
|---|
| 463 |
* class ApplicationMailer extends AkActionMailer |
|---|
| 464 |
* { |
|---|
| 465 |
* // attachments |
|---|
| 466 |
* public function signupNotification($Recipient) |
|---|
| 467 |
* { |
|---|
| 468 |
* $this->setAttributes(array( |
|---|
| 469 |
* 'recipients' => $Recipient->getEmailAddressWithName(), |
|---|
| 470 |
* 'from' => "system@example.com", |
|---|
| 471 |
* 'subject' => "New account information" |
|---|
| 472 |
* )); |
|---|
| 473 |
* |
|---|
| 474 |
* $this->addAttachment(array( |
|---|
| 475 |
* 'content_type' => 'image/jpeg', |
|---|
| 476 |
* 'body' => Ak::file_get_contents("an-image.jpg"))); |
|---|
| 477 |
* |
|---|
| 478 |
* $this->addAttachment('application/pdf', generate_your_pdf_here()); |
|---|
| 479 |
* } |
|---|
| 480 |
* } |
|---|
| 481 |
* |
|---|
| 482 |
* |
|---|
| 483 |
*/ |
|---|
| 484 |
public function addAttachment() |
|---|
| 485 |
{ |
|---|
| 486 |
$args = func_get_args(); |
|---|
| 487 |
return call_user_func_array(array(&$this->Message, 'setAttachment'), $args); |
|---|
| 488 |
} |
|---|
| 489 |
|
|---|
| 490 |
|
|---|
| 491 |
* Generic setter |
|---|
| 492 |
* |
|---|
| 493 |
* Calling $this->set(array('body'=>'Hello World', 'subject' => 'First subject')); |
|---|
| 494 |
* is the same as calling $this->setBody('Hello World'); and $this->setSubject('First Subject'); |
|---|
| 495 |
* |
|---|
| 496 |
* This simplifies creating mail objects from datasources. |
|---|
| 497 |
* |
|---|
| 498 |
* If the method does not exists the parameter will be added to the body. |
|---|
| 499 |
*/ |
|---|
| 500 |
public function set($attributes = array()) |
|---|
| 501 |
{ |
|---|
| 502 |
if(!empty($attributes['template'])){ |
|---|
| 503 |
$this->setTemplate($attributes['template']); |
|---|
| 504 |
unset($attributes['template']); |
|---|
| 505 |
} |
|---|
| 506 |
|
|---|
| 507 |
$this->Message->set($attributes); |
|---|
| 508 |
|
|---|
| 509 |
$this->_setter_has_been_called = true; |
|---|
| 510 |
} |
|---|
| 511 |
|
|---|
| 512 |
|
|---|
| 513 |
|
|---|
| 514 |
* Gets a well formed mail in plain text |
|---|
| 515 |
*/ |
|---|
| 516 |
public function getEncoded() |
|---|
| 517 |
{ |
|---|
| 518 |
$this->Message->getEncoded(); |
|---|
| 519 |
} |
|---|
| 520 |
|
|---|
| 521 |
|
|---|
| 522 |
* The mail object instance referenced by this mailer. |
|---|
| 523 |
*/ |
|---|
| 524 |
public function &getMail() |
|---|
| 525 |
{ |
|---|
| 526 |
return $this->Message; |
|---|
| 527 |
} |
|---|
| 528 |
|
|---|
| 529 |
|
|---|
| 530 |
|
|---|
| 531 |
* Receives a raw email, parses it into an email object, decodes it, |
|---|
| 532 |
* instantiates a new mailer, and passes the email object to the mailer |
|---|
| 533 |
* object's #receive method. If you want your mailer to be able to |
|---|
| 534 |
* process incoming messages, you'll need to implement a #receive |
|---|
| 535 |
* method that accepts the email object as a parameter and then call |
|---|
| 536 |
* the AkActionMailer::recieve method using "parent::recieve($Message);" |
|---|
| 537 |
* |
|---|
| 538 |
* |
|---|
| 539 |
* class MyMailer extends AkActionMailer{ |
|---|
| 540 |
* public function receive($Message){ |
|---|
| 541 |
* parent::receive($Message); |
|---|
| 542 |
* ... |
|---|
| 543 |
* } |
|---|
| 544 |
* } |
|---|
| 545 |
*/ |
|---|
| 546 |
public function &receive($raw_mail) |
|---|
| 547 |
{ |
|---|
| 548 |
$this->Message =& AkMailBase::parse($raw_mail); |
|---|
| 549 |
return $this->Message; |
|---|
| 550 |
} |
|---|
| 551 |
|
|---|
| 552 |
|
|---|
| 553 |
|
|---|
| 554 |
* Deliver the given mail object directly. This can be used to deliver |
|---|
| 555 |
* a preconstructed mail object, like: |
|---|
| 556 |
* |
|---|
| 557 |
* $email =& $MyMailer->createSomeMail($parameters); |
|---|
| 558 |
* $email->setHeader("frobnicate"); |
|---|
| 559 |
* MyMailer::deliver($email); |
|---|
| 560 |
*/ |
|---|
| 561 |
public function deliverDirectly(&$Message) |
|---|
| 562 |
{ |
|---|
| 563 |
$Message = new $this->_defaultMailDriverName ($Message); |
|---|
| 564 |
$Message->send(); |
|---|
| 565 |
} |
|---|
| 566 |
|
|---|
| 567 |
public function getRawMessage() |
|---|
| 568 |
{ |
|---|
| 569 |
if(empty($this->Message->_has_been_created_by_mailer)){ |
|---|
| 570 |
trigger_error(Ak::t('You need to create() a message before getting it as raw text.'),E_USER_ERROR); |
|---|
| 571 |
return false; |
|---|
| 572 |
} |
|---|
| 573 |
$Composer =& $this->getComposer(); |
|---|
| 574 |
return $Composer->getRawMessage(); |
|---|
| 575 |
} |
|---|
| 576 |
|
|---|
| 577 |
|
|---|
| 578 |
|
|---|
| 579 |
* Initialize the mailer via the given +method_name+. The body will be |
|---|
| 580 |
* rendered and a new AkMailMessage object created. |
|---|
| 581 |
*/ |
|---|
| 582 |
public function &create($method_name, $parameters, $content_type = '') |
|---|
| 583 |
{ |
|---|
| 584 |
$Composer =& $this->getComposer(); |
|---|
| 585 |
$args = func_get_args(); |
|---|
| 586 |
call_user_func_array(array(&$Composer, 'build'), $args); |
|---|
| 587 |
$this->Message->_has_been_created_by_mailer = true; |
|---|
| 588 |
return $this->Message; |
|---|
| 589 |
} |
|---|
| 590 |
|
|---|
| 591 |
|
|---|
| 592 |
|
|---|
| 593 |
* Delivers an AkMailMessage object. By default, it delivers the cached mail |
|---|
| 594 |
* object (from the AkActionMailer::create method). If no cached mail object exists, and |
|---|
| 595 |
* no alternate has been given as the parameter, this will fail. |
|---|
| 596 |
*/ |
|---|
| 597 |
public function deliver($method_name, $parameters = null, $Message = null) |
|---|
| 598 |
{ |
|---|
| 599 |
if(empty($Message) && |
|---|
| 600 |
(empty($this->Message) || (!empty($this->Message) && get_class($this->Message) != get_class($this)))){ |
|---|
| 601 |
$this->create($method_name, $parameters); |
|---|
| 602 |
}elseif(!empty($Message)){ |
|---|
| 603 |
$this->Message =& $Message; |
|---|
| 604 |
} |
|---|
| 605 |
|
|---|
| 606 |
!empty($this->Message) or trigger_error(Ak::t('No mail object available for delivery!'), E_USER_ERROR); |
|---|
| 607 |
if(!empty($this->perform_deliveries)){ |
|---|
| 608 |
$this->{"perform".ucfirst(strtolower($this->delivery_method))."Delivery"}($this->Message); |
|---|
| 609 |
} |
|---|
| 610 |
$this->Message->_has_been_delivered_by_mailer = true; |
|---|
| 611 |
return $this->Message; |
|---|
| 612 |
} |
|---|
| 613 |
|
|---|
| 614 |
public function performSmtpDelivery(&$Message, $settings = array()) |
|---|
| 615 |
{ |
|---|
| 616 |
$default_settings = array( |
|---|
| 617 |
'host' => @$this->server_settings['address'], |
|---|
| 618 |
'localhost' => @$this->server_settings['domain'], |
|---|
| 619 |
'port' => @$this->server_settings['port'], |
|---|
| 620 |
'username' => @$this->server_settings['user_name'], |
|---|
| 621 |
'password' => @$this->server_settings['password'], |
|---|
| 622 |
'auth' => (!empty($this->server_settings['user_name']) || @$this->server_settings['authentication']), |
|---|
| 623 |
|
|---|
| 624 |
); |
|---|
| 625 |
$settings = array_merge($default_settings, $settings); |
|---|
| 626 |
|
|---|
| 627 |
return $this->_deliverUsingMailDeliveryMethod('Smtp', $Message, $settings); |
|---|
| 628 |
|
|---|
| 629 |
} |
|---|
| 630 |
|
|---|
| 631 |
public function performPhpDelivery(&$Message, $settings = array()) |
|---|
| 632 |
{ |
|---|
| 633 |
return $this->_deliverUsingMailDeliveryMethod('PhpMail', $Message, $settings); |
|---|
| 634 |
} |
|---|
| 635 |
|
|---|
| 636 |
public function performTestDelivery(&$Message) |
|---|
| 637 |
{ |
|---|
| 638 |
return $this->_deliverUsingMailDeliveryMethod('Test', $Message, array('ActionMailer'=>&$this)); |
|---|
| 639 |
|
|---|
| 640 |
} |
|---|
| 641 |
|
|---|
| 642 |
|
|---|
| 643 |
* Renders a message. |
|---|
| 644 |
* |
|---|
| 645 |
* The template for the message to be rendered is located based on convention: |
|---|
| 646 |
* |
|---|
| 647 |
* AK_APP_VIEWS_DIR/mailer_name/method_name.tpl |
|---|
| 648 |
* |
|---|
| 649 |
* To render a multi/part message place templates with the content-type in the mailer_name folder: |
|---|
| 650 |
* |
|---|
| 651 |
* AK_APP_VIEWS_DIR/mailer_name/method_name.text.html.tpl |
|---|
| 652 |
* AK_APP_VIEWS_DIR/mailer_name/method_name.text.plain.tpl |
|---|
| 653 |
* |
|---|
| 654 |
* @param string $method_name |
|---|
| 655 |
* @param array $body |
|---|
| 656 |
* @param array $options |
|---|
| 657 |
* @return string the rendered message |
|---|
| 658 |
*/ |
|---|
| 659 |
public function renderMessage($method_name, $body, $options = array()) |
|---|
| 660 |
{ |
|---|
| 661 |
|
|---|
| 662 |
$file_name = basename($method_name); |
|---|
| 663 |
$fparts=split('\.',$file_name); |
|---|
| 664 |
$extension=array_pop($fparts); |
|---|
| 665 |
array_shift($fparts); |
|---|
| 666 |
$content_type = join('.',$fparts); |
|---|
| 667 |
$this->current_content_type = join('/',$fparts); |
|---|
| 668 |
$this->current_template_extension = $extension; |
|---|
| 669 |
|
|---|
| 670 |
if($content_type && ($layout_path = $this->_getLayoutPath($method_name, $content_type, $extension))) { |
|---|
| 671 |
$layout_options = array_merge($options, array('file' => $method_name, 'body' => $body)); |
|---|
| 672 |
$this->render(false,true,@$layout_options['body']); |
|---|
| 673 |
return $this->_renderWithALayout($layout_options, $layout_path); |
|---|
| 674 |
} |
|---|
| 675 |
return $this->render(array_merge($options, array('file' => $method_name, 'body' => $body))); |
|---|
| 676 |
} |
|---|
| 677 |
|
|---|
| 678 |
|
|---|
| 679 |
public function render($options = array(),$set_body_only=false,$set_body=null) |
|---|
| 680 |
{ |
|---|
| 681 |
static $body; |
|---|
| 682 |
if($set_body_only===true) { |
|---|
| 683 |
$body = $set_body; |
|---|
| 684 |
return; |
|---|
| 685 |
} |
|---|
| 686 |
if(isset($options['body'])) { |
|---|
| 687 |
$body = $options['body']; |
|---|
| 688 |
unset($options['body']); |
|---|
| 689 |
$Template =& $this->_initializeTemplateClass($body); |
|---|
| 690 |
} else { |
|---|
| 691 |
$Template =& $this->_initializeTemplateClass($body); |
|---|
| 692 |
} |
|---|
| 693 |
|
|---|
| 694 |
if(isset($options['partial']) && !empty($this->current_content_type)) { |
|---|
| 695 |
|
|---|
| 696 |
* choose the partial for this content-type |
|---|
| 697 |
*/ |
|---|
| 698 |
$partialExtension = $this->current_template_extension; |
|---|
| 699 |
$partialParts=split('\.',basename($options['partial'])); |
|---|
| 700 |
$partialDir=dirname($options['partial']); |
|---|
| 701 |
$contentTypePartialFilename=join('.',$partialParts).'.'. |
|---|
| 702 |
str_replace('/','.',$this->current_content_type).'.'. |
|---|
| 703 |
$partialExtension; |
|---|
| 704 |
|
|---|
| 705 |
$testContentTypePartialPath=$this->getTemplatePath().DS.$partialDir.DS.'_'.$contentTypePartialFilename; |
|---|
| 706 |
|
|---|
| 707 |
if(file_exists($testContentTypePartialPath)) { |
|---|
| 708 |
$options['partial'] = $options['partial'].'.'.str_replace('/','.',$this->current_content_type); |
|---|
| 709 |
} |
|---|
| 710 |
|
|---|
| 711 |
} |
|---|
| 712 |
$options['locals'] = array_merge((array)@$options['locals'], $this->getHelpers()); |
|---|
| 713 |
$options['locals'] = array_merge($options['locals'], array('mailer'=>&$this,'controller'=>&$this)); |
|---|
| 714 |
if(!empty($body)) { |
|---|
| 715 |
$options['locals']['body'] = $body; |
|---|
| 716 |
} |
|---|
| 717 |
return $Template->render($options); |
|---|
| 718 |
} |
|---|
| 719 |
|
|---|
| 720 |
public function getTemplatePath() |
|---|
| 721 |
{ |
|---|
| 722 |
return $this->templateRoot.DS.$this->mailerName; |
|---|
| 723 |
} |
|---|
| 724 |
|
|---|
| 725 |
|
|---|
| 726 |
* Set up the default values for the various instance variables of this |
|---|
| 727 |
* mailer. Subclasses may override this method to provide different |
|---|
| 728 |
* defaults. |
|---|
| 729 |
*/ |
|---|
| 730 |
public function initializeDefaults($method_name) |
|---|
| 731 |
{ |
|---|
| 732 |
foreach (array('charset','content_type','implicit_parts_order', 'mime_version') as $attribute) { |
|---|
| 733 |
$method = 'set'.AkInflector::camelize($attribute); |
|---|
| 734 |
$this->Message->$method(empty($this->$attribute) ? $this->{'default_'.$attribute} : $this->$attribute); |
|---|
| 735 |
} |
|---|
| 736 |
foreach (array('parts','headers','body') as $attribute) { |
|---|
| 737 |
$method = 'set'.AkInflector::camelize($attribute); |
|---|
| 738 |
$this->Message->$method(empty($this->$attribute) ? array() : $this->$attribute); |
|---|
| 739 |
} |
|---|
| 740 |
|
|---|
| 741 |
$this->templateRoot = empty($this->templateRoot) ? AK_APP_DIR.DS.'views' : $this->templateRoot; |
|---|
| 742 |
$this->template = empty($this->template) ? $method_name : $this->template; |
|---|
| 743 |
$this->mailerName = empty($this->mailerName) ? AkInflector::underscore($this->getModelName()) : $this->mailerName; |
|---|
| 744 |
} |
|---|
| 745 |
|
|---|
| 746 |
|
|---|
| 747 |
public function &_initializeTemplateClass($assigns) |
|---|
| 748 |
{ |
|---|
| 749 |
require_once(AK_LIB_DIR.DS.'AkActionView.php'); |
|---|
| 750 |
$TemplateInstance = new AkActionView($this->getTemplatePath(), $assigns, $this); |
|---|
| 751 |
require_once (AK_LIB_DIR.DS.'AkActionView'.DS.'AkPhpTemplateHandler.php'); |
|---|
| 752 |
$TemplateInstance->_registerTemplateHandler('tpl','AkPhpTemplateHandler'); |
|---|
| 753 |
return $TemplateInstance; |
|---|
| 754 |
} |
|---|
| 755 |
|
|---|
| 756 |
public function &getComposer() |
|---|
| 757 |
{ |
|---|
| 758 |
if(empty($this->Composer)){ |
|---|
| 759 |
$this->setComposer(); |
|---|
| 760 |
} |
|---|
| 761 |
return $this->Composer; |
|---|
| 762 |
} |
|---|
| 763 |
|
|---|
| 764 |
public function setComposer($Composer = null) |
|---|
| 765 |
{ |
|---|
| 766 |
if(!empty($Composer)){ |
|---|
| 767 |
$this->Composer = $Composer; |
|---|
| 768 |
}else{ |
|---|
| 769 |
$this->Composer = new AkMailComposer(); |
|---|
| 770 |
$this->Composer->init($this); |
|---|
| 771 |
} |
|---|
| 772 |
} |
|---|
| 773 |
|
|---|
| 774 |
|
|---|
| 775 |
|
|---|
| 776 |
* Alias for getModelName |
|---|
| 777 |
*/ |
|---|
| 778 |
public function getMailerName() |
|---|
| 779 |
{ |
|---|
| 780 |
return $this->getModelName(); |
|---|
| 781 |
} |
|---|
| 782 |
|
|---|
| 783 |
|
|---|
| 784 |
|
|---|
| 785 |
* Workarround for limited support of helpers on ActionMailer Views |
|---|
| 786 |
* |
|---|
| 787 |
* @todo refactor helpers to be controller agnostic |
|---|
| 788 |
*/ |
|---|
| 789 |
public function getControllerName() |
|---|
| 790 |
{ |
|---|
| 791 |
return $this->getModelName(); |
|---|
| 792 |
} |
|---|
| 793 |
|
|---|
| 794 |
|
|---|
| 795 |
* This is the url_for version for helpers and emails. |
|---|
| 796 |
* |
|---|
| 797 |
* As we do not have the context of a host being requested, we need to know |
|---|
| 798 |
* the base_url like http://example.com in oder to add it to the generated URL |
|---|
| 799 |
*/ |
|---|
| 800 |
public function urlFor() |
|---|
| 801 |
{ |
|---|
| 802 |
$args = func_get_args(); |
|---|
| 803 |
$base_url = ''; |
|---|
| 804 |
if(isset($args[0]['base_url'])){ |
|---|
| 805 |
$base_url = rtrim(preg_replace('/^(?!http[s]?:\/\/)(.+)/','http://$1', (strstr($args[0]['base_url'],'.')?$args[0]['base_url']:Ak::getSetting('mailer', 'base_url', AK_HOST))),'/'); |
|---|
| 806 |
unset($args[0]['base_url']); |
|---|
| 807 |
} |
|---|
| 808 |
|
|---|
| 809 |
unset($args[0]['only_path'], $args[0]['base_url']); |
|---|
| 810 |
|
|---|
| 811 |
return $base_url.call_user_func_array(array('Ak','toUrl'), $args); |
|---|
| 812 |
} |
|---|
| 813 |
|
|---|
| 814 |
|
|---|
| 815 |
* Creates an instance of each available helper and links it into into current mailer. |
|---|
| 816 |
* |
|---|
| 817 |
* Mailer helpers work as Controller helpers but without the Request context |
|---|
| 818 |
*/ |
|---|
| 819 |
public function getHelpers() |
|---|
| 820 |
{ |
|---|
| 821 |
require_once(AK_LIB_DIR.DS.'AkActionView'.DS.'AkHelperLoader.php'); |
|---|
| 822 |
$HelperLoader = new AkHelperLoader(); |
|---|
| 823 |
$HelperLoader->setHandler(&$this); |
|---|
| 824 |
return $HelperLoader->getHelpersForMailer(); |
|---|
| 825 |
} |
|---|
| 826 |
|
|---|
| 827 |
public function _deliverUsingMailDeliveryMethod($method, &$Message, $options) |
|---|
| 828 |
{ |
|---|
| 829 |
$handler_name = 'Ak'.AkInflector::camelize(Ak::sanitize_include($method, 'paranoid')).'Delivery'; |
|---|
| 830 |
$handler_path = AK_LIB_DIR.DS.'AkActionMailer'.DS.'AkMailDelivery'.DS.$handler_name.'.php'; |
|---|
| 831 |
if(file_exists($handler_path)){ |
|---|
| 832 |
require_once($handler_path); |
|---|
| 833 |
} |
|---|
| 834 |
|
|---|
| 835 |
if(!class_exists($handler_name)){ |
|---|
| 836 |
trigger_error(Ak::t('Could not find message handler %handler_name', array('%handler_name'=>$handler_name)), E_USER_ERROR); |
|---|
| 837 |
return false; |
|---|
| 838 |
} |
|---|
| 839 |
$DeliveryHandler = new $handler_name(); |
|---|
| 840 |
$this->Message =& $Message; |
|---|
| 841 |
return $DeliveryHandler->deliver($this, $options); |
|---|
| 842 |
} |
|---|
| 843 |
|
|---|
| 844 |
|
|---|
| 845 |
public function _getLayoutPath($method_name, $content_type = null, $extension = null) |
|---|
| 846 |
{ |
|---|
| 847 |
$dirname = dirname($method_name); |
|---|
| 848 |
if(is_file($dirname.DS.'layout.'.$content_type.'.'.$extension)){ |
|---|
| 849 |
return $dirname.DS.'layout.'.$content_type.'.'.$extension; |
|---|
| 850 |
}elseif(is_file($dirname.DS.'layout.'.$extension)){ |
|---|
| 851 |
return is_file($dirname.DS.'layout.'.$extension); |
|---|
| 852 |
} |
|---|
| 853 |
return false; |
|---|
| 854 |
} |
|---|
| 855 |
|
|---|
| 856 |
public function _renderWithALayout($options = array(), $layout_file) |
|---|
| 857 |
{ |
|---|
| 858 |
static $body; |
|---|
| 859 |
if(isset($options['body'])) { |
|---|
| 860 |
$body = $options['body']; |
|---|
| 861 |
unset($options['body']); |
|---|
| 862 |
$Template =& $this->_initializeTemplateClass($body); |
|---|
| 863 |
} else { |
|---|
| 864 |
$Template =& $this->_initializeTemplateClass($body); |
|---|
| 865 |
} |
|---|
| 866 |
$options['locals'] = array_merge((array)@$options['locals'], $this->getHelpers()); |
|---|
| 867 |
$options['locals'] = array_merge($options['locals'], array('mailer'=>&$this,'controller'=>&$this)); |
|---|
| 868 |
if(!empty($body)) { |
|---|
| 869 |
$options['locals']['body'] = $body; |
|---|
| 870 |
} else { |
|---|
| 871 |
$options['locals']['body'] = array(); |
|---|
| 872 |
} |
|---|
| 873 |
$options['locals']['body']['content_for_layout']=$Template->render($options); |
|---|
| 874 |
|
|---|
| 875 |
$layout_options=$options; |
|---|
| 876 |
$layout_options['file'] = $layout_file; |
|---|
| 877 |
$LayoutTemplate =& $this->_initializeTemplateClass($options['locals']['body']); |
|---|
| 878 |
return $LayoutTemplate->render($layout_options); |
|---|
| 879 |
} |
|---|
| 880 |
} |
|---|
| 881 |
|
|---|
| 882 |
|
|---|
| 883 |
?> |
|---|