phpmailerTest.php 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  1. <?php
  2. /**
  3. * PHPMailer - PHP email transport unit tests
  4. * Requires PHPUnit 3.3 or later.
  5. *
  6. * PHP version 5.0.0
  7. *
  8. * @package PHPMailer
  9. * @author Andy Prevost
  10. * @author Marcus Bointon <phpmailer@synchromedia.co.uk>
  11. * @copyright 2004 - 2009 Andy Prevost
  12. * @copyright 2010 Marcus Bointon
  13. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  14. */
  15. require_once '../PHPMailerAutoload.php';
  16. /**
  17. * PHPMailer - PHP email transport unit test class
  18. * Performs authentication tests
  19. */
  20. class PHPMailerTest extends PHPUnit_Framework_TestCase
  21. {
  22. /**
  23. * Holds the default phpmailer instance.
  24. * @private
  25. * @type PHPMailer
  26. */
  27. public $Mail;
  28. /**
  29. * Holds the SMTP mail host.
  30. * @public
  31. * @type string
  32. */
  33. public $Host = '';
  34. /**
  35. * Holds the change log.
  36. * @private
  37. * @type string[]
  38. */
  39. public $ChangeLog = array();
  40. /**
  41. * Holds the note log.
  42. * @private
  43. * @type string[]
  44. */
  45. public $NoteLog = array();
  46. /**
  47. * Default include path
  48. * @type string
  49. */
  50. public $INCLUDE_DIR = '../';
  51. /**
  52. * PIDs of any processes we need to kill
  53. * @type array
  54. * @access private
  55. */
  56. private $pids = array();
  57. /**
  58. * Run before each test is started.
  59. */
  60. public function setUp()
  61. {
  62. if (file_exists('./testbootstrap.php')) {
  63. include './testbootstrap.php'; //Overrides go in here
  64. }
  65. $this->Mail = new PHPMailer;
  66. $this->Mail->SMTPDebug = 4; //Full debug output
  67. $this->Mail->Priority = 3;
  68. $this->Mail->Encoding = '8bit';
  69. $this->Mail->CharSet = 'iso-8859-1';
  70. if (array_key_exists('mail_from', $_REQUEST)) {
  71. $this->Mail->From = $_REQUEST['mail_from'];
  72. } else {
  73. $this->Mail->From = 'unit_test@phpmailer.example.com';
  74. }
  75. $this->Mail->FromName = 'Unit Tester';
  76. $this->Mail->Sender = '';
  77. $this->Mail->Subject = 'Unit Test';
  78. $this->Mail->Body = '';
  79. $this->Mail->AltBody = '';
  80. $this->Mail->WordWrap = 0;
  81. if (array_key_exists('mail_host', $_REQUEST)) {
  82. $this->Mail->Host = $_REQUEST['mail_host'];
  83. } else {
  84. $this->Mail->Host = 'mail.example.com';
  85. }
  86. if (array_key_exists('mail_port', $_REQUEST)) {
  87. $this->Mail->Port = $_REQUEST['mail_port'];
  88. } else {
  89. $this->Mail->Port = 25;
  90. }
  91. $this->Mail->Helo = 'localhost.localdomain';
  92. $this->Mail->SMTPAuth = false;
  93. $this->Mail->Username = '';
  94. $this->Mail->Password = '';
  95. $this->Mail->PluginDir = $this->INCLUDE_DIR;
  96. $this->Mail->addReplyTo('no_reply@phpmailer.example.com', 'Reply Guy');
  97. $this->Mail->Sender = 'unit_test@phpmailer.example.com';
  98. if (strlen($this->Mail->Host) > 0) {
  99. $this->Mail->Mailer = 'smtp';
  100. } else {
  101. $this->Mail->Mailer = 'mail';
  102. $this->Mail->Sender = 'unit_test@phpmailer.example.com';
  103. }
  104. if (array_key_exists('mail_to', $_REQUEST)) {
  105. $this->setAddress($_REQUEST['mail_to'], 'Test User', 'to');
  106. }
  107. if (array_key_exists('mail_cc', $_REQUEST) and strlen($_REQUEST['mail_cc']) > 0) {
  108. $this->setAddress($_REQUEST['mail_cc'], 'Carbon User', 'cc');
  109. }
  110. }
  111. /**
  112. * Run after each test is completed.
  113. */
  114. public function tearDown()
  115. {
  116. // Clean global variables
  117. $this->Mail = null;
  118. $this->ChangeLog = array();
  119. $this->NoteLog = array();
  120. foreach ($this->pids as $pid) {
  121. $p = escapeshellarg($pid);
  122. shell_exec("ps $p && kill -TERM $p");
  123. }
  124. }
  125. /**
  126. * Build the body of the message in the appropriate format.
  127. *
  128. * @private
  129. * @return void
  130. */
  131. public function buildBody()
  132. {
  133. $this->checkChanges();
  134. // Determine line endings for message
  135. if ($this->Mail->ContentType == 'text/html' || strlen($this->Mail->AltBody) > 0) {
  136. $eol = '<br/>';
  137. $bullet = '<li>';
  138. $bullet_start = '<ul>';
  139. $bullet_end = '</ul>';
  140. } else {
  141. $eol = "\n";
  142. $bullet = ' - ';
  143. $bullet_start = '';
  144. $bullet_end = '';
  145. }
  146. $ReportBody = '';
  147. $ReportBody .= '---------------------' . $eol;
  148. $ReportBody .= 'Unit Test Information' . $eol;
  149. $ReportBody .= '---------------------' . $eol;
  150. $ReportBody .= 'phpmailer version: ' . $this->Mail->Version . $eol;
  151. $ReportBody .= 'Content Type: ' . $this->Mail->ContentType . $eol;
  152. if (strlen($this->Mail->Host) > 0) {
  153. $ReportBody .= 'Host: ' . $this->Mail->Host . $eol;
  154. }
  155. // If attachments then create an attachment list
  156. $attachments = $this->Mail->getAttachments();
  157. if (count($attachments) > 0) {
  158. $ReportBody .= 'Attachments:' . $eol;
  159. $ReportBody .= $bullet_start;
  160. foreach ($attachments as $attachment) {
  161. $ReportBody .= $bullet . 'Name: ' . $attachment[1] . ', ';
  162. $ReportBody .= 'Encoding: ' . $attachment[3] . ', ';
  163. $ReportBody .= 'Type: ' . $attachment[4] . $eol;
  164. }
  165. $ReportBody .= $bullet_end . $eol;
  166. }
  167. // If there are changes then list them
  168. if (count($this->ChangeLog) > 0) {
  169. $ReportBody .= 'Changes' . $eol;
  170. $ReportBody .= '-------' . $eol;
  171. $ReportBody .= $bullet_start;
  172. for ($i = 0; $i < count($this->ChangeLog); $i++) {
  173. $ReportBody .= $bullet . $this->ChangeLog[$i][0] . ' was changed to [' .
  174. $this->ChangeLog[$i][1] . ']' . $eol;
  175. }
  176. $ReportBody .= $bullet_end . $eol . $eol;
  177. }
  178. // If there are notes then list them
  179. if (count($this->NoteLog) > 0) {
  180. $ReportBody .= 'Notes' . $eol;
  181. $ReportBody .= '-----' . $eol;
  182. $ReportBody .= $bullet_start;
  183. for ($i = 0; $i < count($this->NoteLog); $i++) {
  184. $ReportBody .= $bullet . $this->NoteLog[$i] . $eol;
  185. }
  186. $ReportBody .= $bullet_end;
  187. }
  188. // Re-attach the original body
  189. $this->Mail->Body .= $eol . $eol . $ReportBody;
  190. }
  191. /**
  192. * Check which default settings have been changed for the report.
  193. * @private
  194. * @return void
  195. */
  196. public function checkChanges()
  197. {
  198. if ($this->Mail->Priority != 3) {
  199. $this->addChange('Priority', $this->Mail->Priority);
  200. }
  201. if ($this->Mail->Encoding != '8bit') {
  202. $this->addChange('Encoding', $this->Mail->Encoding);
  203. }
  204. if ($this->Mail->CharSet != 'iso-8859-1') {
  205. $this->addChange('CharSet', $this->Mail->CharSet);
  206. }
  207. if ($this->Mail->Sender != '') {
  208. $this->addChange('Sender', $this->Mail->Sender);
  209. }
  210. if ($this->Mail->WordWrap != 0) {
  211. $this->addChange('WordWrap', $this->Mail->WordWrap);
  212. }
  213. if ($this->Mail->Mailer != 'mail') {
  214. $this->addChange('Mailer', $this->Mail->Mailer);
  215. }
  216. if ($this->Mail->Port != 25) {
  217. $this->addChange('Port', $this->Mail->Port);
  218. }
  219. if ($this->Mail->Helo != 'localhost.localdomain') {
  220. $this->addChange('Helo', $this->Mail->Helo);
  221. }
  222. if ($this->Mail->SMTPAuth) {
  223. $this->addChange('SMTPAuth', 'true');
  224. }
  225. }
  226. /**
  227. * Add a changelog entry.
  228. * @access private
  229. * @param string $sName
  230. * @param string $sNewValue
  231. * @return void
  232. */
  233. public function addChange($sName, $sNewValue)
  234. {
  235. $this->ChangeLog[] = array($sName, $sNewValue);
  236. }
  237. /**
  238. * Adds a simple note to the message.
  239. * @public
  240. * @param string $sValue
  241. * @return void
  242. */
  243. public function addNote($sValue)
  244. {
  245. $this->NoteLog[] = $sValue;
  246. }
  247. /**
  248. * Adds all of the addresses
  249. * @access public
  250. * @param string $sAddress
  251. * @param string $sName
  252. * @param string $sType
  253. * @return boolean
  254. */
  255. public function setAddress($sAddress, $sName = '', $sType = 'to')
  256. {
  257. switch ($sType) {
  258. case 'to':
  259. return $this->Mail->addAddress($sAddress, $sName);
  260. case 'cc':
  261. return $this->Mail->addCC($sAddress, $sName);
  262. case 'bcc':
  263. return $this->Mail->addBCC($sAddress, $sName);
  264. }
  265. return false;
  266. }
  267. /**
  268. * Check that we have loaded default test params.
  269. * Pretty much everything will fail due to unset recipient if this is not done.
  270. */
  271. public function testBootstrap()
  272. {
  273. $this->assertTrue(
  274. file_exists('./testbootstrap.php'),
  275. 'Test config params missing - copy testbootstrap.php to testbootstrap-dist.php and change as appropriate'
  276. );
  277. }
  278. /**
  279. * Test CRAM-MD5 authentication
  280. * Needs a connection to a server that supports this auth mechanism, so commented out by default
  281. */
  282. public function testAuthCRAMMD5()
  283. {
  284. $this->Mail->Host = 'hostname';
  285. $this->Mail->Port = 587;
  286. $this->Mail->SMTPAuth = true;
  287. $this->Mail->SMTPSecure = 'tls';
  288. $this->Mail->AuthType = 'CRAM-MD5';
  289. $this->Mail->Username = 'username';
  290. $this->Mail->Password = 'password';
  291. $this->Mail->Body = 'Test body';
  292. $this->Mail->Subject .= ': Auth CRAM-MD5';
  293. $this->Mail->From = 'from@example.com';
  294. $this->Mail->Sender = 'from@example.com';
  295. $this->Mail->clearAllRecipients();
  296. $this->Mail->addAddress('user@example.com');
  297. //$this->assertTrue($this->mail->send(), $this->mail->ErrorInfo);
  298. }
  299. /**
  300. * Test email address validation
  301. * Test addresses obtained from http://isemail.info
  302. * Some failing cases commented out that are apparently up for debate!
  303. */
  304. public function testValidate()
  305. {
  306. $validaddresses = array(
  307. 'first@iana.org',
  308. 'first.last@iana.org',
  309. '1234567890123456789012345678901234567890123456789012345678901234@iana.org',
  310. '"first\"last"@iana.org',
  311. '"first@last"@iana.org',
  312. '"first\last"@iana.org',
  313. 'first.last@[12.34.56.78]',
  314. 'first.last@[IPv6:::12.34.56.78]',
  315. 'first.last@[IPv6:1111:2222:3333::4444:12.34.56.78]',
  316. 'first.last@[IPv6:1111:2222:3333:4444:5555:6666:12.34.56.78]',
  317. 'first.last@[IPv6:::1111:2222:3333:4444:5555:6666]',
  318. 'first.last@[IPv6:1111:2222:3333::4444:5555:6666]',
  319. 'first.last@[IPv6:1111:2222:3333:4444:5555:6666::]',
  320. 'first.last@[IPv6:1111:2222:3333:4444:5555:6666:7777:8888]',
  321. 'first.last@x23456789012345678901234567890123456789012345678901234567890123.iana.org',
  322. 'first.last@3com.com',
  323. 'first.last@123.iana.org',
  324. '"first\last"@iana.org',
  325. 'first.last@[IPv6:1111:2222:3333::4444:5555:12.34.56.78]',
  326. 'first.last@[IPv6:1111:2222:3333::4444:5555:6666:7777]',
  327. 'first.last@example.123',
  328. 'first.last@com',
  329. '"Abc\@def"@iana.org',
  330. '"Fred\ Bloggs"@iana.org',
  331. '"Joe.\Blow"@iana.org',
  332. '"Abc@def"@iana.org',
  333. '"Fred Bloggs"@iana.org',
  334. 'user+mailbox@iana.org',
  335. 'customer/department=shipping@iana.org',
  336. '$A12345@iana.org',
  337. '!def!xyz%abc@iana.org',
  338. '_somename@iana.org',
  339. 'dclo@us.ibm.com',
  340. 'peter.piper@iana.org',
  341. '"Doug \"Ace\" L."@iana.org',
  342. 'test@iana.org',
  343. 'TEST@iana.org',
  344. '1234567890@iana.org',
  345. 'test+test@iana.org',
  346. 'test-test@iana.org',
  347. 't*est@iana.org',
  348. '+1~1+@iana.org',
  349. '{_test_}@iana.org',
  350. '"[[ test ]]"@iana.org',
  351. 'test.test@iana.org',
  352. '"test.test"@iana.org',
  353. 'test."test"@iana.org',
  354. '"test@test"@iana.org',
  355. 'test@123.123.123.x123',
  356. 'test@123.123.123.123',
  357. 'test@[123.123.123.123]',
  358. 'test@example.iana.org',
  359. 'test@example.example.iana.org',
  360. '"test\test"@iana.org',
  361. 'test@example',
  362. '"test\blah"@iana.org',
  363. '"test\blah"@iana.org',
  364. '"test\"blah"@iana.org',
  365. 'customer/department@iana.org',
  366. '_Yosemite.Sam@iana.org',
  367. '~@iana.org',
  368. '"Austin@Powers"@iana.org',
  369. 'Ima.Fool@iana.org',
  370. '"Ima.Fool"@iana.org',
  371. '"Ima Fool"@iana.org',
  372. '"first"."last"@iana.org',
  373. '"first".middle."last"@iana.org',
  374. '"first".last@iana.org',
  375. 'first."last"@iana.org',
  376. '"first"."middle"."last"@iana.org',
  377. '"first.middle"."last"@iana.org',
  378. '"first.middle.last"@iana.org',
  379. '"first..last"@iana.org',
  380. '"first\"last"@iana.org',
  381. 'first."mid\dle"."last"@iana.org',
  382. '"test blah"@iana.org',
  383. '(foo)cal(bar)@(baz)iamcal.com(quux)',
  384. 'cal@iamcal(woo).(yay)com',
  385. 'cal(woo(yay)hoopla)@iamcal.com',
  386. 'cal(foo\@bar)@iamcal.com',
  387. 'cal(foo\)bar)@iamcal.com',
  388. 'first().last@iana.org',
  389. 'pete(his account)@silly.test(his host)',
  390. 'c@(Chris\'s host.)public.example',
  391. 'jdoe@machine(comment). example',
  392. '1234 @ local(blah) .machine .example',
  393. 'first(abc.def).last@iana.org',
  394. 'first(a"bc.def).last@iana.org',
  395. 'first.(")middle.last(")@iana.org',
  396. 'first(abc\(def)@iana.org',
  397. 'first.last@x(1234567890123456789012345678901234567890123456789012345678901234567890).com',
  398. 'a(a(b(c)d(e(f))g)h(i)j)@iana.org',
  399. 'name.lastname@domain.com',
  400. 'a@b',
  401. 'a@bar.com',
  402. 'aaa@[123.123.123.123]',
  403. 'a@bar',
  404. 'a-b@bar.com',
  405. '+@b.c',
  406. '+@b.com',
  407. 'a@b.co-foo.uk',
  408. '"hello my name is"@stutter.com',
  409. '"Test \"Fail\" Ing"@iana.org',
  410. 'valid@about.museum',
  411. 'shaitan@my-domain.thisisminekthx',
  412. 'foobar@192.168.0.1',
  413. '"Joe\Blow"@iana.org',
  414. 'HM2Kinsists@(that comments are allowed)this.is.ok',
  415. 'user%uucp!path@berkeley.edu',
  416. 'first.last @iana.org',
  417. 'cdburgess+!#$%&\'*-/=?+_{}|~test@gmail.com',
  418. 'first.last@[IPv6:::a2:a3:a4:b1:b2:b3:b4]',
  419. 'first.last@[IPv6:a1:a2:a3:a4:b1:b2:b3::]',
  420. 'first.last@[IPv6:::]',
  421. 'first.last@[IPv6:::b4]',
  422. 'first.last@[IPv6:::b3:b4]',
  423. 'first.last@[IPv6:a1::b4]',
  424. 'first.last@[IPv6:a1::]',
  425. 'first.last@[IPv6:a1:a2::]',
  426. 'first.last@[IPv6:0123:4567:89ab:cdef::]',
  427. 'first.last@[IPv6:0123:4567:89ab:CDEF::]',
  428. 'first.last@[IPv6:::a3:a4:b1:ffff:11.22.33.44]',
  429. 'first.last@[IPv6:::a2:a3:a4:b1:ffff:11.22.33.44]',
  430. 'first.last@[IPv6:a1:a2:a3:a4::11.22.33.44]',
  431. 'first.last@[IPv6:a1:a2:a3:a4:b1::11.22.33.44]',
  432. 'first.last@[IPv6:a1::11.22.33.44]',
  433. 'first.last@[IPv6:a1:a2::11.22.33.44]',
  434. 'first.last@[IPv6:0123:4567:89ab:cdef::11.22.33.44]',
  435. 'first.last@[IPv6:0123:4567:89ab:CDEF::11.22.33.44]',
  436. 'first.last@[IPv6:a1::b2:11.22.33.44]',
  437. 'test@test.com',
  438. 'test@xn--example.com',
  439. 'test@example.com'
  440. );
  441. $invalidaddresses = array(
  442. 'first.last@sub.do,com',
  443. 'first\@last@iana.org',
  444. '123456789012345678901234567890123456789012345678901234567890' .
  445. '@12345678901234567890123456789012345678901234 [...]',
  446. 'first.last',
  447. '12345678901234567890123456789012345678901234567890123456789012345@iana.org',
  448. '.first.last@iana.org',
  449. 'first.last.@iana.org',
  450. 'first..last@iana.org',
  451. '"first"last"@iana.org',
  452. '"""@iana.org',
  453. '"\"@iana.org',
  454. //'""@iana.org',
  455. 'first\@last@iana.org',
  456. 'first.last@',
  457. 'x@x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.' .
  458. 'x23456789.x23456789.x23456789.x23 [...]',
  459. 'first.last@[.12.34.56.78]',
  460. 'first.last@[12.34.56.789]',
  461. 'first.last@[::12.34.56.78]',
  462. 'first.last@[IPv5:::12.34.56.78]',
  463. 'first.last@[IPv6:1111:2222:3333:4444:5555:12.34.56.78]',
  464. 'first.last@[IPv6:1111:2222:3333:4444:5555:6666:7777:12.34.56.78]',
  465. 'first.last@[IPv6:1111:2222:3333:4444:5555:6666:7777]',
  466. 'first.last@[IPv6:1111:2222:3333:4444:5555:6666:7777:8888:9999]',
  467. 'first.last@[IPv6:1111:2222::3333::4444:5555:6666]',
  468. 'first.last@[IPv6:1111:2222:333x::4444:5555]',
  469. 'first.last@[IPv6:1111:2222:33333::4444:5555]',
  470. 'first.last@-xample.com',
  471. 'first.last@exampl-.com',
  472. 'first.last@x234567890123456789012345678901234567890123456789012345678901234.iana.org',
  473. 'abc\@def@iana.org',
  474. 'abc\@iana.org',
  475. 'Doug\ \"Ace\"\ Lovell@iana.org',
  476. 'abc@def@iana.org',
  477. 'abc\@def@iana.org',
  478. 'abc\@iana.org',
  479. '@iana.org',
  480. 'doug@',
  481. '"qu@iana.org',
  482. 'ote"@iana.org',
  483. '.dot@iana.org',
  484. 'dot.@iana.org',
  485. 'two..dot@iana.org',
  486. '"Doug "Ace" L."@iana.org',
  487. 'Doug\ \"Ace\"\ L\.@iana.org',
  488. 'hello world@iana.org',
  489. //'helloworld@iana .org',
  490. 'gatsby@f.sc.ot.t.f.i.tzg.era.l.d.',
  491. 'test.iana.org',
  492. 'test.@iana.org',
  493. 'test..test@iana.org',
  494. '.test@iana.org',
  495. 'test@test@iana.org',
  496. 'test@@iana.org',
  497. '-- test --@iana.org',
  498. '[test]@iana.org',
  499. '"test"test"@iana.org',
  500. '()[]\;:,><@iana.org',
  501. 'test@.',
  502. 'test@example.',
  503. 'test@.org',
  504. 'test@12345678901234567890123456789012345678901234567890123456789012345678901234567890' .
  505. '12345678901234567890 [...]',
  506. 'test@[123.123.123.123',
  507. 'test@123.123.123.123]',
  508. 'NotAnEmail',
  509. '@NotAnEmail',
  510. '"test"blah"@iana.org',
  511. '.wooly@iana.org',
  512. 'wo..oly@iana.org',
  513. 'pootietang.@iana.org',
  514. '.@iana.org',
  515. 'Ima Fool@iana.org',
  516. 'phil.h\@\@ck@haacked.com',
  517. 'foo@[\1.2.3.4]',
  518. //'first."".last@iana.org',
  519. 'first\last@iana.org',
  520. 'Abc\@def@iana.org',
  521. 'Fred\ Bloggs@iana.org',
  522. 'Joe.\Blow@iana.org',
  523. 'first.last@[IPv6:1111:2222:3333:4444:5555:6666:12.34.567.89]',
  524. '{^c\@**Dog^}@cartoon.com',
  525. //'"foo"(yay)@(hoopla)[1.2.3.4]',
  526. 'cal(foo(bar)@iamcal.com',
  527. 'cal(foo)bar)@iamcal.com',
  528. 'cal(foo\)@iamcal.com',
  529. 'first(12345678901234567890123456789012345678901234567890)last@(1234567890123456789' .
  530. '01234567890123456789012 [...]',
  531. 'first(middle)last@iana.org',
  532. 'first(abc("def".ghi).mno)middle(abc("def".ghi).mno).last@(abc("def".ghi).mno)example' .
  533. '(abc("def".ghi).mno). [...]',
  534. 'a(a(b(c)d(e(f))g)(h(i)j)@iana.org',
  535. '.@',
  536. '@bar.com',
  537. '@@bar.com',
  538. 'aaa.com',
  539. 'aaa@.com',
  540. 'aaa@.123',
  541. 'aaa@[123.123.123.123]a',
  542. 'aaa@[123.123.123.333]',
  543. 'a@bar.com.',
  544. 'a@-b.com',
  545. 'a@b-.com',
  546. '-@..com',
  547. '-@a..com',
  548. 'invalid@about.museum-',
  549. 'test@...........com',
  550. '"Unicode NULL' . chr(0) . '"@char.com',
  551. 'Unicode NULL' . chr(0) . '@char.com',
  552. 'first.last@[IPv6::]',
  553. 'first.last@[IPv6::::]',
  554. 'first.last@[IPv6::b4]',
  555. 'first.last@[IPv6::::b4]',
  556. 'first.last@[IPv6::b3:b4]',
  557. 'first.last@[IPv6::::b3:b4]',
  558. 'first.last@[IPv6:a1:::b4]',
  559. 'first.last@[IPv6:a1:]',
  560. 'first.last@[IPv6:a1:::]',
  561. 'first.last@[IPv6:a1:a2:]',
  562. 'first.last@[IPv6:a1:a2:::]',
  563. 'first.last@[IPv6::11.22.33.44]',
  564. 'first.last@[IPv6::::11.22.33.44]',
  565. 'first.last@[IPv6:a1:11.22.33.44]',
  566. 'first.last@[IPv6:a1:::11.22.33.44]',
  567. 'first.last@[IPv6:a1:a2:::11.22.33.44]',
  568. 'first.last@[IPv6:0123:4567:89ab:cdef::11.22.33.xx]',
  569. 'first.last@[IPv6:0123:4567:89ab:CDEFF::11.22.33.44]',
  570. 'first.last@[IPv6:a1::a4:b1::b4:11.22.33.44]',
  571. 'first.last@[IPv6:a1::11.22.33]',
  572. 'first.last@[IPv6:a1::11.22.33.44.55]',
  573. 'first.last@[IPv6:a1::b211.22.33.44]',
  574. 'first.last@[IPv6:a1::b2::11.22.33.44]',
  575. 'first.last@[IPv6:a1::b3:]',
  576. 'first.last@[IPv6::a2::b4]',
  577. 'first.last@[IPv6:a1:a2:a3:a4:b1:b2:b3:]',
  578. 'first.last@[IPv6::a2:a3:a4:b1:b2:b3:b4]',
  579. 'first.last@[IPv6:a1:a2:a3:a4::b1:b2:b3:b4]'
  580. );
  581. $goodfails = array();
  582. foreach ($validaddresses as $address) {
  583. if (!PHPMailer::validateAddress($address)) {
  584. $goodfails[] = $address;
  585. }
  586. }
  587. $badpasses = array();
  588. foreach ($invalidaddresses as $address) {
  589. if (PHPMailer::validateAddress($address)) {
  590. $badpasses[] = $address;
  591. }
  592. }
  593. $err = '';
  594. if (count($goodfails) > 0) {
  595. $err .= "Good addresses that failed validation:\n";
  596. $err .= implode("\n", $goodfails);
  597. }
  598. if (count($badpasses) > 0) {
  599. if (!empty($err)) {
  600. $err .= "\n\n";
  601. }
  602. $err .= "Bad addresses that passed validation:\n";
  603. $err .= implode("\n", $badpasses);
  604. }
  605. $this->assertEmpty($err, $err);
  606. //For coverage
  607. $this->assertTrue(PHPMailer::validateAddress('test@example.com', 'auto'));
  608. $this->assertFalse(PHPMailer::validateAddress('test@example.com.', 'auto'));
  609. $this->assertTrue(PHPMailer::validateAddress('test@example.com', 'pcre'));
  610. $this->assertFalse(PHPMailer::validateAddress('test@example.com.', 'pcre'));
  611. $this->assertTrue(PHPMailer::validateAddress('test@example.com', 'pcre8'));
  612. $this->assertFalse(PHPMailer::validateAddress('test@example.com.', 'pcre8'));
  613. $this->assertTrue(PHPMailer::validateAddress('test@example.com', 'php'));
  614. $this->assertFalse(PHPMailer::validateAddress('test@example.com.', 'php'));
  615. $this->assertTrue(PHPMailer::validateAddress('test@example.com', 'noregex'));
  616. $this->assertFalse(PHPMailer::validateAddress('bad', 'noregex'));
  617. }
  618. /**
  619. * Try a plain message.
  620. */
  621. public function testWordWrap()
  622. {
  623. $this->Mail->WordWrap = 40;
  624. $my_body = str_repeat(
  625. 'Here is the main body of this message. It should ' .
  626. 'be quite a few lines. It should be wrapped at ' .
  627. '40 characters. Make sure that it is. ',
  628. 10
  629. );
  630. $nBodyLen = strlen($my_body);
  631. $my_body .= "\n\nThis is the above body length: " . $nBodyLen;
  632. $this->Mail->Body = $my_body;
  633. $this->Mail->Subject .= ': Wordwrap';
  634. $this->buildBody();
  635. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  636. }
  637. /**
  638. * Try a plain message.
  639. */
  640. public function testLowPriority()
  641. {
  642. $this->Mail->Priority = 5;
  643. $this->Mail->Body = 'Here is the main body. There should be ' .
  644. 'a reply to address in this message.';
  645. $this->Mail->Subject .= ': Low Priority';
  646. $this->Mail->addReplyTo('nobody@nobody.com', 'Nobody (Unit Test)');
  647. $this->buildBody();
  648. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  649. }
  650. /**
  651. * Simple plain file attachment test.
  652. */
  653. public function testMultiplePlainFileAttachment()
  654. {
  655. $this->Mail->Body = 'Here is the text body';
  656. $this->Mail->Subject .= ': Plain + Multiple FileAttachments';
  657. if (!$this->Mail->addAttachment('../examples/images/phpmailer.png')) {
  658. $this->assertTrue(false, $this->Mail->ErrorInfo);
  659. return;
  660. }
  661. if (!$this->Mail->addAttachment(__FILE__, 'test.txt')) {
  662. $this->assertTrue(false, $this->Mail->ErrorInfo);
  663. return;
  664. }
  665. $this->buildBody();
  666. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  667. }
  668. /**
  669. * Simple plain string attachment test.
  670. */
  671. public function testPlainStringAttachment()
  672. {
  673. $this->Mail->Body = 'Here is the text body';
  674. $this->Mail->Subject .= ': Plain + StringAttachment';
  675. $sAttachment = 'These characters are the content of the ' .
  676. "string attachment.\nThis might be taken from a " .
  677. 'database or some other such thing. ';
  678. $this->Mail->addStringAttachment($sAttachment, 'string_attach.txt');
  679. $this->buildBody();
  680. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  681. }
  682. /**
  683. * Plain quoted-printable message.
  684. */
  685. public function testQuotedPrintable()
  686. {
  687. $this->Mail->Body = 'Here is the main body';
  688. $this->Mail->Subject .= ': Plain + Quoted-printable';
  689. $this->Mail->Encoding = 'quoted-printable';
  690. $this->buildBody();
  691. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  692. //Check that a quoted printable encode and decode results in the same as went in
  693. $t = file_get_contents(__FILE__); //Use this file as test content
  694. $this->assertEquals(
  695. $t,
  696. quoted_printable_decode($this->Mail->encodeQP($t)),
  697. 'Quoted-Printable encoding round-trip failed'
  698. );
  699. $this->assertEquals(
  700. $this->Mail->encodeQP($t),
  701. $this->Mail->encodeQPphp($t),
  702. 'Quoted-Printable BC wrapper failed'
  703. );
  704. }
  705. /**
  706. * Try a plain message.
  707. */
  708. public function testHtml()
  709. {
  710. $this->Mail->isHTML(true);
  711. $this->Mail->Subject .= ": HTML only";
  712. $this->Mail->Body = <<<EOT
  713. <html>
  714. <head>
  715. <title>HTML email test</title>
  716. </head>
  717. <body>
  718. <h1>PHPMailer does HTML!</h1>
  719. <p>This is a <strong>test message</strong> written in HTML.<br>
  720. Go to <a href="https://github.com/PHPMailer/PHPMailer/">https://github.com/PHPMailer/PHPMailer/</a>
  721. for new versions of PHPMailer.</p>
  722. <p>Thank you!</p>
  723. </body>
  724. </html>
  725. EOT;
  726. $this->buildBody();
  727. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  728. }
  729. /**
  730. * Test simple message builder and html2text converters
  731. */
  732. public function testMsgHTML()
  733. {
  734. $message = file_get_contents('../examples/contents.html');
  735. $this->Mail->CharSet = 'utf-8';
  736. $this->Mail->Body = '';
  737. $this->Mail->AltBody = '';
  738. $this->Mail->msgHTML($message, '../examples');
  739. $this->Mail->Subject .= ': msgHTML';
  740. $this->assertNotEmpty($this->Mail->Body, 'Body not set by msgHTML');
  741. $this->assertNotEmpty($this->Mail->AltBody, 'AltBody not set by msgHTML');
  742. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  743. //Again, using the advanced HTML to text converter
  744. $this->Mail->AltBody = '';
  745. $this->Mail->msgHTML($message, '../examples', true);
  746. $this->Mail->Subject .= ' + html2text advanced';
  747. $this->assertNotEmpty($this->Mail->AltBody, 'Advanced AltBody not set by msgHTML');
  748. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  749. }
  750. /**
  751. * Simple HTML and attachment test
  752. */
  753. public function testHTMLAttachment()
  754. {
  755. $this->Mail->Body = 'This is the <strong>HTML</strong> part of the email.';
  756. $this->Mail->Subject .= ': HTML + Attachment';
  757. $this->Mail->isHTML(true);
  758. if (!$this->Mail->addAttachment(__FILE__, 'test_attach.txt')) {
  759. $this->assertTrue(false, $this->Mail->ErrorInfo);
  760. return;
  761. }
  762. //Make sure that trying to attach a nonexistent file fails
  763. $this->assertFalse($this->Mail->addAttachment(__FILE__ . md5(microtime()), 'nonexistent_file.txt'));
  764. $this->buildBody();
  765. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  766. }
  767. /**
  768. * An embedded attachment test.
  769. */
  770. public function testEmbeddedImage()
  771. {
  772. $this->Mail->Body = 'Embedded Image: <img alt="phpmailer" src="cid:my-attach">' .
  773. 'Here is an image!</a>';
  774. $this->Mail->Subject .= ': Embedded Image';
  775. $this->Mail->isHTML(true);
  776. if (!$this->Mail->addEmbeddedImage(
  777. '../examples/images/phpmailer.png',
  778. 'my-attach',
  779. 'phpmailer.png',
  780. 'base64',
  781. 'image/png'
  782. )
  783. ) {
  784. $this->assertTrue(false, $this->Mail->ErrorInfo);
  785. return;
  786. }
  787. $this->buildBody();
  788. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  789. //For code coverage
  790. $this->Mail->addEmbeddedImage('thisfiledoesntexist', 'xyz'); //Non-existent file
  791. $this->Mail->addEmbeddedImage(__FILE__, '123'); //Missing name
  792. }
  793. /**
  794. * An embedded attachment test.
  795. */
  796. public function testMultiEmbeddedImage()
  797. {
  798. $this->Mail->Body = 'Embedded Image: <img alt="phpmailer" src="cid:my-attach">' .
  799. 'Here is an image!</a>';
  800. $this->Mail->Subject .= ': Embedded Image + Attachment';
  801. $this->Mail->isHTML(true);
  802. if (!$this->Mail->addEmbeddedImage(
  803. '../examples/images/phpmailer.png',
  804. 'my-attach',
  805. 'phpmailer.png',
  806. 'base64',
  807. 'image/png'
  808. )
  809. ) {
  810. $this->assertTrue(false, $this->Mail->ErrorInfo);
  811. return;
  812. }
  813. if (!$this->Mail->addAttachment(__FILE__, 'test.txt')) {
  814. $this->assertTrue(false, $this->Mail->ErrorInfo);
  815. return;
  816. }
  817. $this->buildBody();
  818. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  819. }
  820. /**
  821. * Simple multipart/alternative test.
  822. */
  823. public function testAltBody()
  824. {
  825. $this->Mail->Body = 'This is the <strong>HTML</strong> part of the email.';
  826. $this->Mail->AltBody = 'Here is the text body of this message. ' .
  827. 'It should be quite a few lines. It should be wrapped at the ' .
  828. '40 characters. Make sure that it is.';
  829. $this->Mail->WordWrap = 40;
  830. $this->addNote('This is a mulipart alternative email');
  831. $this->Mail->Subject .= ': AltBody + Word Wrap';
  832. $this->buildBody();
  833. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  834. }
  835. /**
  836. * Simple HTML and attachment test
  837. */
  838. public function testAltBodyAttachment()
  839. {
  840. $this->Mail->Body = 'This is the <strong>HTML</strong> part of the email.';
  841. $this->Mail->AltBody = 'This is the text part of the email.';
  842. $this->Mail->Subject .= ': AltBody + Attachment';
  843. $this->Mail->isHTML(true);
  844. if (!$this->Mail->addAttachment(__FILE__, 'test_attach.txt')) {
  845. $this->assertTrue(false, $this->Mail->ErrorInfo);
  846. return;
  847. }
  848. $this->buildBody();
  849. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  850. if (is_writable('.')) {
  851. file_put_contents('message.txt', $this->Mail->createHeader() . $this->Mail->createBody());
  852. } else {
  853. $this->assertTrue(false, 'Could not write local file - check permissions');
  854. }
  855. }
  856. /**
  857. * iCal event test
  858. */
  859. public function testIcal()
  860. {
  861. $this->Mail->Body = 'This is the <strong>HTML</strong> part of the email.';
  862. $this->Mail->AltBody = 'This is the text part of the email.';
  863. $this->Mail->Subject .= ': iCal';
  864. $this->Mail->isHTML(true);
  865. $this->buildBody();
  866. require_once '../extras/EasyPeasyICS.php';
  867. $ICS = new EasyPeasyICS("PHPMailer test calendar");
  868. $ICS->addEvent(
  869. strtotime('tomorrow 10:00 Europe/Paris'),
  870. strtotime('tomorrow 11:00 Europe/Paris'),
  871. 'PHPMailer iCal test',
  872. 'A test of PHPMailer iCal support',
  873. 'https://github.com/PHPMailer/PHPMailer'
  874. );
  875. $this->Mail->Ical = $ICS->render(false);
  876. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  877. $this->Mail->Body = 'Embedded Image: <img alt="phpmailer" src="cid:my-attach">' .
  878. 'Here is an image!</a>.';
  879. $this->Mail->AltBody = 'This is the text part of the email.';
  880. $this->Mail->Subject .= ': iCal + inline';
  881. $this->Mail->isHTML(true);
  882. $this->Mail->addEmbeddedImage(
  883. '../examples/images/phpmailer.png',
  884. 'my-attach',
  885. 'phpmailer.png',
  886. 'base64',
  887. 'image/png'
  888. );
  889. $this->buildBody();
  890. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  891. }
  892. /**
  893. * Test sending multiple messages with separate connections
  894. */
  895. public function testMultipleSend()
  896. {
  897. $this->Mail->Body = 'Sending two messages without keepalive';
  898. $this->buildBody();
  899. $subject = $this->Mail->Subject;
  900. $this->Mail->Subject = $subject . ': SMTP 1';
  901. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  902. $this->Mail->Subject = $subject . ': SMTP 2';
  903. $this->Mail->Sender = 'blah@example.com';
  904. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  905. }
  906. /**
  907. * Test sending using SendMail
  908. */
  909. public function testSendmailSend()
  910. {
  911. $this->Mail->Body = 'Sending via sendmail';
  912. $this->buildBody();
  913. $subject = $this->Mail->Subject;
  914. $this->Mail->Subject = $subject . ': sendmail';
  915. $this->Mail->isSendmail();
  916. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  917. }
  918. /**
  919. * Test sending using Qmail
  920. */
  921. public function testQmailSend()
  922. {
  923. //Only run if we have qmail installed
  924. if (file_exists('/var/qmail/bin/qmail-inject')) {
  925. $this->Mail->Body = 'Sending via qmail';
  926. $this->BuildBody();
  927. $subject = $this->Mail->Subject;
  928. $this->Mail->Subject = $subject . ': qmail';
  929. $this->Mail->IsQmail();
  930. $this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
  931. } else {
  932. $this->markTestSkipped('Qmail is not installed');
  933. }
  934. }
  935. /**
  936. * Test sending using PHP mail() function
  937. */
  938. public function testMailSend()
  939. {
  940. $sendmail = ini_get('sendmail_path');
  941. if (strpos($sendmail, '/') === false) { //No path in sendmail_path
  942. ini_set('sendmail_path', '/usr/sbin/sendmail -t -i ');
  943. }
  944. $this->Mail->Body = 'Sending via mail()';
  945. $this->buildBody();
  946. $this->Mail->Subject = $this->Mail->Subject . ': mail()';
  947. $this->Mail->isMail();
  948. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  949. }
  950. /**
  951. * Test sending an empty body
  952. */
  953. public function testEmptyBody()
  954. {
  955. $this->buildBody();
  956. $this->Mail->Body = '';
  957. $this->Mail->Subject = $this->Mail->Subject . ': Empty Body';
  958. $this->Mail->isMail();
  959. $this->Mail->AllowEmpty = true;
  960. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  961. $this->Mail->AllowEmpty = false;
  962. $this->assertFalse($this->Mail->send(), $this->Mail->ErrorInfo);
  963. }
  964. /**
  965. * Test keepalive (sending multiple messages in a single connection)
  966. */
  967. public function testSmtpKeepAlive()
  968. {
  969. $this->Mail->Body = 'This was done using the SMTP keep-alive.';
  970. $this->buildBody();
  971. $subject = $this->Mail->Subject;
  972. $this->Mail->SMTPKeepAlive = true;
  973. $this->Mail->Subject = $subject . ': SMTP keep-alive 1';
  974. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  975. $this->Mail->Subject = $subject . ': SMTP keep-alive 2';
  976. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  977. $this->Mail->smtpClose();
  978. }
  979. /**
  980. * Tests this denial of service attack:
  981. * http://www.cybsec.com/vuln/PHPMailer-DOS.pdf
  982. */
  983. public function testDenialOfServiceAttack()
  984. {
  985. $this->Mail->Body = 'This should no longer cause a denial of service.';
  986. $this->buildBody();
  987. $this->Mail->Subject = substr(str_repeat('0123456789', 100), 0, 998);
  988. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  989. }
  990. /**
  991. * Tests this denial of service attack:
  992. * https://sourceforge.net/p/phpmailer/bugs/383/
  993. * According to the ticket, this should get stuck in a loop, though I can't make it happen.
  994. */
  995. public function testDenialOfServiceAttack2()
  996. {
  997. //Encoding name longer than 68 chars
  998. $this->Mail->Encoding = '1234567890123456789012345678901234567890123456789012345678901234567890';
  999. //Call wrapText with a zero length value
  1000. $this->Mail->wrapText(str_repeat('This should no longer cause a denial of service. ', 30), 0);
  1001. }
  1002. /**
  1003. * Test error handling
  1004. */
  1005. public function testError()
  1006. {
  1007. $this->Mail->Subject .= ': This should be sent';
  1008. $this->buildBody();
  1009. $this->Mail->clearAllRecipients(); // no addresses should cause an error
  1010. $this->assertTrue($this->Mail->isError() == false, 'Error found');
  1011. $this->assertTrue($this->Mail->send() == false, 'send succeeded');
  1012. $this->assertTrue($this->Mail->isError(), 'No error found');
  1013. $this->assertEquals('You must provide at least one recipient email address.', $this->Mail->ErrorInfo);
  1014. $this->Mail->addAddress($_REQUEST['mail_to']);
  1015. $this->assertTrue($this->Mail->send(), 'send failed');
  1016. }
  1017. /**
  1018. * Test addressing
  1019. */
  1020. public function testAddressing()
  1021. {
  1022. $this->assertFalse($this->Mail->addAddress(''), 'Empty address accepted');
  1023. $this->assertFalse($this->Mail->addAddress('', 'Nobody'), 'Empty address with name accepted');
  1024. $this->assertFalse($this->Mail->addAddress('a@example..com'), 'Invalid address accepted');
  1025. $this->assertTrue($this->Mail->addAddress('a@example.com'), 'Addressing failed');
  1026. $this->assertFalse($this->Mail->addAddress('a@example.com'), 'Duplicate addressing failed');
  1027. $this->assertTrue($this->Mail->addCC('b@example.com'), 'CC addressing failed');
  1028. $this->assertFalse($this->Mail->addCC('b@example.com'), 'CC duplicate addressing failed');
  1029. $this->assertFalse($this->Mail->addCC('a@example.com'), 'CC duplicate addressing failed (2)');
  1030. $this->assertTrue($this->Mail->addBCC('c@example.com'), 'BCC addressing failed');
  1031. $this->assertFalse($this->Mail->addBCC('c@example.com'), 'BCC duplicate addressing failed');
  1032. $this->assertFalse($this->Mail->addBCC('a@example.com'), 'BCC duplicate addressing failed (2)');
  1033. $this->assertTrue($this->Mail->addReplyTo('a@example.com'), 'Replyto Addressing failed');
  1034. $this->assertFalse($this->Mail->addReplyTo('a@example..com'), 'Invalid Replyto address accepted');
  1035. $this->assertTrue($this->Mail->setFrom('a@example.com', 'some name'), 'setFrom failed');
  1036. $this->assertFalse($this->Mail->setFrom('a@example.com.', 'some name'), 'setFrom accepted invalid address');
  1037. $this->Mail->Sender = '';
  1038. $this->Mail->setFrom('a@example.com', 'some name', true);
  1039. $this->assertEquals($this->Mail->Sender, 'a@example.com', 'setFrom failed to set sender');
  1040. $this->Mail->Sender = '';
  1041. $this->Mail->setFrom('a@example.com', 'some name', false);
  1042. $this->assertEquals($this->Mail->Sender, '', 'setFrom should not have set sender');
  1043. $this->Mail->clearCCs();
  1044. $this->Mail->clearBCCs();
  1045. $this->Mail->clearReplyTos();
  1046. }
  1047. /**
  1048. * Test address escaping
  1049. */
  1050. public function testAddressEscaping()
  1051. {
  1052. $this->Mail->Subject .= ': Address escaping';
  1053. $this->Mail->clearAddresses();
  1054. $this->Mail->addAddress('foo@example.com', 'Tim "The Book" O\'Reilly');
  1055. $this->Mail->Body = 'Test correct escaping of quotes in addresses.';
  1056. $this->buildBody();
  1057. $this->Mail->preSend();
  1058. $b = $this->Mail->getSentMIMEMessage();
  1059. $this->assertTrue((strpos($b, 'To: "Tim \"The Book\" O\'Reilly" <foo@example.com>') !== false));
  1060. }
  1061. /**
  1062. * Test BCC-only addressing
  1063. */
  1064. public function testBCCAddressing()
  1065. {
  1066. $this->Mail->Subject .= ': BCC-only addressing';
  1067. $this->buildBody();
  1068. $this->Mail->clearAllRecipients();
  1069. $this->assertTrue($this->Mail->addBCC('a@example.com'), 'BCC addressing failed');
  1070. $this->assertTrue($this->Mail->send(), 'send failed');
  1071. }
  1072. /**
  1073. * Encoding and charset tests
  1074. */
  1075. public function testEncodings()
  1076. {
  1077. $this->Mail->CharSet = 'iso-8859-1';
  1078. $this->assertEquals(
  1079. '=A1Hola!_Se=F1or!',
  1080. $this->Mail->encodeQ("\xa1Hola! Se\xf1or!", 'text'),
  1081. 'Q Encoding (text) failed'
  1082. );
  1083. $this->assertEquals(
  1084. '=A1Hola!_Se=F1or!',
  1085. $this->Mail->encodeQ("\xa1Hola! Se\xf1or!", 'comment'),
  1086. 'Q Encoding (comment) failed'
  1087. );
  1088. $this->assertEquals(
  1089. '=A1Hola!_Se=F1or!',
  1090. $this->Mail->encodeQ("\xa1Hola! Se\xf1or!", 'phrase'),
  1091. 'Q Encoding (phrase) failed'
  1092. );
  1093. $this->Mail->CharSet = 'UTF-8';
  1094. $this->assertEquals(
  1095. '=C2=A1Hola!_Se=C3=B1or!',
  1096. $this->Mail->encodeQ("\xc2\xa1Hola! Se\xc3\xb1or!", 'text'),
  1097. 'Q Encoding (text) failed'
  1098. );
  1099. //Strings containing '=' are a special case
  1100. $this->assertEquals(
  1101. 'Nov=C3=A1=3D',
  1102. $this->Mail->encodeQ("Nov\xc3\xa1=", 'text'),
  1103. 'Q Encoding (text) failed 2'
  1104. );
  1105. }
  1106. public function testBase64()
  1107. {
  1108. $this->Mail->Subject .= ': Base-64 encoding';
  1109. $this->Mail->Encoding = 'base64';
  1110. $this->buildBody();
  1111. $this->assertTrue($this->Mail->send(), 'Base64 encoding failed');
  1112. }
  1113. /**
  1114. * S/MIME Signing tests
  1115. */
  1116. public function testSigning()
  1117. {
  1118. $this->Mail->Subject .= ': S/MIME signing';
  1119. $this->Mail->Body = 'This message is S/MIME signed.';
  1120. $this->buildBody();
  1121. $dn = array(
  1122. 'countryName' => 'UK',
  1123. 'stateOrProvinceName' => 'Here',
  1124. 'localityName' => 'There',
  1125. 'organizationName' => 'PHP',
  1126. 'organizationalUnitName' => 'PHPMailer',
  1127. 'commonName' => 'PHPMailer Test',
  1128. 'emailAddress' => 'phpmailer@example.com'
  1129. );
  1130. $password = 'password';
  1131. $certfile = 'certfile.txt';
  1132. $keyfile = 'keyfile.txt';
  1133. //Make a new key pair
  1134. $pk = openssl_pkey_new();
  1135. //Create a certificate signing request
  1136. $csr = openssl_csr_new($dn, $pk);
  1137. //Create a self-signed cert
  1138. $cert = openssl_csr_sign($csr, null, $pk, 1);
  1139. //Save the cert
  1140. openssl_x509_export($cert, $certout);
  1141. file_put_contents($certfile, $certout);
  1142. //Save the key
  1143. openssl_pkey_export($pk, $pkeyout, $password);
  1144. file_put_contents($keyfile, $pkeyout);
  1145. $this->Mail->sign(
  1146. $certfile,
  1147. $keyfile,
  1148. $password
  1149. );
  1150. $this->assertTrue($this->Mail->send(), 'S/MIME signing failed');
  1151. unlink($certfile);
  1152. unlink($keyfile);
  1153. }
  1154. /**
  1155. * DKIM Signing tests
  1156. */
  1157. public function testDKIM()
  1158. {
  1159. $this->Mail->Subject .= ': DKIM signing';
  1160. $this->Mail->Body = 'This message is DKIM signed.';
  1161. $this->buildBody();
  1162. $privatekeyfile = 'dkim_private.key';
  1163. //Make a new key pair
  1164. //(2048 bits is the recommended minimum key length -
  1165. //gmail won't accept less than 1024 bits)
  1166. $pk = openssl_pkey_new(
  1167. array(
  1168. 'private_key_bits' => 2048,
  1169. 'private_key_type' => OPENSSL_KEYTYPE_RSA
  1170. )
  1171. );
  1172. openssl_pkey_export_to_file($pk, $privatekeyfile);
  1173. $this->Mail->DKIM_domain = 'example.com';
  1174. $this->Mail->DKIM_private = $privatekeyfile;
  1175. $this->Mail->DKIM_selector = 'phpmailer';
  1176. $this->Mail->DKIM_passphrase = ''; //key is not encrypted
  1177. $this->assertTrue($this->Mail->send(), 'DKIM signed mail failed');
  1178. unlink($privatekeyfile);
  1179. }
  1180. /**
  1181. * Test line break reformatting
  1182. */
  1183. public function testLineBreaks()
  1184. {
  1185. $unixsrc = "hello\nWorld\nAgain\n";
  1186. $macsrc = "hello\rWorld\rAgain\r";
  1187. $windowssrc = "hello\r\nWorld\r\nAgain\r\n";
  1188. $mixedsrc = "hello\nWorld\rAgain\r\n";
  1189. $target = "hello\r\nWorld\r\nAgain\r\n";
  1190. $this->assertEquals($target, PHPMailer::normalizeBreaks($unixsrc), 'UNIX break reformatting failed');
  1191. $this->assertEquals($target, PHPMailer::normalizeBreaks($macsrc), 'Mac break reformatting failed');
  1192. $this->assertEquals($target, PHPMailer::normalizeBreaks($windowssrc), 'Windows break reformatting failed');
  1193. $this->assertEquals($target, PHPMailer::normalizeBreaks($mixedsrc), 'Mixed break reformatting failed');
  1194. }
  1195. /**
  1196. * Test setting and retrieving message ID
  1197. */
  1198. public function testMessageID()
  1199. {
  1200. $this->Mail->Body = 'Test message ID.';
  1201. $id = md5(12345);
  1202. $this->Mail->MessageID = $id;
  1203. $this->buildBody();
  1204. $this->Mail->preSend();
  1205. $lastid = $this->Mail->getLastMessageID();
  1206. $this->assertEquals($lastid, $id, 'Custom Message ID mismatch');
  1207. }
  1208. /**
  1209. * Miscellaneous calls to improve test coverage and some small tests
  1210. */
  1211. public function testMiscellaneous()
  1212. {
  1213. $this->assertEquals('application/pdf', PHPMailer::_mime_types('pdf'), 'MIME TYPE lookup failed');
  1214. $this->Mail->addCustomHeader('SomeHeader: Some Value');
  1215. $this->Mail->clearCustomHeaders();
  1216. $this->Mail->clearAttachments();
  1217. $this->Mail->isHTML(false);
  1218. $this->Mail->isSMTP();
  1219. $this->Mail->isMail();
  1220. $this->Mail->isSendmail();
  1221. $this->Mail->isQmail();
  1222. $this->Mail->setLanguage('fr');
  1223. $this->Mail->Sender = '';
  1224. $this->Mail->createHeader();
  1225. $this->assertFalse($this->Mail->set('x', 'y'), 'Invalid property set succeeded');
  1226. $this->assertTrue($this->Mail->set('Timeout', 11), 'Valid property set failed');
  1227. //Test pathinfo
  1228. $a = '/mnt/files/飛兒樂 團光茫.mp3';
  1229. $q = PHPMailer::mb_pathinfo($a);
  1230. $this->assertEquals($q['dirname'], '/mnt/files', 'UNIX dirname not matched');
  1231. $this->assertEquals($q['basename'], '飛兒樂 團光茫.mp3', 'UNIX basename not matched');
  1232. $this->assertEquals($q['extension'], 'mp3', 'UNIX extension not matched');
  1233. $this->assertEquals($q['filename'], '飛兒樂 團光茫', 'UNIX filename not matched');
  1234. $this->assertEquals(
  1235. PHPMailer::mb_pathinfo($a, PATHINFO_DIRNAME),
  1236. '/mnt/files',
  1237. 'Dirname path element not matched'
  1238. );
  1239. $this->assertEquals(PHPMailer::mb_pathinfo($a, 'filename'), '飛兒樂 團光茫', 'Filename path element not matched');
  1240. $a = 'c:\mnt\files\飛兒樂 團光茫.mp3';
  1241. $q = PHPMailer::mb_pathinfo($a);
  1242. $this->assertEquals($q['dirname'], 'c:\mnt\files', 'Windows dirname not matched');
  1243. $this->assertEquals($q['basename'], '飛兒樂 團光茫.mp3', 'Windows basename not matched');
  1244. $this->assertEquals($q['extension'], 'mp3', 'Windows extension not matched');
  1245. $this->assertEquals($q['filename'], '飛兒樂 團光茫', 'Windows filename not matched');
  1246. }
  1247. /**
  1248. * Use a fake POP3 server to test POP-before-SMTP auth
  1249. * With a known-good login
  1250. */
  1251. public function testPopBeforeSmtpGood()
  1252. {
  1253. //Start a fake POP server
  1254. $pid = shell_exec('nohup ./runfakepopserver.sh >/dev/null 2>/dev/null & printf "%u" $!');
  1255. $this->pids[] = $pid;
  1256. sleep(2);
  1257. //Test a known-good login
  1258. $this->assertTrue(
  1259. POP3::popBeforeSmtp('localhost', 1100, 10, 'user', 'test', $this->Mail->SMTPDebug),
  1260. 'POP before SMTP failed'
  1261. );
  1262. //Kill the fake server
  1263. shell_exec('kill -TERM '.escapeshellarg($pid));
  1264. sleep(2);
  1265. }
  1266. /**
  1267. * Use a fake POP3 server to test POP-before-SMTP auth
  1268. * With a known-bad login
  1269. */
  1270. public function testPopBeforeSmtpBad()
  1271. {
  1272. //Start a fake POP server on a different port
  1273. //so we don't inadvertently connect to the previous instance
  1274. $pid = shell_exec('nohup ./runfakepopserver.sh 1101 >/dev/null 2>/dev/null & printf "%u" $!');
  1275. $this->pids[] = $pid;
  1276. sleep(2);
  1277. //Test a known-bad login
  1278. $this->assertFalse(
  1279. POP3::popBeforeSmtp('localhost', 1101, 10, 'user', 'xxx', $this->Mail->SMTPDebug),
  1280. 'POP before SMTP should have failed'
  1281. );
  1282. shell_exec('kill -TERM '.escapeshellarg($pid));
  1283. sleep(2);
  1284. }
  1285. /**
  1286. * Test SMTP host connections.
  1287. * This test can take a long time, so run it last
  1288. */
  1289. public function testSmtpConnect()
  1290. {
  1291. $this->assertTrue($this->Mail->smtpConnect(), 'SMTP single connect failed');
  1292. $this->Mail->smtpClose();
  1293. $this->Mail->Host = "ssl://localhost:12345;tls://localhost:587;10.10.10.10:54321;localhost:12345;10.10.10.10";
  1294. $this->assertFalse($this->Mail->smtpConnect(), 'SMTP bad multi-connect succeeded');
  1295. $this->Mail->smtpClose();
  1296. $this->Mail->Host = "localhost:12345;10.10.10.10:54321;" . $_REQUEST['mail_host'];
  1297. $this->assertTrue($this->Mail->smtpConnect(), 'SMTP multi-connect failed');
  1298. $this->Mail->smtpClose();
  1299. $this->Mail->Host = " localhost:12345 ; " . $_REQUEST['mail_host'] . ' ';
  1300. $this->assertTrue($this->Mail->smtpConnect(), 'SMTP hosts with stray spaces failed');
  1301. $this->Mail->smtpClose();
  1302. $this->Mail->Host = $_REQUEST['mail_host'];
  1303. //Need to pick a harmless option so as not cause problems of its own! socket:bind doesn't work with Travis-CI
  1304. $this->assertTrue(
  1305. $this->Mail->smtpConnect(array('ssl' => array('verify_depth' => 10))),
  1306. 'SMTP connect with options failed'
  1307. );
  1308. }
  1309. }
  1310. /**
  1311. * This is a sample form for setting appropriate test values through a browser
  1312. * These values can also be set using a file called testbootstrap.php (not in svn) in the same folder as this script
  1313. * which is probably more useful if you run these tests a lot
  1314. * <html>
  1315. * <body>
  1316. * <h3>phpmailer Unit Test</h3>
  1317. * By entering a SMTP hostname it will automatically perform tests with SMTP.
  1318. *
  1319. * <form name="phpmailer_unit" action=__FILE__ method="get">
  1320. * <input type="hidden" name="submitted" value="1"/>
  1321. * From Address: <input type="text" size="50" name="mail_from" value="<?php echo get("mail_from"); ?>"/>
  1322. * <br/>
  1323. * To Address: <input type="text" size="50" name="mail_to" value="<?php echo get("mail_to"); ?>"/>
  1324. * <br/>
  1325. * Cc Address: <input type="text" size="50" name="mail_cc" value="<?php echo get("mail_cc"); ?>"/>
  1326. * <br/>
  1327. * SMTP Hostname: <input type="text" size="50" name="mail_host" value="<?php echo get("mail_host"); ?>"/>
  1328. * <p/>
  1329. * <input type="submit" value="Run Test"/>
  1330. *
  1331. * </form>
  1332. * </body>
  1333. * </html>
  1334. */