00001 <?php
00002
00003
00004
00005
00006
00007 $INCLUDE_DIR = "../";
00008
00009 require("phpunit.php");
00010 require($INCLUDE_DIR . "class.phpmailer.php");
00011 error_reporting(E_ALL);
00012
00016 class phpmailerTest extends TestCase
00017 {
00023 var $Mail = false;
00024
00030 var $Host = "";
00031
00037 var $ChangeLog = array();
00038
00044 var $NoteLog = array();
00045
00049 function phpmailerTest($name) {
00050
00051 $this->TestCase( $name );
00052 }
00053
00057 function setUp() {
00058 global $global_vars;
00059 global $INCLUDE_DIR;
00060
00061 $this->Mail = new PHPMailer();
00062
00063 $this->Mail->Priority = 3;
00064 $this->Mail->Encoding = "8bit";
00065 $this->Mail->CharSet = "iso-8859-1";
00066 $this->Mail->From = "unit_test@phpmailer.sf.net";
00067 $this->Mail->FromName = "Unit Tester";
00068 $this->Mail->Sender = "";
00069 $this->Mail->Subject = "Unit Test";
00070 $this->Mail->Body = "";
00071 $this->Mail->AltBody = "";
00072 $this->Mail->WordWrap = 0;
00073 $this->Mail->Host = $global_vars["mail_host"];
00074 $this->Mail->Port = 25;
00075 $this->Mail->Helo = "localhost.localdomain";
00076 $this->Mail->SMTPAuth = false;
00077 $this->Mail->Username = "";
00078 $this->Mail->Password = "";
00079 $this->Mail->PluginDir = $INCLUDE_DIR;
00080 $this->Mail->AddReplyTo("no_reply@phpmailer.sf.net", "Reply Guy");
00081 $this->Mail->Sender = "unit_test@phpmailer.sf.net";
00082
00083 if(strlen($this->Mail->Host) > 0)
00084 $this->Mail->Mailer = "smtp";
00085 else
00086 {
00087 $this->Mail->Mailer = "mail";
00088 $this->Sender = "unit_test@phpmailer.sf.net";
00089 }
00090
00091 global $global_vars;
00092 $this->SetAddress($global_vars["mail_to"], "Test User");
00093 if(strlen($global_vars["mail_cc"]) > 0)
00094 $this->SetAddress($global_vars["mail_cc"], "Carbon User", "cc");
00095 }
00096
00100 function tearDown() {
00101
00102 $this->Mail = NULL;
00103 $this->ChangeLog = array();
00104 $this->NoteLog = array();
00105 }
00106
00107
00113 function BuildBody() {
00114 $this->CheckChanges();
00115
00116
00117 if($this->Mail->ContentType == "text/html" || strlen($this->Mail->AltBody) > 0)
00118 {
00119 $eol = "<br/>";
00120 $bullet = "<li>";
00121 $bullet_start = "<ul>";
00122 $bullet_end = "</ul>";
00123 }
00124 else
00125 {
00126 $eol = "\n";
00127 $bullet = " - ";
00128 $bullet_start = "";
00129 $bullet_end = "";
00130 }
00131
00132 $ReportBody = "";
00133
00134 $ReportBody .= "---------------------" . $eol;
00135 $ReportBody .= "Unit Test Information" . $eol;
00136 $ReportBody .= "---------------------" . $eol;
00137 $ReportBody .= "phpmailer version: " . $this->Mail->Version . $eol;
00138 $ReportBody .= "Content Type: " . $this->Mail->ContentType . $eol;
00139
00140 if(strlen($this->Mail->Host) > 0)
00141 $ReportBody .= "Host: " . $this->Mail->Host . $eol;
00142
00143
00144 if(count($this->Mail->attachment) > 0)
00145 {
00146 $ReportBody .= "Attachments:" . $eol;
00147 $ReportBody .= $bullet_start;
00148 for($i = 0; $i < count($this->Mail->attachment); $i++)
00149 {
00150 $ReportBody .= $bullet . "Name: " . $this->Mail->attachment[$i][1] . ", ";
00151 $ReportBody .= "Encoding: " . $this->Mail->attachment[$i][3] . ", ";
00152 $ReportBody .= "Type: " . $this->Mail->attachment[$i][4] . $eol;
00153 }
00154 $ReportBody .= $bullet_end . $eol;
00155 }
00156
00157
00158 if(count($this->ChangeLog) > 0)
00159 {
00160 $ReportBody .= "Changes" . $eol;
00161 $ReportBody .= "-------" . $eol;
00162
00163 $ReportBody .= $bullet_start;
00164 for($i = 0; $i < count($this->ChangeLog); $i++)
00165 {
00166 $ReportBody .= $bullet . $this->ChangeLog[$i][0] . " was changed to [" .
00167 $this->ChangeLog[$i][1] . "]" . $eol;
00168 }
00169 $ReportBody .= $bullet_end . $eol . $eol;
00170 }
00171
00172
00173 if(count($this->NoteLog) > 0)
00174 {
00175 $ReportBody .= "Notes" . $eol;
00176 $ReportBody .= "-----" . $eol;
00177
00178 $ReportBody .= $bullet_start;
00179 for($i = 0; $i < count($this->NoteLog); $i++)
00180 {
00181 $ReportBody .= $bullet . $this->NoteLog[$i] . $eol;
00182 }
00183 $ReportBody .= $bullet_end;
00184 }
00185
00186
00187 $this->Mail->Body .= $eol . $eol . $ReportBody;
00188 }
00189
00195 function CheckChanges() {
00196 if($this->Mail->Priority != 3)
00197 $this->AddChange("Priority", $this->Mail->Priority);
00198 if($this->Mail->Encoding != "8bit")
00199 $this->AddChange("Encoding", $this->Mail->Encoding);
00200 if($this->Mail->CharSet != "iso-8859-1")
00201 $this->AddChange("CharSet", $this->Mail->CharSet);
00202 if($this->Mail->Sender != "")
00203 $this->AddChange("Sender", $this->Mail->Sender);
00204 if($this->Mail->WordWrap != 0)
00205 $this->AddChange("WordWrap", $this->Mail->WordWrap);
00206 if($this->Mail->Mailer != "mail")
00207 $this->AddChange("Mailer", $this->Mail->Mailer);
00208 if($this->Mail->Port != 25)
00209 $this->AddChange("Port", $this->Mail->Port);
00210 if($this->Mail->Helo != "localhost.localdomain")
00211 $this->AddChange("Helo", $this->Mail->Helo);
00212 if($this->Mail->SMTPAuth)
00213 $this->AddChange("SMTPAuth", "true");
00214 }
00215
00221 function AddChange($sName, $sNewValue) {
00222 $cur = count($this->ChangeLog);
00223 $this->ChangeLog[$cur][0] = $sName;
00224 $this->ChangeLog[$cur][1] = $sNewValue;
00225 }
00226
00232 function AddNote($sValue) {
00233 $this->NoteLog[] = $sValue;
00234 }
00235
00241 function SetAddress($sAddress, $sName = "", $sType = "to") {
00242 switch($sType)
00243 {
00244 case "to":
00245 $this->Mail->AddAddress($sAddress, $sName);
00246 break;
00247 case "cc":
00248 $this->Mail->AddCC($sAddress, $sName);
00249 break;
00250 case "bcc":
00251 $this->Mail->AddBCC($sAddress, $sName);
00252 break;
00253 }
00254 }
00255
00257
00259
00263 function test_WordWrap() {
00264
00265 $this->Mail->WordWrap = 40;
00266 $my_body = "Here is the main body of this message. It should " .
00267 "be quite a few lines. It should be wrapped at the " .
00268 "40 characters. Make sure that it is.";
00269 $nBodyLen = strlen($my_body);
00270 $my_body .= "\n\nThis is the above body length: " . $nBodyLen;
00271
00272 $this->Mail->Body = $my_body;
00273 $this->Mail->Subject .= ": Wordwrap";
00274
00275 $this->BuildBody();
00276 $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
00277 }
00278
00282 function test_Low_Priority() {
00283
00284 $this->Mail->Priority = 5;
00285 $this->Mail->Body = "Here is the main body. There should be " .
00286 "a reply to address in this message.";
00287 $this->Mail->Subject .= ": Low Priority";
00288 $this->Mail->AddReplyTo("nobody@nobody.com", "Nobody (Unit Test)");
00289
00290 $this->BuildBody();
00291 $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
00292 }
00293
00297 function test_Multiple_Plain_FileAttachment() {
00298
00299 $this->Mail->Body = "Here is the text body";
00300 $this->Mail->Subject .= ": Plain + Multiple FileAttachments";
00301
00302 if(!$this->Mail->AddAttachment("test.png"))
00303 {
00304 $this->assert(false, $this->Mail->ErrorInfo);
00305 return;
00306 }
00307
00308 if(!$this->Mail->AddAttachment("phpmailer_test.php", "test.txt"))
00309 {
00310 $this->assert(false, $this->Mail->ErrorInfo);
00311 return;
00312 }
00313
00314 $this->BuildBody();
00315 $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
00316 }
00317
00321 function test_Plain_StringAttachment() {
00322
00323 $this->Mail->Body = "Here is the text body";
00324 $this->Mail->Subject .= ": Plain + StringAttachment";
00325
00326 $sAttachment = "These characters are the content of the " .
00327 "string attachment.\nThis might be taken from a ".
00328 "database or some other such thing. ";
00329
00330 $this->Mail->AddStringAttachment($sAttachment, "string_attach.txt");
00331
00332 $this->BuildBody();
00333 $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
00334 }
00335
00339 function test_Quoted_Printable() {
00340
00341 $this->Mail->Body = "Here is the main body";
00342 $this->Mail->Subject .= ": Plain + Quoted-printable";
00343 $this->Mail->Encoding = "quoted-printable";
00344
00345 $this->BuildBody();
00346 $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
00347 }
00348
00352 function test_Html() {
00353
00354 $this->Mail->IsHTML(true);
00355 $this->Mail->Subject .= ": HTML only";
00356
00357 $this->Mail->Body = "This is a <b>test message</b> written in HTML. </br>" .
00358 "Go to <a href=\"http://phpmailer.sourceforge.net/\">" .
00359 "http://phpmailer.sourceforge.net/</a> for new versions of " .
00360 "phpmailer. <p/> Thank you!";
00361
00362 $this->BuildBody();
00363 $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
00364 }
00365
00369 function test_HTML_Attachment() {
00370
00371 $this->Mail->Body = "This is the <b>HTML</b> part of the email.";
00372 $this->Mail->Subject .= ": HTML + Attachment";
00373 $this->Mail->IsHTML(true);
00374
00375 if(!$this->Mail->AddAttachment("phpmailer_test.php", "test_attach.txt"))
00376 {
00377 $this->assert(false, $this->Mail->ErrorInfo);
00378 return;
00379 }
00380
00381 $this->BuildBody();
00382 $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
00383 }
00384
00388 function test_Embedded_Image() {
00389
00390 $this->Mail->Body = "Embedded Image: <img alt=\"phpmailer\" src=\"cid:my-attach\">" .
00391 "Here is an image!</a>";
00392 $this->Mail->Subject .= ": Embedded Image";
00393 $this->Mail->IsHTML(true);
00394
00395 if(!$this->Mail->AddEmbeddedImage("test.png", "my-attach", "test.png",
00396 "base64", "image/png"))
00397 {
00398 $this->assert(false, $this->Mail->ErrorInfo);
00399 return;
00400 }
00401
00402 $this->BuildBody();
00403 $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
00404 }
00405
00409 function test_Multi_Embedded_Image() {
00410
00411 $this->Mail->Body = "Embedded Image: <img alt=\"phpmailer\" src=\"cid:my-attach\">" .
00412 "Here is an image!</a>";
00413 $this->Mail->Subject .= ": Embedded Image + Attachment";
00414 $this->Mail->IsHTML(true);
00415
00416 if(!$this->Mail->AddEmbeddedImage("test.png", "my-attach", "test.png",
00417 "base64", "image/png"))
00418 {
00419 $this->assert(false, $this->Mail->ErrorInfo);
00420 return;
00421 }
00422
00423 if(!$this->Mail->AddAttachment("phpmailer_test.php", "test.txt"))
00424 {
00425 $this->assert(false, $this->Mail->ErrorInfo);
00426 return;
00427 }
00428
00429 $this->BuildBody();
00430 $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
00431 }
00432
00436 function test_AltBody() {
00437
00438 $this->Mail->Body = "This is the <b>HTML</b> part of the email.";
00439 $this->Mail->AltBody = "Here is the text body of this message. " .
00440 "It should be quite a few lines. It should be wrapped at the " .
00441 "40 characters. Make sure that it is.";
00442 $this->Mail->WordWrap = 40;
00443 $this->AddNote("This is a mulipart alternative email");
00444 $this->Mail->Subject .= ": AltBody + Word Wrap";
00445
00446 $this->BuildBody();
00447 $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
00448 }
00449
00453 function test_AltBody_Attachment() {
00454
00455 $this->Mail->Body = "This is the <b>HTML</b> part of the email.";
00456 $this->Mail->AltBody = "This is the text part of the email.";
00457 $this->Mail->Subject .= ": AltBody + Attachment";
00458 $this->Mail->IsHTML(true);
00459
00460 if(!$this->Mail->AddAttachment("phpmailer_test.php", "test_attach.txt"))
00461 {
00462 $this->assert(false, $this->Mail->ErrorInfo);
00463 return;
00464 }
00465
00466 $this->BuildBody();
00467 $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
00468
00469 $fp = fopen("message.txt", "w");
00470 fwrite($fp, $this->Mail->CreateHeader() . $this->Mail->CreateBody());
00471 fclose($fp);
00472 }
00473
00474 function test_MultipleSend() {
00475 $this->Mail->Body = "Sending two messages without keepalive";
00476 $this->BuildBody();
00477 $subject = $this->Mail->Subject;
00478
00479 $this->Mail->Subject = $subject . ": SMTP 1";
00480 $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
00481
00482 $this->Mail->Subject = $subject . ": SMTP 2";
00483 $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
00484 }
00485
00486 function test_SmtpKeepAlive() {
00487 $this->Mail->Body = "This was done using the SMTP keep-alive.";
00488 $this->BuildBody();
00489 $subject = $this->Mail->Subject;
00490
00491 $this->Mail->SMTPKeepAlive = true;
00492 $this->Mail->Subject = $subject . ": SMTP keep-alive 1";
00493 $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
00494
00495 $this->Mail->Subject = $subject . ": SMTP keep-alive 2";
00496 $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
00497 $this->Mail->SmtpClose();
00498 }
00499
00504 function test_DenialOfServiceAttack() {
00505 $this->Mail->Body = "This should no longer cause a denial of service.";
00506 $this->BuildBody();
00507
00508 $this->Mail->Subject = str_repeat("A", 998);
00509 $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
00510 }
00511
00512 function test_Error() {
00513 $this->Mail->Subject .= ": This should be sent";
00514 $this->BuildBody();
00515 $this->Mail->ClearAllRecipients();
00516 $this->assert($this->Mail->IsError() == false, "Error found");
00517 $this->assert($this->Mail->Send() == false, "Send succeeded");
00518 $this->assert($this->Mail->IsError(), "No error found");
00519 $this->assertEquals('You must provide at least one ' .
00520 'recipient email address.', $this->Mail->ErrorInfo);
00521 $this->Mail->AddAddress(get("mail_to"));
00522 $this->assert($this->Mail->Send(), "Send failed");
00523 }
00524 }
00525
00530 if(isset($HTTP_GET_VARS))
00531 $global_vars = $HTTP_GET_VARS;
00532 else
00533 $global_vars = $_REQUEST;
00534
00535 if(isset($global_vars["submitted"]))
00536 {
00537 echo "Test results:<br>";
00538 $suite = new TestSuite( "phpmailerTest" );
00539
00540 $testRunner = new TestRunner;
00541 $testRunner->run($suite);
00542 echo "<hr noshade/>";
00543 }
00544
00545 function get($sName) {
00546 global $global_vars;
00547 if(isset($global_vars[$sName]))
00548 return $global_vars[$sName];
00549 else
00550 return "";
00551 }
00552
00553 ?>
00554
00555 <html>
00556 <body>
00557 <h3>phpmailer Unit Test</h3>
00558 By entering a SMTP hostname it will automatically perform tests with SMTP.
00559
00560 <form name="phpmailer_unit" action="phpmailer_test.php" method="get">
00561 <input type="hidden" name="submitted" value="1"/>
00562 To Address: <input type="text" size="50" name="mail_to" value="<?php echo get("mail_to"); ?>"/>
00563 <br/>
00564 Cc Address: <input type="text" size="50" name="mail_cc" value="<?php echo get("mail_cc"); ?>"/>
00565 <br/>
00566 SMTP Hostname: <input type="text" size="50" name="mail_host" value="<?php echo get("mail_host"); ?>"/>
00567 <p/>
00568 <input type="submit" value="Run Test"/>
00569
00570 </form>
00571 </body>
00572 </html>