4 Type: phpmailer class
\r
5 ********************/
\r
7 $INCLUDE_DIR = "../";
\r
9 require("phpunit.php");
\r
10 require($INCLUDE_DIR . "class.phpmailer.php");
\r
11 error_reporting(E_ALL);
\r
14 * Performs authentication tests
\r
16 class phpmailerTest extends TestCase
\r
19 * Holds the default phpmailer instance.
\r
26 * Holds the SMTP mail host.
\r
33 * Holds the change log.
\r
35 * @type string array
\r
37 var $ChangeLog = array();
\r
40 * Holds the note log.
\r
42 * @type string array
\r
44 var $NoteLog = array();
\r
49 function phpmailerTest($name) {
\r
50 /* must define this constructor */
\r
51 $this->TestCase( $name );
\r
55 * Run before each test is started.
\r
58 global $global_vars;
\r
59 global $INCLUDE_DIR;
\r
61 $this->Mail = new PHPMailer();
\r
63 $this->Mail->Priority = 3;
\r
64 $this->Mail->Encoding = "8bit";
\r
65 $this->Mail->CharSet = "iso-8859-1";
\r
66 $this->Mail->From = "unit_test@phpmailer.sf.net";
\r
67 $this->Mail->FromName = "Unit Tester";
\r
68 $this->Mail->Sender = "";
\r
69 $this->Mail->Subject = "Unit Test";
\r
70 $this->Mail->Body = "";
\r
71 $this->Mail->AltBody = "";
\r
72 $this->Mail->WordWrap = 0;
\r
73 $this->Mail->Host = $global_vars["mail_host"];
\r
74 $this->Mail->Port = 25;
\r
75 $this->Mail->Helo = "localhost.localdomain";
\r
76 $this->Mail->SMTPAuth = false;
\r
77 $this->Mail->Username = "";
\r
78 $this->Mail->Password = "";
\r
79 $this->Mail->PluginDir = $INCLUDE_DIR;
\r
80 $this->Mail->AddReplyTo("no_reply@phpmailer.sf.net", "Reply Guy");
\r
81 $this->Mail->Sender = "unit_test@phpmailer.sf.net";
\r
83 if(strlen($this->Mail->Host) > 0)
\r
84 $this->Mail->Mailer = "smtp";
\r
87 $this->Mail->Mailer = "mail";
\r
88 $this->Sender = "unit_test@phpmailer.sf.net";
\r
91 global $global_vars;
\r
92 $this->SetAddress($global_vars["mail_to"], "Test User");
\r
93 if(strlen($global_vars["mail_cc"]) > 0)
\r
94 $this->SetAddress($global_vars["mail_cc"], "Carbon User", "cc");
\r
98 * Run after each test is completed.
\r
100 function tearDown() {
\r
101 // Clean global variables
\r
102 $this->Mail = NULL;
\r
103 $this->ChangeLog = array();
\r
104 $this->NoteLog = array();
\r
109 * Build the body of the message in the appropriate format.
\r
113 function BuildBody() {
\r
114 $this->CheckChanges();
\r
116 // Determine line endings for message
\r
117 if($this->Mail->ContentType == "text/html" || strlen($this->Mail->AltBody) > 0)
\r
121 $bullet_start = "<ul>";
\r
122 $bullet_end = "</ul>";
\r
128 $bullet_start = "";
\r
134 $ReportBody .= "---------------------" . $eol;
\r
135 $ReportBody .= "Unit Test Information" . $eol;
\r
136 $ReportBody .= "---------------------" . $eol;
\r
137 $ReportBody .= "phpmailer version: " . $this->Mail->Version . $eol;
\r
138 $ReportBody .= "Content Type: " . $this->Mail->ContentType . $eol;
\r
140 if(strlen($this->Mail->Host) > 0)
\r
141 $ReportBody .= "Host: " . $this->Mail->Host . $eol;
\r
143 // If attachments then create an attachment list
\r
144 if(count($this->Mail->attachment) > 0)
\r
146 $ReportBody .= "Attachments:" . $eol;
\r
147 $ReportBody .= $bullet_start;
\r
148 for($i = 0; $i < count($this->Mail->attachment); $i++)
\r
150 $ReportBody .= $bullet . "Name: " . $this->Mail->attachment[$i][1] . ", ";
\r
151 $ReportBody .= "Encoding: " . $this->Mail->attachment[$i][3] . ", ";
\r
152 $ReportBody .= "Type: " . $this->Mail->attachment[$i][4] . $eol;
\r
154 $ReportBody .= $bullet_end . $eol;
\r
157 // If there are changes then list them
\r
158 if(count($this->ChangeLog) > 0)
\r
160 $ReportBody .= "Changes" . $eol;
\r
161 $ReportBody .= "-------" . $eol;
\r
163 $ReportBody .= $bullet_start;
\r
164 for($i = 0; $i < count($this->ChangeLog); $i++)
\r
166 $ReportBody .= $bullet . $this->ChangeLog[$i][0] . " was changed to [" .
\r
167 $this->ChangeLog[$i][1] . "]" . $eol;
\r
169 $ReportBody .= $bullet_end . $eol . $eol;
\r
172 // If there are notes then list them
\r
173 if(count($this->NoteLog) > 0)
\r
175 $ReportBody .= "Notes" . $eol;
\r
176 $ReportBody .= "-----" . $eol;
\r
178 $ReportBody .= $bullet_start;
\r
179 for($i = 0; $i < count($this->NoteLog); $i++)
\r
181 $ReportBody .= $bullet . $this->NoteLog[$i] . $eol;
\r
183 $ReportBody .= $bullet_end;
\r
186 // Re-attach the original body
\r
187 $this->Mail->Body .= $eol . $eol . $ReportBody;
\r
191 * Check which default settings have been changed for the report.
\r
195 function CheckChanges() {
\r
196 if($this->Mail->Priority != 3)
\r
197 $this->AddChange("Priority", $this->Mail->Priority);
\r
198 if($this->Mail->Encoding != "8bit")
\r
199 $this->AddChange("Encoding", $this->Mail->Encoding);
\r
200 if($this->Mail->CharSet != "iso-8859-1")
\r
201 $this->AddChange("CharSet", $this->Mail->CharSet);
\r
202 if($this->Mail->Sender != "")
\r
203 $this->AddChange("Sender", $this->Mail->Sender);
\r
204 if($this->Mail->WordWrap != 0)
\r
205 $this->AddChange("WordWrap", $this->Mail->WordWrap);
\r
206 if($this->Mail->Mailer != "mail")
\r
207 $this->AddChange("Mailer", $this->Mail->Mailer);
\r
208 if($this->Mail->Port != 25)
\r
209 $this->AddChange("Port", $this->Mail->Port);
\r
210 if($this->Mail->Helo != "localhost.localdomain")
\r
211 $this->AddChange("Helo", $this->Mail->Helo);
\r
212 if($this->Mail->SMTPAuth)
\r
213 $this->AddChange("SMTPAuth", "true");
\r
217 * Adds a change entry.
\r
221 function AddChange($sName, $sNewValue) {
\r
222 $cur = count($this->ChangeLog);
\r
223 $this->ChangeLog[$cur][0] = $sName;
\r
224 $this->ChangeLog[$cur][1] = $sNewValue;
\r
228 * Adds a simple note to the message.
\r
232 function AddNote($sValue) {
\r
233 $this->NoteLog[] = $sValue;
\r
237 * Adds all of the addresses
\r
241 function SetAddress($sAddress, $sName = "", $sType = "to") {
\r
245 $this->Mail->AddAddress($sAddress, $sName);
\r
248 $this->Mail->AddCC($sAddress, $sName);
\r
251 $this->Mail->AddBCC($sAddress, $sName);
\r
256 /////////////////////////////////////////////////
\r
258 /////////////////////////////////////////////////
\r
261 * Try a plain message.
\r
263 function test_WordWrap() {
\r
265 $this->Mail->WordWrap = 40;
\r
266 $my_body = "Here is the main body of this message. It should " .
\r
267 "be quite a few lines. It should be wrapped at the " .
\r
268 "40 characters. Make sure that it is.";
\r
269 $nBodyLen = strlen($my_body);
\r
270 $my_body .= "\n\nThis is the above body length: " . $nBodyLen;
\r
272 $this->Mail->Body = $my_body;
\r
273 $this->Mail->Subject .= ": Wordwrap";
\r
275 $this->BuildBody();
\r
276 $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
\r
280 * Try a plain message.
\r
282 function test_Low_Priority() {
\r
284 $this->Mail->Priority = 5;
\r
285 $this->Mail->Body = "Here is the main body. There should be " .
\r
286 "a reply to address in this message.";
\r
287 $this->Mail->Subject .= ": Low Priority";
\r
288 $this->Mail->AddReplyTo("nobody@nobody.com", "Nobody (Unit Test)");
\r
290 $this->BuildBody();
\r
291 $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
\r
295 * Simple plain file attachment test.
\r
297 function test_Multiple_Plain_FileAttachment() {
\r
299 $this->Mail->Body = "Here is the text body";
\r
300 $this->Mail->Subject .= ": Plain + Multiple FileAttachments";
\r
302 if(!$this->Mail->AddAttachment("test.png"))
\r
304 $this->assert(false, $this->Mail->ErrorInfo);
\r
308 if(!$this->Mail->AddAttachment("phpmailer_test.php", "test.txt"))
\r
310 $this->assert(false, $this->Mail->ErrorInfo);
\r
314 $this->BuildBody();
\r
315 $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
\r
319 * Simple plain string attachment test.
\r
321 function test_Plain_StringAttachment() {
\r
323 $this->Mail->Body = "Here is the text body";
\r
324 $this->Mail->Subject .= ": Plain + StringAttachment";
\r
326 $sAttachment = "These characters are the content of the " .
\r
327 "string attachment.\nThis might be taken from a ".
\r
328 "database or some other such thing. ";
\r
330 $this->Mail->AddStringAttachment($sAttachment, "string_attach.txt");
\r
332 $this->BuildBody();
\r
333 $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
\r
337 * Plain quoted-printable message.
\r
339 function test_Quoted_Printable() {
\r
341 $this->Mail->Body = "Here is the main body";
\r
342 $this->Mail->Subject .= ": Plain + Quoted-printable";
\r
343 $this->Mail->Encoding = "quoted-printable";
\r
345 $this->BuildBody();
\r
346 $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
\r
350 * Try a plain message.
\r
352 function test_Html() {
\r
354 $this->Mail->IsHTML(true);
\r
355 $this->Mail->Subject .= ": HTML only";
\r
357 $this->Mail->Body = "This is a <b>test message</b> written in HTML. </br>" .
\r
358 "Go to <a href=\"http://phpmailer.sourceforge.net/\">" .
\r
359 "http://phpmailer.sourceforge.net/</a> for new versions of " .
\r
360 "phpmailer. <p/> Thank you!";
\r
362 $this->BuildBody();
\r
363 $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
\r
367 * Simple HTML and attachment test
\r
369 function test_HTML_Attachment() {
\r
371 $this->Mail->Body = "This is the <b>HTML</b> part of the email.";
\r
372 $this->Mail->Subject .= ": HTML + Attachment";
\r
373 $this->Mail->IsHTML(true);
\r
375 if(!$this->Mail->AddAttachment("phpmailer_test.php", "test_attach.txt"))
\r
377 $this->assert(false, $this->Mail->ErrorInfo);
\r
381 $this->BuildBody();
\r
382 $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
\r
386 * An embedded attachment test.
\r
388 function test_Embedded_Image() {
\r
390 $this->Mail->Body = "Embedded Image: <img alt=\"phpmailer\" src=\"cid:my-attach\">" .
\r
391 "Here is an image!</a>";
\r
392 $this->Mail->Subject .= ": Embedded Image";
\r
393 $this->Mail->IsHTML(true);
\r
395 if(!$this->Mail->AddEmbeddedImage("test.png", "my-attach", "test.png",
\r
396 "base64", "image/png"))
\r
398 $this->assert(false, $this->Mail->ErrorInfo);
\r
402 $this->BuildBody();
\r
403 $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
\r
407 * An embedded attachment test.
\r
409 function test_Multi_Embedded_Image() {
\r
411 $this->Mail->Body = "Embedded Image: <img alt=\"phpmailer\" src=\"cid:my-attach\">" .
\r
412 "Here is an image!</a>";
\r
413 $this->Mail->Subject .= ": Embedded Image + Attachment";
\r
414 $this->Mail->IsHTML(true);
\r
416 if(!$this->Mail->AddEmbeddedImage("test.png", "my-attach", "test.png",
\r
417 "base64", "image/png"))
\r
419 $this->assert(false, $this->Mail->ErrorInfo);
\r
423 if(!$this->Mail->AddAttachment("phpmailer_test.php", "test.txt"))
\r
425 $this->assert(false, $this->Mail->ErrorInfo);
\r
429 $this->BuildBody();
\r
430 $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
\r
434 * Simple multipart/alternative test.
\r
436 function test_AltBody() {
\r
438 $this->Mail->Body = "This is the <b>HTML</b> part of the email.";
\r
439 $this->Mail->AltBody = "Here is the text body of this message. " .
\r
440 "It should be quite a few lines. It should be wrapped at the " .
\r
441 "40 characters. Make sure that it is.";
\r
442 $this->Mail->WordWrap = 40;
\r
443 $this->AddNote("This is a mulipart alternative email");
\r
444 $this->Mail->Subject .= ": AltBody + Word Wrap";
\r
446 $this->BuildBody();
\r
447 $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
\r
451 * Simple HTML and attachment test
\r
453 function test_AltBody_Attachment() {
\r
455 $this->Mail->Body = "This is the <b>HTML</b> part of the email.";
\r
456 $this->Mail->AltBody = "This is the text part of the email.";
\r
457 $this->Mail->Subject .= ": AltBody + Attachment";
\r
458 $this->Mail->IsHTML(true);
\r
460 if(!$this->Mail->AddAttachment("phpmailer_test.php", "test_attach.txt"))
\r
462 $this->assert(false, $this->Mail->ErrorInfo);
\r
466 $this->BuildBody();
\r
467 $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
\r
469 $fp = fopen("message.txt", "w");
\r
470 fwrite($fp, $this->Mail->CreateHeader() . $this->Mail->CreateBody());
\r
474 function test_MultipleSend() {
\r
475 $this->Mail->Body = "Sending two messages without keepalive";
\r
476 $this->BuildBody();
\r
477 $subject = $this->Mail->Subject;
\r
479 $this->Mail->Subject = $subject . ": SMTP 1";
\r
480 $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
\r
482 $this->Mail->Subject = $subject . ": SMTP 2";
\r
483 $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
\r
486 function test_SmtpKeepAlive() {
\r
487 $this->Mail->Body = "This was done using the SMTP keep-alive.";
\r
488 $this->BuildBody();
\r
489 $subject = $this->Mail->Subject;
\r
491 $this->Mail->SMTPKeepAlive = true;
\r
492 $this->Mail->Subject = $subject . ": SMTP keep-alive 1";
\r
493 $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
\r
495 $this->Mail->Subject = $subject . ": SMTP keep-alive 2";
\r
496 $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
\r
497 $this->Mail->SmtpClose();
\r
501 * Tests this denial of service attack:
\r
502 * http://www.cybsec.com/vuln/PHPMailer-DOS.pdf
\r
504 function test_DenialOfServiceAttack() {
\r
505 $this->Mail->Body = "This should no longer cause a denial of service.";
\r
506 $this->BuildBody();
\r
508 $this->Mail->Subject = str_repeat("A", 998);
\r
509 $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
\r
512 function test_Error() {
\r
513 $this->Mail->Subject .= ": This should be sent";
\r
514 $this->BuildBody();
\r
515 $this->Mail->ClearAllRecipients(); // no addresses should cause an error
\r
516 $this->assert($this->Mail->IsError() == false, "Error found");
\r
517 $this->assert($this->Mail->Send() == false, "Send succeeded");
\r
518 $this->assert($this->Mail->IsError(), "No error found");
\r
519 $this->assertEquals('You must provide at least one ' .
\r
520 'recipient email address.', $this->Mail->ErrorInfo);
\r
521 $this->Mail->AddAddress(get("mail_to"));
\r
522 $this->assert($this->Mail->Send(), "Send failed");
\r
527 * Create and run test instance.
\r
530 if(isset($HTTP_GET_VARS))
\r
531 $global_vars = $HTTP_GET_VARS;
\r
533 $global_vars = $_REQUEST;
\r
535 if(isset($global_vars["submitted"]))
\r
537 echo "Test results:<br>";
\r
538 $suite = new TestSuite( "phpmailerTest" );
\r
540 $testRunner = new TestRunner;
\r
541 $testRunner->run($suite);
\r
542 echo "<hr noshade/>";
\r
545 function get($sName) {
\r
546 global $global_vars;
\r
547 if(isset($global_vars[$sName]))
\r
548 return $global_vars[$sName];
\r
557 <h3>phpmailer Unit Test</h3>
\r
558 By entering a SMTP hostname it will automatically perform tests with SMTP.
\r
560 <form name="phpmailer_unit" action="phpmailer_test.php" method="get">
\r
561 <input type="hidden" name="submitted" value="1"/>
\r
562 To Address: <input type="text" size="50" name="mail_to" value="<?php echo get("mail_to"); ?>"/>
\r
564 Cc Address: <input type="text" size="50" name="mail_cc" value="<?php echo get("mail_cc"); ?>"/>
\r
566 SMTP Hostname: <input type="text" size="50" name="mail_host" value="<?php echo get("mail_host"); ?>"/>
\r
568 <input type="submit" value="Run Test"/>
\r