--- /dev/null
+<?php\r
+/*******************\r
+ Unit Test\r
+ Type: phpmailer class\r
+********************/\r
+\r
+$INCLUDE_DIR = "../";\r
+\r
+require("phpunit.php");\r
+require($INCLUDE_DIR . "class.phpmailer.php");\r
+error_reporting(E_ALL);\r
+\r
+/**\r
+ * Performs authentication tests\r
+ */\r
+class phpmailerTest extends TestCase\r
+{\r
+ /**\r
+ * Holds the default phpmailer instance.\r
+ * @private\r
+ * @type object\r
+ */\r
+ var $Mail = false;\r
+\r
+ /**\r
+ * Holds the SMTP mail host.\r
+ * @public\r
+ * @type string\r
+ */\r
+ var $Host = "";\r
+ \r
+ /**\r
+ * Holds the change log.\r
+ * @private\r
+ * @type string array\r
+ */\r
+ var $ChangeLog = array();\r
+ \r
+ /**\r
+ * Holds the note log.\r
+ * @private\r
+ * @type string array\r
+ */\r
+ var $NoteLog = array(); \r
+\r
+ /**\r
+ * Class constuctor.\r
+ */\r
+ function phpmailerTest($name) {\r
+ /* must define this constructor */\r
+ $this->TestCase( $name );\r
+ }\r
+ \r
+ /**\r
+ * Run before each test is started.\r
+ */\r
+ function setUp() {\r
+ global $global_vars;\r
+ global $INCLUDE_DIR;\r
+\r
+ $this->Mail = new PHPMailer();\r
+\r
+ $this->Mail->Priority = 3;\r
+ $this->Mail->Encoding = "8bit";\r
+ $this->Mail->CharSet = "iso-8859-1";\r
+ $this->Mail->From = "unit_test@phpmailer.sf.net";\r
+ $this->Mail->FromName = "Unit Tester";\r
+ $this->Mail->Sender = "";\r
+ $this->Mail->Subject = "Unit Test";\r
+ $this->Mail->Body = "";\r
+ $this->Mail->AltBody = "";\r
+ $this->Mail->WordWrap = 0;\r
+ $this->Mail->Host = $global_vars["mail_host"];\r
+ $this->Mail->Port = 25;\r
+ $this->Mail->Helo = "localhost.localdomain";\r
+ $this->Mail->SMTPAuth = false;\r
+ $this->Mail->Username = "";\r
+ $this->Mail->Password = "";\r
+ $this->Mail->PluginDir = $INCLUDE_DIR;\r
+ $this->Mail->AddReplyTo("no_reply@phpmailer.sf.net", "Reply Guy");\r
+ $this->Mail->Sender = "unit_test@phpmailer.sf.net";\r
+\r
+ if(strlen($this->Mail->Host) > 0)\r
+ $this->Mail->Mailer = "smtp";\r
+ else\r
+ {\r
+ $this->Mail->Mailer = "mail";\r
+ $this->Sender = "unit_test@phpmailer.sf.net";\r
+ }\r
+ \r
+ global $global_vars;\r
+ $this->SetAddress($global_vars["mail_to"], "Test User");\r
+ if(strlen($global_vars["mail_cc"]) > 0)\r
+ $this->SetAddress($global_vars["mail_cc"], "Carbon User", "cc");\r
+ } \r
+\r
+ /**\r
+ * Run after each test is completed.\r
+ */\r
+ function tearDown() {\r
+ // Clean global variables\r
+ $this->Mail = NULL;\r
+ $this->ChangeLog = array();\r
+ $this->NoteLog = array();\r
+ }\r
+\r
+\r
+ /**\r
+ * Build the body of the message in the appropriate format.\r
+ * @private\r
+ * @returns void\r
+ */\r
+ function BuildBody() {\r
+ $this->CheckChanges();\r
+ \r
+ // Determine line endings for message \r
+ if($this->Mail->ContentType == "text/html" || strlen($this->Mail->AltBody) > 0)\r
+ {\r
+ $eol = "<br/>";\r
+ $bullet = "<li>";\r
+ $bullet_start = "<ul>";\r
+ $bullet_end = "</ul>";\r
+ }\r
+ else\r
+ {\r
+ $eol = "\n";\r
+ $bullet = " - ";\r
+ $bullet_start = "";\r
+ $bullet_end = "";\r
+ }\r
+ \r
+ $ReportBody = "";\r
+ \r
+ $ReportBody .= "---------------------" . $eol;\r
+ $ReportBody .= "Unit Test Information" . $eol;\r
+ $ReportBody .= "---------------------" . $eol;\r
+ $ReportBody .= "phpmailer version: " . $this->Mail->Version . $eol;\r
+ $ReportBody .= "Content Type: " . $this->Mail->ContentType . $eol;\r
+ \r
+ if(strlen($this->Mail->Host) > 0)\r
+ $ReportBody .= "Host: " . $this->Mail->Host . $eol;\r
+ \r
+ // If attachments then create an attachment list\r
+ if(count($this->Mail->attachment) > 0)\r
+ {\r
+ $ReportBody .= "Attachments:" . $eol;\r
+ $ReportBody .= $bullet_start;\r
+ for($i = 0; $i < count($this->Mail->attachment); $i++)\r
+ {\r
+ $ReportBody .= $bullet . "Name: " . $this->Mail->attachment[$i][1] . ", ";\r
+ $ReportBody .= "Encoding: " . $this->Mail->attachment[$i][3] . ", ";\r
+ $ReportBody .= "Type: " . $this->Mail->attachment[$i][4] . $eol;\r
+ }\r
+ $ReportBody .= $bullet_end . $eol;\r
+ }\r
+ \r
+ // If there are changes then list them\r
+ if(count($this->ChangeLog) > 0)\r
+ {\r
+ $ReportBody .= "Changes" . $eol;\r
+ $ReportBody .= "-------" . $eol;\r
+\r
+ $ReportBody .= $bullet_start;\r
+ for($i = 0; $i < count($this->ChangeLog); $i++)\r
+ {\r
+ $ReportBody .= $bullet . $this->ChangeLog[$i][0] . " was changed to [" . \r
+ $this->ChangeLog[$i][1] . "]" . $eol;\r
+ }\r
+ $ReportBody .= $bullet_end . $eol . $eol;\r
+ }\r
+ \r
+ // If there are notes then list them\r
+ if(count($this->NoteLog) > 0)\r
+ {\r
+ $ReportBody .= "Notes" . $eol;\r
+ $ReportBody .= "-----" . $eol;\r
+\r
+ $ReportBody .= $bullet_start;\r
+ for($i = 0; $i < count($this->NoteLog); $i++)\r
+ {\r
+ $ReportBody .= $bullet . $this->NoteLog[$i] . $eol;\r
+ }\r
+ $ReportBody .= $bullet_end;\r
+ }\r
+ \r
+ // Re-attach the original body\r
+ $this->Mail->Body .= $eol . $eol . $ReportBody;\r
+ }\r
+ \r
+ /**\r
+ * Check which default settings have been changed for the report.\r
+ * @private\r
+ * @returns void\r
+ */\r
+ function CheckChanges() {\r
+ if($this->Mail->Priority != 3)\r
+ $this->AddChange("Priority", $this->Mail->Priority);\r
+ if($this->Mail->Encoding != "8bit")\r
+ $this->AddChange("Encoding", $this->Mail->Encoding);\r
+ if($this->Mail->CharSet != "iso-8859-1")\r
+ $this->AddChange("CharSet", $this->Mail->CharSet);\r
+ if($this->Mail->Sender != "")\r
+ $this->AddChange("Sender", $this->Mail->Sender);\r
+ if($this->Mail->WordWrap != 0)\r
+ $this->AddChange("WordWrap", $this->Mail->WordWrap);\r
+ if($this->Mail->Mailer != "mail")\r
+ $this->AddChange("Mailer", $this->Mail->Mailer);\r
+ if($this->Mail->Port != 25)\r
+ $this->AddChange("Port", $this->Mail->Port);\r
+ if($this->Mail->Helo != "localhost.localdomain")\r
+ $this->AddChange("Helo", $this->Mail->Helo);\r
+ if($this->Mail->SMTPAuth)\r
+ $this->AddChange("SMTPAuth", "true");\r
+ }\r
+ \r
+ /**\r
+ * Adds a change entry.\r
+ * @private\r
+ * @returns void\r
+ */\r
+ function AddChange($sName, $sNewValue) {\r
+ $cur = count($this->ChangeLog);\r
+ $this->ChangeLog[$cur][0] = $sName;\r
+ $this->ChangeLog[$cur][1] = $sNewValue;\r
+ }\r
+ \r
+ /**\r
+ * Adds a simple note to the message.\r
+ * @public\r
+ * @returns void\r
+ */\r
+ function AddNote($sValue) {\r
+ $this->NoteLog[] = $sValue;\r
+ }\r
+\r
+ /**\r
+ * Adds all of the addresses\r
+ * @public\r
+ * @returns void\r
+ */\r
+ function SetAddress($sAddress, $sName = "", $sType = "to") {\r
+ switch($sType)\r
+ {\r
+ case "to":\r
+ $this->Mail->AddAddress($sAddress, $sName);\r
+ break;\r
+ case "cc":\r
+ $this->Mail->AddCC($sAddress, $sName);\r
+ break;\r
+ case "bcc":\r
+ $this->Mail->AddBCC($sAddress, $sName);\r
+ break;\r
+ }\r
+ }\r
+\r
+ /////////////////////////////////////////////////\r
+ // UNIT TESTS\r
+ /////////////////////////////////////////////////\r
+\r
+ /**\r
+ * Try a plain message.\r
+ */\r
+ function test_WordWrap() {\r
+\r
+ $this->Mail->WordWrap = 40;\r
+ $my_body = "Here is the main body of this message. It should " .\r
+ "be quite a few lines. It should be wrapped at the " .\r
+ "40 characters. Make sure that it is.";\r
+ $nBodyLen = strlen($my_body);\r
+ $my_body .= "\n\nThis is the above body length: " . $nBodyLen;\r
+\r
+ $this->Mail->Body = $my_body;\r
+ $this->Mail->Subject .= ": Wordwrap";\r
+\r
+ $this->BuildBody();\r
+ $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);\r
+ }\r
+\r
+ /**\r
+ * Try a plain message.\r
+ */\r
+ function test_Low_Priority() {\r
+ \r
+ $this->Mail->Priority = 5;\r
+ $this->Mail->Body = "Here is the main body. There should be " .\r
+ "a reply to address in this message.";\r
+ $this->Mail->Subject .= ": Low Priority";\r
+ $this->Mail->AddReplyTo("nobody@nobody.com", "Nobody (Unit Test)");\r
+\r
+ $this->BuildBody();\r
+ $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);\r
+ }\r
+\r
+ /**\r
+ * Simple plain file attachment test.\r
+ */\r
+ function test_Multiple_Plain_FileAttachment() {\r
+\r
+ $this->Mail->Body = "Here is the text body";\r
+ $this->Mail->Subject .= ": Plain + Multiple FileAttachments";\r
+\r
+ if(!$this->Mail->AddAttachment("test.png"))\r
+ {\r
+ $this->assert(false, $this->Mail->ErrorInfo);\r
+ return;\r
+ }\r
+\r
+ if(!$this->Mail->AddAttachment("phpmailer_test.php", "test.txt"))\r
+ {\r
+ $this->assert(false, $this->Mail->ErrorInfo);\r
+ return;\r
+ }\r
+\r
+ $this->BuildBody();\r
+ $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);\r
+ }\r
+\r
+ /**\r
+ * Simple plain string attachment test.\r
+ */\r
+ function test_Plain_StringAttachment() {\r
+\r
+ $this->Mail->Body = "Here is the text body";\r
+ $this->Mail->Subject .= ": Plain + StringAttachment";\r
+ \r
+ $sAttachment = "These characters are the content of the " .\r
+ "string attachment.\nThis might be taken from a ".\r
+ "database or some other such thing. ";\r
+ \r
+ $this->Mail->AddStringAttachment($sAttachment, "string_attach.txt");\r
+\r
+ $this->BuildBody();\r
+ $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);\r
+ }\r
+\r
+ /**\r
+ * Plain quoted-printable message.\r
+ */\r
+ function test_Quoted_Printable() {\r
+\r
+ $this->Mail->Body = "Here is the main body";\r
+ $this->Mail->Subject .= ": Plain + Quoted-printable";\r
+ $this->Mail->Encoding = "quoted-printable";\r
+\r
+ $this->BuildBody();\r
+ $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);\r
+ }\r
+\r
+ /**\r
+ * Try a plain message.\r
+ */\r
+ function test_Html() {\r
+ \r
+ $this->Mail->IsHTML(true);\r
+ $this->Mail->Subject .= ": HTML only";\r
+ \r
+ $this->Mail->Body = "This is a <b>test message</b> written in HTML. </br>" .\r
+ "Go to <a href=\"http://phpmailer.sourceforge.net/\">" .\r
+ "http://phpmailer.sourceforge.net/</a> for new versions of " .\r
+ "phpmailer. <p/> Thank you!";\r
+\r
+ $this->BuildBody();\r
+ $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);\r
+ }\r
+\r
+ /**\r
+ * Simple HTML and attachment test\r
+ */\r
+ function test_HTML_Attachment() {\r
+\r
+ $this->Mail->Body = "This is the <b>HTML</b> part of the email.";\r
+ $this->Mail->Subject .= ": HTML + Attachment";\r
+ $this->Mail->IsHTML(true);\r
+ \r
+ if(!$this->Mail->AddAttachment("phpmailer_test.php", "test_attach.txt"))\r
+ {\r
+ $this->assert(false, $this->Mail->ErrorInfo);\r
+ return;\r
+ }\r
+\r
+ $this->BuildBody();\r
+ $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);\r
+ }\r
+\r
+ /**\r
+ * An embedded attachment test.\r
+ */\r
+ function test_Embedded_Image() {\r
+\r
+ $this->Mail->Body = "Embedded Image: <img alt=\"phpmailer\" src=\"cid:my-attach\">" .\r
+ "Here is an image!</a>";\r
+ $this->Mail->Subject .= ": Embedded Image";\r
+ $this->Mail->IsHTML(true);\r
+ \r
+ if(!$this->Mail->AddEmbeddedImage("test.png", "my-attach", "test.png",\r
+ "base64", "image/png"))\r
+ {\r
+ $this->assert(false, $this->Mail->ErrorInfo);\r
+ return;\r
+ }\r
+\r
+ $this->BuildBody();\r
+ $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);\r
+ }\r
+\r
+ /**\r
+ * An embedded attachment test.\r
+ */\r
+ function test_Multi_Embedded_Image() {\r
+\r
+ $this->Mail->Body = "Embedded Image: <img alt=\"phpmailer\" src=\"cid:my-attach\">" .\r
+ "Here is an image!</a>";\r
+ $this->Mail->Subject .= ": Embedded Image + Attachment";\r
+ $this->Mail->IsHTML(true);\r
+ \r
+ if(!$this->Mail->AddEmbeddedImage("test.png", "my-attach", "test.png",\r
+ "base64", "image/png"))\r
+ {\r
+ $this->assert(false, $this->Mail->ErrorInfo);\r
+ return;\r
+ }\r
+\r
+ if(!$this->Mail->AddAttachment("phpmailer_test.php", "test.txt"))\r
+ {\r
+ $this->assert(false, $this->Mail->ErrorInfo);\r
+ return;\r
+ }\r
+ \r
+ $this->BuildBody();\r
+ $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);\r
+ }\r
+\r
+ /**\r
+ * Simple multipart/alternative test.\r
+ */\r
+ function test_AltBody() {\r
+\r
+ $this->Mail->Body = "This is the <b>HTML</b> part of the email.";\r
+ $this->Mail->AltBody = "Here is the text body of this message. " .\r
+ "It should be quite a few lines. It should be wrapped at the " .\r
+ "40 characters. Make sure that it is.";\r
+ $this->Mail->WordWrap = 40;\r
+ $this->AddNote("This is a mulipart alternative email");\r
+ $this->Mail->Subject .= ": AltBody + Word Wrap";\r
+\r
+ $this->BuildBody();\r
+ $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);\r
+ }\r
+\r
+ /**\r
+ * Simple HTML and attachment test\r
+ */\r
+ function test_AltBody_Attachment() {\r
+\r
+ $this->Mail->Body = "This is the <b>HTML</b> part of the email.";\r
+ $this->Mail->AltBody = "This is the text part of the email.";\r
+ $this->Mail->Subject .= ": AltBody + Attachment";\r
+ $this->Mail->IsHTML(true);\r
+ \r
+ if(!$this->Mail->AddAttachment("phpmailer_test.php", "test_attach.txt"))\r
+ {\r
+ $this->assert(false, $this->Mail->ErrorInfo);\r
+ return;\r
+ }\r
+\r
+ $this->BuildBody();\r
+ $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);\r
+\r
+ $fp = fopen("message.txt", "w");\r
+ fwrite($fp, $this->Mail->CreateHeader() . $this->Mail->CreateBody());\r
+ fclose($fp);\r
+ } \r
+\r
+ function test_MultipleSend() {\r
+ $this->Mail->Body = "Sending two messages without keepalive";\r
+ $this->BuildBody();\r
+ $subject = $this->Mail->Subject;\r
+\r
+ $this->Mail->Subject = $subject . ": SMTP 1";\r
+ $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);\r
+ \r
+ $this->Mail->Subject = $subject . ": SMTP 2";\r
+ $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);\r
+ }\r
+\r
+ function test_SmtpKeepAlive() {\r
+ $this->Mail->Body = "This was done using the SMTP keep-alive.";\r
+ $this->BuildBody();\r
+ $subject = $this->Mail->Subject;\r
+\r
+ $this->Mail->SMTPKeepAlive = true;\r
+ $this->Mail->Subject = $subject . ": SMTP keep-alive 1";\r
+ $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);\r
+ \r
+ $this->Mail->Subject = $subject . ": SMTP keep-alive 2";\r
+ $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);\r
+ $this->Mail->SmtpClose();\r
+ }\r
+ \r
+ /**\r
+ * Tests this denial of service attack: \r
+ * http://www.cybsec.com/vuln/PHPMailer-DOS.pdf\r
+ */\r
+ function test_DenialOfServiceAttack() {\r
+ $this->Mail->Body = "This should no longer cause a denial of service.";\r
+ $this->BuildBody();\r
+ \r
+ $this->Mail->Subject = str_repeat("A", 998);\r
+ $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);\r
+ }\r
+ \r
+ function test_Error() {\r
+ $this->Mail->Subject .= ": This should be sent"; \r
+ $this->BuildBody();\r
+ $this->Mail->ClearAllRecipients(); // no addresses should cause an error\r
+ $this->assert($this->Mail->IsError() == false, "Error found");\r
+ $this->assert($this->Mail->Send() == false, "Send succeeded");\r
+ $this->assert($this->Mail->IsError(), "No error found");\r
+ $this->assertEquals('You must provide at least one ' .\r
+ 'recipient email address.', $this->Mail->ErrorInfo);\r
+ $this->Mail->AddAddress(get("mail_to"));\r
+ $this->assert($this->Mail->Send(), "Send failed");\r
+ }\r
+} \r
+ \r
+/**\r
+ * Create and run test instance.\r
+ */\r
+ \r
+if(isset($HTTP_GET_VARS))\r
+ $global_vars = $HTTP_GET_VARS;\r
+else\r
+ $global_vars = $_REQUEST;\r
+\r
+if(isset($global_vars["submitted"]))\r
+{\r
+ echo "Test results:<br>";\r
+ $suite = new TestSuite( "phpmailerTest" );\r
+ \r
+ $testRunner = new TestRunner;\r
+ $testRunner->run($suite);\r
+ echo "<hr noshade/>";\r
+}\r
+\r
+function get($sName) {\r
+ global $global_vars;\r
+ if(isset($global_vars[$sName]))\r
+ return $global_vars[$sName];\r
+ else\r
+ return "";\r
+}\r
+\r
+?>\r
+\r
+<html>\r
+<body>\r
+<h3>phpmailer Unit Test</h3>\r
+By entering a SMTP hostname it will automatically perform tests with SMTP.\r
+\r
+<form name="phpmailer_unit" action="phpmailer_test.php" method="get">\r
+<input type="hidden" name="submitted" value="1"/>\r
+To Address: <input type="text" size="50" name="mail_to" value="<?php echo get("mail_to"); ?>"/>\r
+<br/>\r
+Cc Address: <input type="text" size="50" name="mail_cc" value="<?php echo get("mail_cc"); ?>"/>\r
+<br/>\r
+SMTP Hostname: <input type="text" size="50" name="mail_host" value="<?php echo get("mail_host"); ?>"/>\r
+<p/>\r
+<input type="submit" value="Run Test"/>\r
+\r
+</form>\r
+</body>\r
+</html>\r