Changeset 972
- Timestamp:
- 07/25/08 17:25:47 (1 month ago)
- Files:
-
- branches/action_mailer/app/models/notifications.php (deleted)
- branches/action_mailer/app/views/notifications (deleted)
- branches/action_mailer/config/mailer.yml (modified) (1 diff)
- branches/action_mailer/lib/Ak.php (modified) (1 diff)
- branches/action_mailer/lib/AkActionMailer.php (modified) (2 diffs)
- branches/action_mailer/lib/AkActionMailer/AkMailBase.php (modified) (7 diffs)
- branches/action_mailer/lib/AkActionMailer/AkMailComposer.php (modified) (5 diffs)
- branches/action_mailer/lib/AkActionMailer/AkMailDelivery/AkPhpMailDelivery.php (modified) (1 diff)
- branches/action_mailer/lib/AkActionMailer/AkMailEncoder.php (deleted)
- branches/action_mailer/lib/AkActionMailer/AkMailHeader.php (deleted)
- branches/action_mailer/lib/AkActionMailer/AkMailMessage.php (modified) (2 diffs)
- branches/action_mailer/test/fixtures/app/models/notifications.php (deleted)
- branches/action_mailer/test/fixtures/app/models/test_mailer.php (modified) (2 diffs)
- branches/action_mailer/test/fixtures/app/views/test_mailer/alternative_message_from_templates.text.html.tpl (modified) (1 diff)
- branches/action_mailer/test/unit/lib/AkActionMailer.php (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/action_mailer/config/mailer.yml
r887 r972 3 3 port: 587 4 4 domain: localhost 5 user_name: testing@bermilabs.com6 password: 221582T7775 user_name: username@gmail.com 6 password: 1234 7 7 authentication: login branches/action_mailer/lib/Ak.php
r846 r972 1317 1317 return $models; 1318 1318 } 1319 1320 function import_mailer() 1321 { 1322 require_once(AK_LIB_DIR.DS.'AkActionMailer.php'); 1323 $args = func_get_args(); 1324 return call_user_func_array(array('Ak','import'),$args); 1325 } 1319 1326 1320 1327 function uses() branches/action_mailer/lib/AkActionMailer.php
r946 r972 236 236 * Options are: plain, login, cram_md5 237 237 * 238 * * <tt>delivery_method</tt> - Defines a delivery method. Possible values are ' smtp' (default), 'php', and 'test'.238 * * <tt>delivery_method</tt> - Defines a delivery method. Possible values are 'php' (default), 'smtp', and 'test'. 239 239 * 240 240 * * <tt>perform_deliveries</tt> - Determines whether AkActionMailer::deliver(*) methods are actually carried out. By default they are, … … 268 268 ); 269 269 270 var $delivery_method = ' smtp';270 var $delivery_method = 'php'; 271 271 var $perform_deliveries = true; 272 272 var $deliveries = array(); branches/action_mailer/lib/AkActionMailer/AkMailBase.php
r950 r972 13 13 var $parts = array(); 14 14 var $attachments = array(); 15 16 var $ attach_html_images = true;15 16 var $_attach_html_images = true; 17 17 18 18 function AkMailBase() … … 52 52 $content_type = @$this->content_type; 53 53 $this->body = stristr($content_type,'text/') ? str_replace(array("\r\n","\r"),"\n", $body) : $body; 54 if($this-> attach_html_images && $content_type == 'text/html'){54 if($this->_attach_html_images && $content_type == 'text/html'){ 55 55 $Parser = new AkMailParser(); 56 56 $Parser->extractImagesIntoInlineParts($this); … … 138 138 return empty($this->content_type) ? ($this->isMultipart()?'multipart/alternative':null) : $this->content_type.$this->getContenttypeAttributes(); 139 139 } 140 140 141 141 function hasContentType() 142 142 { … … 212 212 } 213 213 } 214 214 215 215 if (!empty($this->_header_attributes_set_for[$header_index])){ 216 216 return $this->_header_attributes_set_for[$header_index]; … … 320 320 $this->loadHeaders(); 321 321 $this->_addHeaderAttributes(); 322 322 323 323 } 324 324 return $this->headers; … … 527 527 unset($this->$k); 528 528 } 529 529 530 $this->setAsMultipart(); 531 $this->setPart($options, 'preppend'); 532 } 533 534 function setAsMultipart() 535 { 530 536 $this->_multipart_message = true; 531 $this->setPart($options, 'preppend');532 537 } 533 538 … … 600 605 $this->setPart($options); 601 606 } 602 607 603 608 function setAttachments($attachments = array()) 604 609 { branches/action_mailer/lib/AkActionMailer/AkMailComposer.php
r950 r972 29 29 30 30 31 function getRawMessage($MessageOrPart = null, $force_overload = false) 32 { 33 $Message = empty($MessageOrPart) ? $this->Message : $MessageOrPart; 34 if($force_overload || empty($Message->raw_message)){ 35 list($raw_headers, $raw_body) = $this->getRawHeadersAndBody($Message); 36 $Message->raw_message = $raw_headers. 37 AK_ACTION_MAILER_EOL.AK_ACTION_MAILER_EOL. 38 $raw_body; 39 } 40 return $Message->raw_message; 41 } 42 43 44 function getRawHeadersAndBody($MessageOrPart = null) 45 { 46 $Message = empty($MessageOrPart) ? $this->Message : $MessageOrPart; 47 $raw_body_or_parts = $this->getRawBodyOrRawParts($Message); 48 49 if(is_array($raw_body_or_parts)){ 50 $raw_body = ''; 51 $this->openMultipartBlock(); 52 if(!$Message->hasContentType()){ 53 $Message->setContentType('multipart/related'); 54 } 55 $Message->content_type_attributes['boundary'] = $this->getBoundary(); 56 $Message->_skip_adding_date_to_headers = !$Message->isMainMessage(); 57 58 $raw_headers = $Message->getRawHeaders(); 59 foreach ($raw_body_or_parts as $raw_part_headers=>$raw_part_body){ 60 $raw_body .= 61 AK_ACTION_MAILER_EOL. 62 AK_ACTION_MAILER_EOL. 63 '--'. 64 $this->getBoundary(). 65 AK_ACTION_MAILER_EOL. 66 $raw_part_headers. 67 AK_ACTION_MAILER_EOL. 68 AK_ACTION_MAILER_EOL. 69 $raw_part_body; 70 } 71 $raw_body .= AK_ACTION_MAILER_EOL.'--'.$this->getBoundary().'--'.AK_ACTION_MAILER_EOL; 72 73 $this->closeMultipartBlock(); 74 }else{ 75 $raw_headers = $Message->getRawHeaders(); 76 $raw_body = $raw_body_or_parts; 77 } 78 79 return array($raw_headers, $raw_body); 80 } 81 82 83 function getRawBodyOrRawParts($MessageOrPart = null) 84 { 85 $Message = empty($MessageOrPart) ? $this->Message : $MessageOrPart; 86 $body = $Message->getBody(); 87 if(empty($body) && ($Message->hasParts() || $Message->hasAttachments())){ 88 $result = array(); 89 foreach (array_keys($Message->parts) as $k){ 90 $Part = $Message->parts[$k]; 91 list($raw_headers, $raw_body) = $this->getRawHeadersAndBody($Part); 92 $result[$raw_headers] = $raw_body; 93 } 94 return $result; 95 } 96 return $body; 97 } 98 99 function openMultipartBlock() 100 { 101 $this->setBoundary($this->getBoundaryString()); 102 } 103 104 function closeMultipartBlock() 105 { 106 $this->latest_closed_boundary = array_pop($this->_boundary_stack); 107 } 108 109 110 function setBoundary($boundary) 111 { 112 $this->boundary = $boundary; 113 array_push($this->_boundary_stack, $boundary); 114 return $this->boundary; 115 } 116 117 function getBoundary() 118 { 119 return $this->boundary; 120 } 121 122 123 function getBoundaryString() 124 { 125 return md5(Ak::randomString(10).time()); 126 } 127 128 129 130 31 131 function _callActionMailerMethod($method_name, $params = array()) 32 132 { … … 92 192 { 93 193 $result = empty($this->Message->parts); 94 if(!$result && empty($this->Message->implicit_parts_order) && $this->_has Template()){194 if(!$result && empty($this->Message->implicit_parts_order) && $this->_hasIndividualTemplate()){ 95 195 $result = true; 96 196 } … … 99 199 100 200 101 102 function _hasTemplate() 201 function _hasIndividualTemplate() 103 202 { 104 203 $templates = $this->_getAvailableTemplates(); … … 114 213 115 214 function &_getPartsWithRenderedTemplates() 116 { 215 { 117 216 $templates = $this->_getAvailableTemplates(); 118 217 $alternative_multiparts = array(); … … 144 243 145 244 146 147 148 149 function getRawMessage($MessageOrPart = null, $force_overload = false)150 {151 $Message = empty($MessageOrPart) ? $this->Message : $MessageOrPart;152 if($force_overload || empty($Message->raw_message)){153 list($raw_headers, $raw_body) = $this->getRawHeadersAndBody($Message);154 $Message->raw_message = $raw_headers.155 AK_ACTION_MAILER_EOL.AK_ACTION_MAILER_EOL.156 $raw_body;157 }158 return $Message->raw_message;159 }160 161 162 163 function getRawHeadersAndBody($MessageOrPart = null)164 {165 $Message = empty($MessageOrPart) ? $this->Message : $MessageOrPart;166 $raw_body_or_parts = $this->getRawBodyOrRawParts($Message);167 168 if(is_array($raw_body_or_parts)){169 $raw_body = '';170 $this->openMultipartBlock();171 if(!$Message->hasContentType()){172 $Message->setContentType('multipart/related');173 }174 $Message->content_type_attributes['boundary'] = $this->getBoundary();175 $Message->_skip_adding_date_to_headers = !$Message->isMainMessage();176 177 /*178 if($Message->isMainMessage()){179 $Message->setContentType('multipart/mixed');180 }181 */182 183 $raw_headers = $Message->getRawHeaders();184 foreach ($raw_body_or_parts as $raw_part_headers=>$raw_part_body){185 $raw_body .=186 AK_ACTION_MAILER_EOL.187 AK_ACTION_MAILER_EOL.188 '--'.189 $this->getBoundary().190 AK_ACTION_MAILER_EOL.191 $raw_part_headers.192 AK_ACTION_MAILER_EOL.193 AK_ACTION_MAILER_EOL.194 $raw_part_body;195 }196 $raw_body .= AK_ACTION_MAILER_EOL.'--'.$this->getBoundary().'--'.AK_ACTION_MAILER_EOL;197 198 $this->closeMultipartBlock();199 }else{200 $raw_headers = $Message->getRawHeaders();201 $raw_body = $raw_body_or_parts;202 }203 204 return array($raw_headers, $raw_body);205 }206 207 208 function getRawBodyOrRawParts($MessageOrPart = null)209 {210 $Message = empty($MessageOrPart) ? $this->Message : $MessageOrPart;211 $body = $Message->getBody();212 if(empty($body) && $Message->hasParts()){213 $result = array();214 foreach (array_keys($Message->parts) as $k){215 $Part = $Message->parts[$k];216 list($raw_headers, $raw_body) = $this->getRawHeadersAndBody($Part);217 $result[$raw_headers] = $raw_body;218 }219 return $result;220 }221 return $body;222 }223 224 function openMultipartBlock()225 {226 $this->setBoundary($this->getBoundaryString());227 }228 229 function closeMultipartBlock()230 {231 $this->latest_closed_boundary = array_pop($this->_boundary_stack);232 }233 234 235 function setBoundary($boundary)236 {237 $this->boundary = $boundary;238 array_push($this->_boundary_stack, $boundary);239 return $this->boundary;240 }241 242 function getBoundary()243 {244 return $this->boundary;245 }246 247 248 function getBoundaryString()249 {250 return md5(Ak::randomString(10).time());251 }252 253 254 255 256 257 258 259 260 function compose(&$Mail)261 {262 $raw_message = '';263 if(empty($this->parts)){264 if(!empty($Mail->_isPart)){265 $raw_message .= $Mail->getRawPart();266 }else{267 $raw_message .= $this->getRawHeaders($Mail).AK_ACTION_MAILER_EOL.AK_ACTION_MAILER_EOL.$Mail->getBody();268 }269 }else{270 $boundary = $Mail->getBoundary();271 272 $Mail->content_type_attributes['boundary'] = $boundary;273 $raw_message .= $Mail->getRawHeaders();274 275 foreach (array_keys($Mail->parts) as $k){276 $raw_message .= AK_ACTION_MAILER_EOL.AK_ACTION_MAILER_EOL.'--'.$boundary.AK_ACTION_MAILER_EOL.$this->composePart($Mail->parts[$k]);277 }278 279 $raw_message .= AK_ACTION_MAILER_EOL.'--'.$boundary.'--'.AK_ACTION_MAILER_EOL;280 }281 282 //_propagateMultipartParts283 284 return $raw_message;285 }286 287 288 /*289 290 function create($Mailer, $method_name, $parameters, $content_type)291 {292 $args = func_get_args();293 294 $this->_initializeDefaults($method_name);295 if(method_exists($this, $method_name)){296 call_user_func_array(array(&$this, $method_name), $args);297 }else{298 trigger_error(Ak::t('Could not find the method %method on the model %model', array('%method'=>$method_name, '%model'=>$this->getModelName())), E_USER_ERROR);299 }300 $parameters = @array_shift($args);301 302 $Mail =& $this->_MailDriver;303 304 $this->_prepareInlineBodyParts($Mail);305 306 $Mail->setMimeVersion((empty($Mail->mime_version) && !empty($Mail->parts)) ? '1.0' : $Mail->mime_version);307 308 $this->Mail =& $Mail;309 return $Mail;310 }311 312 313 function composePart($Part)314 {315 return $this->compose($Part);316 }317 318 319 320 function getMultipartMessage()321 {322 $raw_message = '';323 $boundary = $this->getBoundary();324 325 $this->content_type_attributes['boundary'] = $boundary;326 $this->_skip_adding_date_to_headers = true;327 $raw_message .= $this->getRawHeaders();328 329 foreach (array_keys($this->parts) as $k){330 $raw_message .= AK_ACTION_MAILER_EOL.AK_ACTION_MAILER_EOL.'--'.$boundary.AK_ACTION_MAILER_EOL.$this->parts[$k]->getRawMessage();331 }332 333 $raw_message .= AK_ACTION_MAILER_EOL.'--'.$boundary.'--'.AK_ACTION_MAILER_EOL;334 335 }336 337 338 339 */340 341 342 245 } 343 246 branches/action_mailer/lib/AkActionMailer/AkMailDelivery/AkPhpMailDelivery.php
r946 r972 7 7 { 8 8 $Message =& $Mailer->Message; 9 return mail($Message->getTo(), $Message->getSubject(), $Message->bodyToString(), $Message->_getHeadersAsText()); 9 $to = $Message->getTo(); 10 $subject = $Message->getSubject(); 11 12 list($header, $body) = $Message->getRawHeadersAndBody(); 13 14 $header = preg_replace('/(To|Subject): [^\r]+\r\n/', '', $header); 15 16 return mail($to, $subject, $body, $header); 10 17 } 11 18 } branches/action_mailer/lib/AkActionMailer/AkMailMessage.php
r946 r972 19 19 20 20 require_once(AK_LIB_DIR.DS.'AkActionMailer'.DS.'AkMailBase.php'); 21 require_once(AK_LIB_DIR.DS.'AkActionMailer'.DS.'AkMailHeader.php');22 21 require_once(AK_LIB_DIR.DS.'AkActionMailer'.DS.'AkMailPart.php'); 23 22 require_once(AK_LIB_DIR.DS.'AkActionMailer'.DS.'AkMailParser.php'); … … 134 133 return AkMailComposer::getRawMessage($this); 135 134 } 135 136 function getRawHeadersAndBody() 137 { 138 $Composer =& new AkMailComposer(); 139 return $Composer->getRawHeadersAndBody($this); 140 } 136 141 } 137 142 branches/action_mailer/test/fixtures/app/models/test_mailer.php
r946 r972 307 307 308 308 309 function multipart_rendering() 310 { 311 $this->set(array( 312 'recipients' => "bermi@bermilabs.com", 313 'subject' => "Multipart rendering", 314 'from' => "testing@bermilabs.com", 315 'attachment' => array('content_type' => 'audio/mp3', 'filename' => 'test.mp3', 'body' => file_get_contents(AK_BASE_DIR.DS.'test/fixtures/data/action_mailer/testing.mp3')), 316 )); 317 318 return ; 319 320 $this->set(array( 321 'recipients' => "no.one@example.com", 322 'subject' => "Multipart rendering", 323 'from' => "some.one@example.com" 324 )); 325 } 326 327 function alternative_message_from_templates($recipient, $include_logo = false) 309 310 function alternative_message_from_templates($recipient, $include_logo = false, $include_external_image = false, $add_attachment = false) 328 311 { 329 312 $this->set(array( … … 331 314 'subject' => "Alternative message from template", 332 315 'from' => "some.one@example.com", 333 'body' => array('include_logo' => $include_logo) 334 )); 316 'body' => array('include_logo' => $include_logo, 'include_external_image' => $include_external_image) 317 )); 318 319 320 /** 321 * @todo Adding an attachment overides rendered views 322 */ 323 if($add_attachment){ 324 325 $this->addAttachment(array( 326 327 'content_type' => 'text/php', 328 'body' => '<?php ?>', 329 'filename' => 'test_mailer.php' 330 331 )); 332 } 333 335 334 } 336 335 branches/action_mailer/test/fixtures/app/views/test_mailer/alternative_message_from_templates.text.html.tpl
r946 r972 4 4 <h1>Rendered as HTML</h1> 5 5 6 {?include_logo} 6 7 <p>Akelos framework Logo</p> 7 8 8 {?include_logo}9 9 <img src="/images/akelos_framework_logo.png" /> 10 10 {end} 11 <hidden> 11 12 {?include_external_image} 13 14 <p>BermiLabs Logo</p> 15 12 16 <img src="http://www.bermilabs.com/images/bermilabs_logo.png" /> 13 </hidden> 17 18 {end} 14 19 15 20 </body> branches/action_mailer/test/unit/lib/AkActionMailer.php
r950 r972 5 5 require_once(dirname(__FILE__).'/../../fixtures/config/config.php'); 6 6 7 require_once(AK_LIB_DIR.DS.'AkActionMailer.php'); 8 9 Ak::import('render_mailer,first_mailer,second_mailer,helper_mailer,test_mailer'); 7 Ak::import_mailer('render_mailer,first_mailer,second_mailer,helper_mailer,test_mailer'); 10 8 11 9 class Tests_for_Mailers extends AkUnitTest … … 178 176 $this->Mailer->deliveries = array(); 179 177 $this->recipient = 'test@localhost'; 180 $this->recipient = 'bermi@bermilabs.com'; 181 } 182 183 184 185 /**/ 178 } 179 180 186 181 function test_nested_parts() 187 182 { … … 744 739 $rendered_message = $TestMailer->getRawMessage(); 745 740 746 741 747 742 $this->assertPattern( '/Content-Type: multipart\/alternative;charset=UTF-8;boundary=[a-f0-9]{32}\r\n'. 748 743 'Mime-Version: 1.0\r\n'. … … 754 749 $this->assertPattern('/--[a-f0-9]{32}--/', $rendered_message); 755 750 } 756 /**/ 751 757 752 function test_should_encode_alternative_message_from_templates_with_embeded_images() 758 753 { … … 760 755 $Message = $TestMailer->create('alternative_message_from_templates', $this->recipient, true); 761 756 762 //$TestMailer->delivery_method = 'smtp';763 764 $TestMailer->deliver($Message);765 766 757 $rendered_message = $TestMailer->getRawMessage(); 767 768 //echo $rendered_message; 769 return ; 770 771 $this->assertPattern( '/Content-Type: multipart\/alternative;charset=UTF-8;boundary=[a-f0-9]{32}\r\n'. 772 'Mime-Version: 1.0\r\n'. 773 'Subject:/', $rendered_message); 774 $this->assertPattern('/To:/', $rendered_message); 775 $this->assertPattern('/Date:/', $rendered_message); 776 $this->assertPattern('/Content-Type: multipart\/alternative;charset=UTF-8/', $rendered_message); 777 $this->assertPattern('/--[a-f0-9]{32}\r\nContent-Type: text\/plain;charset=UTF-8\r\nContent-Disposition: inline/', $rendered_message); 778 $this->assertPattern('/--[a-f0-9]{32}\r\nContent-Type: text\/html;charset=UTF-8\r\nContent-Disposition: inline/', $rendered_message); 779 $this->assertPattern('/--[a-f0-9]{32}--/', $rendered_message); 780 781 782 } 783 784 /**/ 758 759 $this->assertPattern('/==\r\n--[a-f0-9]{32}--\r\n\r\n--[a-f0-9]{32}--\r\n$/', $rendered_message, 'Closing 2 boundaries'); 760 $this->assertPattern('/([A-Za-z0-9\/+]{76}\r\n){50,}/', $rendered_message, 'large base64 encoded file'); 761 762 763 $this->assertPattern( 764 '/<\/html>\r\n\r\n--[a-f0-9]{32}\r\n'. 765 'Content-Type: image\/png;name=([^\.]{20,})\.png\r\n'. 766 'Content-Transfer-Encoding: base64\r\n'. 767 'Content-Id: <\\1\.png>\r\n'. 768 'Content-Disposition: inline;filename=\\1\.png\r\n'. 769 '\r\n[A-Za-z0-9\/+]{76}/', $rendered_message, 'inline image headers'); 770 771 $this->assertPattern('/<img src=3D"cid:([^\.]{20,})\.png" \/>/', $rendered_message, 'Image src pointing to cid'); 772 773 774 $this->assertPattern('/--([a-f0-9]{32})\r\n'. 775 'Content-Type: text\/plain;charset=UTF-8\r\n'. 776 'Content-Transfer-Encoding: quoted-printable\r\n'. 777 'Content-Disposition: inline\r\n\r\n'. 778 'Rendered as Text\r\n\r\n'. 779 '--\\1\r\n'. 780 'Content-Type: multipart\/related;charset=UTF-8;boundary=([a-f0-9]{32})\r\n\r\n\r\n\r\n'. 781 '--\\2\r\n'. 782 'Content-Type: text\/html;charset=UTF-8\r\n'. 783 'Content-Transfer-Encoding: quoted-printable\r\n'. 784 'Content-Disposition: inline\r\n\r\n'. 785 '<html>/', $rendered_message, 'Multipart nesting'); 786 787 $this->assertPattern('/Content-Type: multipart\/alternative;charset=UTF-8;boundary=[a-f0-9]{32}\r\nMime-Version: 1.0/', $rendered_message, 'main headers'); 788 789 790 } 791 792 function test_should_encode_alternative_message_from_templates_with_external_embeded_images() 793 { 794 $TestMailer =& new TestMailer(); 795 $Message = $TestMailer->create('alternative_message_from_templates', $this->recipient, true, true); 796 //$TestMailer->delivery_method = 'php'; 797 //$TestMailer->deliver($Message); 798 $rendered_message = $TestMailer->getRawMessage(); 799 800 $this->assertPattern('/==\r\n\r\n--[a-f0-9]{32}\r\nContent-Type: image\/png;/', $rendered_message, 'Two images embeded'); 801 } 802 785 803 } 786 804
