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