phpmailer_test.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. <?php
  2. /*******************
  3. Unit Test
  4. Type: phpmailer class
  5. ********************/
  6. $INCLUDE_DIR = "../";
  7. require("phpunit.php");
  8. require($INCLUDE_DIR . "class.phpmailer.php");
  9. error_reporting(E_ALL);
  10. /**
  11. * Performs authentication tests
  12. */
  13. class phpmailerTest extends TestCase
  14. {
  15. /**
  16. * Holds the default phpmailer instance.
  17. * @private
  18. * @type object
  19. */
  20. var $Mail = false;
  21. /**
  22. * Holds the SMTP mail host.
  23. * @public
  24. * @type string
  25. */
  26. var $Host = "";
  27. /**
  28. * Holds the change log.
  29. * @private
  30. * @type string array
  31. */
  32. var $ChangeLog = array();
  33. /**
  34. * Holds the note log.
  35. * @private
  36. * @type string array
  37. */
  38. var $NoteLog = array();
  39. /**
  40. * Class constuctor.
  41. */
  42. function phpmailerTest($name) {
  43. /* must define this constructor */
  44. $this->TestCase( $name );
  45. }
  46. /**
  47. * Run before each test is started.
  48. */
  49. function setUp() {
  50. global $global_vars;
  51. global $INCLUDE_DIR;
  52. $this->Mail = new PHPMailer();
  53. $this->Mail->Priority = 3;
  54. $this->Mail->Encoding = "8bit";
  55. $this->Mail->CharSet = "iso-8859-1";
  56. $this->Mail->From = "antsnet@126.com";
  57. $this->Mail->FromName = "Unit Tester";
  58. $this->Mail->Sender = "";
  59. $this->Mail->Subject = "Unit Test";
  60. $this->Mail->Body = "";
  61. $this->Mail->AltBody = "";
  62. $this->Mail->WordWrap = 0;
  63. $this->Mail->Host = "smtp.163.com";
  64. $this->Mail->Port = 25;
  65. $this->Mail->Helo = "smtp.163.com";
  66. $this->Mail->SMTPAuth = true;
  67. $this->Mail->Username = "antsnet";
  68. $this->Mail->Password = "119328118";
  69. $this->Mail->PluginDir = $INCLUDE_DIR;
  70. $this->Mail->AddReplyTo("antsent@163.com", "antsnet");
  71. $this->Mail->Sender = "jinhui.zhu@live.cn";
  72. if(strlen($this->Mail->Host) > 0)
  73. $this->Mail->Mailer = "smtp";
  74. else
  75. {
  76. $this->Mail->Mailer = "mail";
  77. $this->Sender = "jinhui.zhu@live.cn";
  78. }
  79. global $global_vars;
  80. $this->SetAddress($global_vars["mail_to"], "Test User");
  81. if(strlen($global_vars["mail_cc"]) > 0)
  82. $this->SetAddress($global_vars["mail_cc"], "Carbon User", "cc");
  83. }
  84. /**
  85. * Run after each test is completed.
  86. */
  87. function tearDown() {
  88. // Clean global variables
  89. $this->Mail = NULL;
  90. $this->ChangeLog = array();
  91. $this->NoteLog = array();
  92. }
  93. /**
  94. * Build the body of the message in the appropriate format.
  95. * @private
  96. * @returns void
  97. */
  98. function BuildBody() {
  99. $this->CheckChanges();
  100. // Determine line endings for message
  101. if($this->Mail->ContentType == "text/html" || strlen($this->Mail->AltBody) > 0)
  102. {
  103. $eol = "<br/>";
  104. $bullet = "<li>";
  105. $bullet_start = "<ul>";
  106. $bullet_end = "</ul>";
  107. }
  108. else
  109. {
  110. $eol = "\n";
  111. $bullet = " - ";
  112. $bullet_start = "";
  113. $bullet_end = "";
  114. }
  115. $ReportBody = "";
  116. $ReportBody .= "---------------------" . $eol;
  117. $ReportBody .= "Unit Test Information" . $eol;
  118. $ReportBody .= "---------------------" . $eol;
  119. $ReportBody .= "phpmailer version: " . $this->Mail->Version . $eol;
  120. $ReportBody .= "Content Type: " . $this->Mail->ContentType . $eol;
  121. if(strlen($this->Mail->Host) > 0)
  122. $ReportBody .= "Host: " . $this->Mail->Host . $eol;
  123. // If attachments then create an attachment list
  124. if(count($this->Mail->attachment) > 0)
  125. {
  126. $ReportBody .= "Attachments:" . $eol;
  127. $ReportBody .= $bullet_start;
  128. for($i = 0; $i < count($this->Mail->attachment); $i++)
  129. {
  130. $ReportBody .= $bullet . "Name: " . $this->Mail->attachment[$i][1] . ", ";
  131. $ReportBody .= "Encoding: " . $this->Mail->attachment[$i][3] . ", ";
  132. $ReportBody .= "Type: " . $this->Mail->attachment[$i][4] . $eol;
  133. }
  134. $ReportBody .= $bullet_end . $eol;
  135. }
  136. // If there are changes then list them
  137. if(count($this->ChangeLog) > 0)
  138. {
  139. $ReportBody .= "Changes" . $eol;
  140. $ReportBody .= "-------" . $eol;
  141. $ReportBody .= $bullet_start;
  142. for($i = 0; $i < count($this->ChangeLog); $i++)
  143. {
  144. $ReportBody .= $bullet . $this->ChangeLog[$i][0] . " was changed to [" .
  145. $this->ChangeLog[$i][1] . "]" . $eol;
  146. }
  147. $ReportBody .= $bullet_end . $eol . $eol;
  148. }
  149. // If there are notes then list them
  150. if(count($this->NoteLog) > 0)
  151. {
  152. $ReportBody .= "Notes" . $eol;
  153. $ReportBody .= "-----" . $eol;
  154. $ReportBody .= $bullet_start;
  155. for($i = 0; $i < count($this->NoteLog); $i++)
  156. {
  157. $ReportBody .= $bullet . $this->NoteLog[$i] . $eol;
  158. }
  159. $ReportBody .= $bullet_end;
  160. }
  161. // Re-attach the original body
  162. $this->Mail->Body .= $eol . $eol . $ReportBody;
  163. }
  164. /**
  165. * Check which default settings have been changed for the report.
  166. * @private
  167. * @returns void
  168. */
  169. function CheckChanges() {
  170. if($this->Mail->Priority != 3)
  171. $this->AddChange("Priority", $this->Mail->Priority);
  172. if($this->Mail->Encoding != "8bit")
  173. $this->AddChange("Encoding", $this->Mail->Encoding);
  174. if($this->Mail->CharSet != "iso-8859-1")
  175. $this->AddChange("CharSet", $this->Mail->CharSet);
  176. if($this->Mail->Sender != "")
  177. $this->AddChange("Sender", $this->Mail->Sender);
  178. if($this->Mail->WordWrap != 0)
  179. $this->AddChange("WordWrap", $this->Mail->WordWrap);
  180. if($this->Mail->Mailer != "mail")
  181. $this->AddChange("Mailer", $this->Mail->Mailer);
  182. if($this->Mail->Port != 25)
  183. $this->AddChange("Port", $this->Mail->Port);
  184. if($this->Mail->Helo != "smtp.163.com")
  185. $this->AddChange("Helo", $this->Mail->Helo);
  186. if($this->Mail->SMTPAuth)
  187. $this->AddChange("SMTPAuth", "true");
  188. }
  189. /**
  190. * Adds a change entry.
  191. * @private
  192. * @returns void
  193. */
  194. function AddChange($sName, $sNewValue) {
  195. $cur = count($this->ChangeLog);
  196. $this->ChangeLog[$cur][0] = $sName;
  197. $this->ChangeLog[$cur][1] = $sNewValue;
  198. }
  199. /**
  200. * Adds a simple note to the message.
  201. * @public
  202. * @returns void
  203. */
  204. function AddNote($sValue) {
  205. $this->NoteLog[] = $sValue;
  206. }
  207. /**
  208. * Adds all of the addresses
  209. * @public
  210. * @returns void
  211. */
  212. function SetAddress($sAddress, $sName = "", $sType = "to") {
  213. switch($sType)
  214. {
  215. case "to":
  216. $this->Mail->AddAddress($sAddress, $sName);
  217. break;
  218. case "cc":
  219. $this->Mail->AddCC($sAddress, $sName);
  220. break;
  221. case "bcc":
  222. $this->Mail->AddBCC($sAddress, $sName);
  223. break;
  224. }
  225. }
  226. /////////////////////////////////////////////////
  227. // UNIT TESTS
  228. /////////////////////////////////////////////////
  229. /**
  230. * Try a plain message.
  231. */
  232. function test_WordWrap() {
  233. $this->Mail->WordWrap = 40;
  234. $my_body = "Here is the main body of this message. It should " .
  235. "be quite a few lines. It should be wrapped at the " .
  236. "40 characters. Make sure that it is.";
  237. $nBodyLen = strlen($my_body);
  238. $my_body .= "\n\nThis is the above body length: " . $nBodyLen;
  239. $this->Mail->Body = $my_body;
  240. $this->Mail->Subject .= ": Wordwrap";
  241. $this->BuildBody();
  242. $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
  243. }
  244. /**
  245. * Try a plain message.
  246. */
  247. function test_Low_Priority() {
  248. $this->Mail->Priority = 5;
  249. $this->Mail->Body = "Here is the main body. There should be " .
  250. "a reply to address in this message.";
  251. $this->Mail->Subject .= ": Low Priority";
  252. $this->Mail->AddReplyTo("jinhui.zhu@live.cn", "Nobody (Unit Test)");
  253. $this->BuildBody();
  254. $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
  255. }
  256. /**
  257. * Simple plain file attachment test.
  258. */
  259. function test_Multiple_Plain_FileAttachment() {
  260. $this->Mail->Body = "Here is the text body";
  261. $this->Mail->Subject .= ": Plain + Multiple FileAttachments";
  262. if(!$this->Mail->AddAttachment("test.png"))
  263. {
  264. $this->assert(false, $this->Mail->ErrorInfo);
  265. return;
  266. }
  267. if(!$this->Mail->AddAttachment("phpmailer_test.php", "test.txt"))
  268. {
  269. $this->assert(false, $this->Mail->ErrorInfo);
  270. return;
  271. }
  272. $this->BuildBody();
  273. $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
  274. }
  275. /**
  276. * Simple plain string attachment test.
  277. */
  278. function test_Plain_StringAttachment() {
  279. $this->Mail->Body = "Here is the text body";
  280. $this->Mail->Subject .= ": Plain + StringAttachment";
  281. $sAttachment = "These characters are the content of the " .
  282. "string attachment.\nThis might be taken from a ".
  283. "database or some other such thing. ";
  284. $this->Mail->AddStringAttachment($sAttachment, "string_attach.txt");
  285. $this->BuildBody();
  286. $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
  287. }
  288. /**
  289. * Plain quoted-printable message.
  290. */
  291. function test_Quoted_Printable() {
  292. $this->Mail->Body = "Here is the main body";
  293. $this->Mail->Subject .= ": Plain + Quoted-printable";
  294. $this->Mail->Encoding = "quoted-printable";
  295. $this->BuildBody();
  296. $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
  297. }
  298. /**
  299. * Try a plain message.
  300. */
  301. function test_Html() {
  302. $this->Mail->IsHTML(true);
  303. $this->Mail->Subject .= ": HTML only";
  304. $this->Mail->Body = "This is a <b>test message</b> written in HTML. </br>" .
  305. "Go to <a href=\"http://phpmailer.sourceforge.net/\">" .
  306. "http://phpmailer.sourceforge.net/</a> for new versions of " .
  307. "phpmailer. <p/> Thank you!";
  308. $this->BuildBody();
  309. $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
  310. }
  311. /**
  312. * Simple HTML and attachment test
  313. */
  314. function test_HTML_Attachment() {
  315. $this->Mail->Body = "This is the <b>HTML</b> part of the email.";
  316. $this->Mail->Subject .= ": HTML + Attachment";
  317. $this->Mail->IsHTML(true);
  318. if(!$this->Mail->AddAttachment("phpmailer_test.php", "test_attach.txt"))
  319. {
  320. $this->assert(false, $this->Mail->ErrorInfo);
  321. return;
  322. }
  323. $this->BuildBody();
  324. $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
  325. }
  326. /**
  327. * An embedded attachment test.
  328. */
  329. function test_Embedded_Image() {
  330. $this->Mail->Body = "Embedded Image: <img alt=\"phpmailer\" src=\"cid:my-attach\">" .
  331. "Here is an image!</a>";
  332. $this->Mail->Subject .= ": Embedded Image";
  333. $this->Mail->IsHTML(true);
  334. if(!$this->Mail->AddEmbeddedImage("test.png", "my-attach", "test.png",
  335. "base64", "image/png"))
  336. {
  337. $this->assert(false, $this->Mail->ErrorInfo);
  338. return;
  339. }
  340. $this->BuildBody();
  341. $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
  342. }
  343. /**
  344. * An embedded attachment test.
  345. */
  346. function test_Multi_Embedded_Image() {
  347. $this->Mail->Body = "Embedded Image: <img alt=\"phpmailer\" src=\"cid:my-attach\">" .
  348. "Here is an image!</a>";
  349. $this->Mail->Subject .= ": Embedded Image + Attachment";
  350. $this->Mail->IsHTML(true);
  351. if(!$this->Mail->AddEmbeddedImage("test.png", "my-attach", "test.png",
  352. "base64", "image/png"))
  353. {
  354. $this->assert(false, $this->Mail->ErrorInfo);
  355. return;
  356. }
  357. if(!$this->Mail->AddAttachment("phpmailer_test.php", "test.txt"))
  358. {
  359. $this->assert(false, $this->Mail->ErrorInfo);
  360. return;
  361. }
  362. $this->BuildBody();
  363. $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
  364. }
  365. /**
  366. * Simple multipart/alternative test.
  367. */
  368. function test_AltBody() {
  369. $this->Mail->Body = "This is the <b>HTML</b> part of the email.";
  370. $this->Mail->AltBody = "Here is the text body of this message. " .
  371. "It should be quite a few lines. It should be wrapped at the " .
  372. "40 characters. Make sure that it is.";
  373. $this->Mail->WordWrap = 40;
  374. $this->AddNote("This is a mulipart alternative email");
  375. $this->Mail->Subject .= ": AltBody + Word Wrap";
  376. $this->BuildBody();
  377. $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
  378. }
  379. /**
  380. * Simple HTML and attachment test
  381. */
  382. function test_AltBody_Attachment() {
  383. $this->Mail->Body = "This is the <b>HTML</b> part of the email.";
  384. $this->Mail->AltBody = "This is the text part of the email.";
  385. $this->Mail->Subject .= ": AltBody + Attachment";
  386. $this->Mail->IsHTML(true);
  387. if(!$this->Mail->AddAttachment("phpmailer_test.php", "test_attach.txt"))
  388. {
  389. $this->assert(false, $this->Mail->ErrorInfo);
  390. return;
  391. }
  392. $this->BuildBody();
  393. $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
  394. $fp = fopen("message.txt", "w");
  395. fwrite($fp, $this->Mail->CreateHeader() . $this->Mail->CreateBody());
  396. fclose($fp);
  397. }
  398. function test_MultipleSend() {
  399. $this->Mail->Body = "Sending two messages without keepalive";
  400. $this->BuildBody();
  401. $subject = $this->Mail->Subject;
  402. $this->Mail->Subject = $subject . ": SMTP 1";
  403. $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
  404. $this->Mail->Subject = $subject . ": SMTP 2";
  405. $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
  406. }
  407. function test_SmtpKeepAlive() {
  408. $this->Mail->Body = "This was done using the SMTP keep-alive.";
  409. $this->BuildBody();
  410. $subject = $this->Mail->Subject;
  411. $this->Mail->SMTPKeepAlive = true;
  412. $this->Mail->Subject = $subject . ": SMTP keep-alive 1";
  413. $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
  414. $this->Mail->Subject = $subject . ": SMTP keep-alive 2";
  415. $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
  416. $this->Mail->SmtpClose();
  417. }
  418. /**
  419. * Tests this denial of service attack:
  420. * http://www.cybsec.com/vuln/PHPMailer-DOS.pdf
  421. */
  422. function test_DenialOfServiceAttack() {
  423. $this->Mail->Body = "This should no longer cause a denial of service.";
  424. $this->BuildBody();
  425. $this->Mail->Subject = str_repeat("A", 998);
  426. $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
  427. }
  428. function test_Error() {
  429. $this->Mail->Subject .= ": This should be sent";
  430. $this->BuildBody();
  431. $this->Mail->ClearAllRecipients(); // no addresses should cause an error
  432. $this->assert($this->Mail->IsError() == false, "Error found");
  433. $this->assert($this->Mail->Send() == false, "Send succeeded");
  434. $this->assert($this->Mail->IsError(), "No error found");
  435. $this->assertEquals('You must provide at least one ' .
  436. 'recipient email address.', $this->Mail->ErrorInfo);
  437. $this->Mail->AddAddress(get("mail_to"));
  438. $this->assert($this->Mail->Send(), "Send failed");
  439. }
  440. }
  441. /**
  442. * Create and run test instance.
  443. */
  444. if(isset($HTTP_GET_VARS))
  445. $global_vars = $HTTP_GET_VARS;
  446. else
  447. $global_vars = $_REQUEST;
  448. if(isset($global_vars["submitted"]))
  449. {
  450. echo "Test results:<br>";
  451. $suite = new TestSuite( "phpmailerTest" );
  452. $testRunner = new TestRunner;
  453. $testRunner->run($suite);
  454. echo "<hr noshade/>";
  455. }
  456. function get($sName) {
  457. global $global_vars;
  458. if(isset($global_vars[$sName]))
  459. return $global_vars[$sName];
  460. else
  461. return "";
  462. }
  463. ?>
  464. <html>
  465. <body>
  466. <h3>phpmailer Unit Test</h3>
  467. By entering a SMTP hostname it will automatically perform tests with SMTP.
  468. <form name="phpmailer_unit" action="phpmailer_test.php" method="get">
  469. <input type="hidden" name="submitted" value="1"/>
  470. To Address: <input type="text" size="50" name="mail_to" value="<?php echo get("mail_to"); ?>"/>
  471. <br/>
  472. Cc Address: <input type="text" size="50" name="mail_cc" value="<?php echo get("mail_cc"); ?>"/>
  473. <br/>
  474. SMTP Hostname: <input type="text" size="50" name="mail_host" value="<?php echo get("mail_host"); ?>"/>
  475. <p/>
  476. <input type="submit" value="Run Test"/>
  477. </form>
  478. </body>
  479. </html>