phpmailerTest.php 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166
  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 realpath('../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. * @var PHPMailer
  26. */
  27. public $Mail;
  28. /**
  29. * Holds the SMTP mail host.
  30. * @public
  31. * @var string
  32. */
  33. public $Host = '';
  34. /**
  35. * Holds the change log.
  36. * @private
  37. * @var string[]
  38. */
  39. public $ChangeLog = array();
  40. /**
  41. * Holds the note log.
  42. * @private
  43. * @var string[]
  44. */
  45. public $NoteLog = array();
  46. /**
  47. * PIDs of any processes we need to kill
  48. * @var array
  49. * @access private
  50. */
  51. private $pids = array();
  52. /**
  53. * Default include path
  54. * @var string
  55. */
  56. const INCLUDE_DIR = '../';
  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 = 3; //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->addReplyTo('no_reply@phpmailer.example.com', 'Reply Guy');
  96. $this->Mail->Sender = 'unit_test@phpmailer.example.com';
  97. if (strlen($this->Mail->Host) > 0) {
  98. $this->Mail->Mailer = 'smtp';
  99. } else {
  100. $this->Mail->Mailer = 'mail';
  101. }
  102. if (array_key_exists('mail_to', $_REQUEST)) {
  103. $this->setAddress($_REQUEST['mail_to'], 'Test User', 'to');
  104. }
  105. if (array_key_exists('mail_cc', $_REQUEST) and strlen($_REQUEST['mail_cc']) > 0) {
  106. $this->setAddress($_REQUEST['mail_cc'], 'Carbon User', 'cc');
  107. }
  108. }
  109. /**
  110. * Run after each test is completed.
  111. */
  112. public function tearDown()
  113. {
  114. // Clean global variables
  115. $this->Mail = null;
  116. $this->ChangeLog = array();
  117. $this->NoteLog = array();
  118. foreach ($this->pids as $pid) {
  119. $p = escapeshellarg($pid);
  120. shell_exec("ps $p && kill -TERM $p");
  121. }
  122. }
  123. /**
  124. * Build the body of the message in the appropriate format.
  125. *
  126. * @private
  127. * @return void
  128. */
  129. public function buildBody()
  130. {
  131. $this->checkChanges();
  132. // Determine line endings for message
  133. if ($this->Mail->ContentType == 'text/html' || strlen($this->Mail->AltBody) > 0) {
  134. $eol = "<br>\r\n";
  135. $bullet_start = '<li>';
  136. $bullet_end = "</li>\r\n";
  137. $list_start = "<ul>\r\n";
  138. $list_end = "</ul>\r\n";
  139. } else {
  140. $eol = "\r\n";
  141. $bullet_start = ' - ';
  142. $bullet_end = "\r\n";
  143. $list_start = '';
  144. $list_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. $ReportBody .= 'CharSet: ' . $this->Mail->CharSet . $eol;
  153. if (strlen($this->Mail->Host) > 0) {
  154. $ReportBody .= 'Host: ' . $this->Mail->Host . $eol;
  155. }
  156. // If attachments then create an attachment list
  157. $attachments = $this->Mail->getAttachments();
  158. if (count($attachments) > 0) {
  159. $ReportBody .= 'Attachments:' . $eol;
  160. $ReportBody .= $list_start;
  161. foreach ($attachments as $attachment) {
  162. $ReportBody .= $bullet_start . 'Name: ' . $attachment[1] . ', ';
  163. $ReportBody .= 'Encoding: ' . $attachment[3] . ', ';
  164. $ReportBody .= 'Type: ' . $attachment[4] . $bullet_end;
  165. }
  166. $ReportBody .= $list_end . $eol;
  167. }
  168. // If there are changes then list them
  169. if (count($this->ChangeLog) > 0) {
  170. $ReportBody .= 'Changes' . $eol;
  171. $ReportBody .= '-------' . $eol;
  172. $ReportBody .= $list_start;
  173. for ($i = 0; $i < count($this->ChangeLog); $i++) {
  174. $ReportBody .= $bullet_start . $this->ChangeLog[$i][0] . ' was changed to [' .
  175. $this->ChangeLog[$i][1] . ']' . $bullet_end;
  176. }
  177. $ReportBody .= $list_end . $eol . $eol;
  178. }
  179. // If there are notes then list them
  180. if (count($this->NoteLog) > 0) {
  181. $ReportBody .= 'Notes' . $eol;
  182. $ReportBody .= '-----' . $eol;
  183. $ReportBody .= $list_start;
  184. for ($i = 0; $i < count($this->NoteLog); $i++) {
  185. $ReportBody .= $bullet_start . $this->NoteLog[$i] . $bullet_end;
  186. }
  187. $ReportBody .= $list_end;
  188. }
  189. // Re-attach the original body
  190. $this->Mail->Body .= $eol . $ReportBody;
  191. }
  192. /**
  193. * Check which default settings have been changed for the report.
  194. * @private
  195. * @return void
  196. */
  197. public function checkChanges()
  198. {
  199. if ($this->Mail->Priority != 3) {
  200. $this->addChange('Priority', $this->Mail->Priority);
  201. }
  202. if ($this->Mail->Encoding != '8bit') {
  203. $this->addChange('Encoding', $this->Mail->Encoding);
  204. }
  205. if ($this->Mail->CharSet != 'iso-8859-1') {
  206. $this->addChange('CharSet', $this->Mail->CharSet);
  207. }
  208. if ($this->Mail->Sender != '') {
  209. $this->addChange('Sender', $this->Mail->Sender);
  210. }
  211. if ($this->Mail->WordWrap != 0) {
  212. $this->addChange('WordWrap', $this->Mail->WordWrap);
  213. }
  214. if ($this->Mail->Mailer != 'mail') {
  215. $this->addChange('Mailer', $this->Mail->Mailer);
  216. }
  217. if ($this->Mail->Port != 25) {
  218. $this->addChange('Port', $this->Mail->Port);
  219. }
  220. if ($this->Mail->Helo != 'localhost.localdomain') {
  221. $this->addChange('Helo', $this->Mail->Helo);
  222. }
  223. if ($this->Mail->SMTPAuth) {
  224. $this->addChange('SMTPAuth', 'true');
  225. }
  226. }
  227. /**
  228. * Add a changelog entry.
  229. * @access private
  230. * @param string $sName
  231. * @param string $sNewValue
  232. * @return void
  233. */
  234. public function addChange($sName, $sNewValue)
  235. {
  236. $this->ChangeLog[] = array($sName, $sNewValue);
  237. }
  238. /**
  239. * Adds a simple note to the message.
  240. * @public
  241. * @param string $sValue
  242. * @return void
  243. */
  244. public function addNote($sValue)
  245. {
  246. $this->NoteLog[] = $sValue;
  247. }
  248. /**
  249. * Adds all of the addresses
  250. * @access public
  251. * @param string $sAddress
  252. * @param string $sName
  253. * @param string $sType
  254. * @return boolean
  255. */
  256. public function setAddress($sAddress, $sName = '', $sType = 'to')
  257. {
  258. switch ($sType) {
  259. case 'to':
  260. return $this->Mail->addAddress($sAddress, $sName);
  261. case 'cc':
  262. return $this->Mail->addCC($sAddress, $sName);
  263. case 'bcc':
  264. return $this->Mail->addBCC($sAddress, $sName);
  265. }
  266. return false;
  267. }
  268. /**
  269. * Check that we have loaded default test params.
  270. * Pretty much everything will fail due to unset recipient if this is not done.
  271. */
  272. public function testBootstrap()
  273. {
  274. $this->assertTrue(
  275. file_exists('testbootstrap.php'),
  276. 'Test config params missing - copy testbootstrap.php to testbootstrap-dist.php and change as appropriate'
  277. );
  278. }
  279. /**
  280. * Test CRAM-MD5 authentication.
  281. * Needs a connection to a server that supports this auth mechanism, so commented out by default
  282. */
  283. public function testAuthCRAMMD5()
  284. {
  285. $this->Mail->Host = 'hostname';
  286. $this->Mail->Port = 587;
  287. $this->Mail->SMTPAuth = true;
  288. $this->Mail->SMTPSecure = 'tls';
  289. $this->Mail->AuthType = 'CRAM-MD5';
  290. $this->Mail->Username = 'username';
  291. $this->Mail->Password = 'password';
  292. $this->Mail->Body = 'Test body';
  293. $this->Mail->Subject .= ': Auth CRAM-MD5';
  294. $this->Mail->From = 'from@example.com';
  295. $this->Mail->Sender = 'from@example.com';
  296. $this->Mail->clearAllRecipients();
  297. $this->Mail->addAddress('user@example.com');
  298. //$this->assertTrue($this->mail->send(), $this->mail->ErrorInfo);
  299. }
  300. /**
  301. * Test email address validation.
  302. * Test addresses obtained from http://isemail.info
  303. * Some failing cases commented out that are apparently up for debate!
  304. */
  305. public function testValidate()
  306. {
  307. $validaddresses = array(
  308. 'first@iana.org',
  309. 'first.last@iana.org',
  310. '1234567890123456789012345678901234567890123456789012345678901234@iana.org',
  311. '"first\"last"@iana.org',
  312. '"first@last"@iana.org',
  313. '"first\last"@iana.org',
  314. 'first.last@[12.34.56.78]',
  315. 'first.last@[IPv6:::12.34.56.78]',
  316. 'first.last@[IPv6:1111:2222:3333::4444:12.34.56.78]',
  317. 'first.last@[IPv6:1111:2222:3333:4444:5555:6666:12.34.56.78]',
  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::]',
  321. 'first.last@[IPv6:1111:2222:3333:4444:5555:6666:7777:8888]',
  322. 'first.last@x23456789012345678901234567890123456789012345678901234567890123.iana.org',
  323. 'first.last@3com.com',
  324. 'first.last@123.iana.org',
  325. '"first\last"@iana.org',
  326. 'first.last@[IPv6:1111:2222:3333::4444:5555:12.34.56.78]',
  327. 'first.last@[IPv6:1111:2222:3333::4444:5555:6666:7777]',
  328. 'first.last@example.123',
  329. 'first.last@com',
  330. '"Abc\@def"@iana.org',
  331. '"Fred\ Bloggs"@iana.org',
  332. '"Joe.\Blow"@iana.org',
  333. '"Abc@def"@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. //This is a valid RCC5322 address, but we don't want to allow it for obvious reasons!
  581. "(\r\n RCPT TO:user@example.com\r\n DATA \\\nSubject: spam10\\\n\r\n Hello,\r\n".
  582. " this is a spam mail.\\\n.\r\n QUIT\r\n ) a@example.net"
  583. );
  584. // IDNs in Unicode and ASCII forms.
  585. $unicodeaddresses = array(
  586. 'first.last@bücher.ch',
  587. 'first.last@кто.рф',
  588. 'first.last@phplíst.com',
  589. );
  590. $asciiaddresses = array(
  591. 'first.last@xn--bcher-kva.ch',
  592. 'first.last@xn--j1ail.xn--p1ai',
  593. 'first.last@xn--phplst-6va.com',
  594. );
  595. $goodfails = array();
  596. foreach (array_merge($validaddresses, $asciiaddresses) as $address) {
  597. if (!PHPMailer::validateAddress($address)) {
  598. $goodfails[] = $address;
  599. }
  600. }
  601. $badpasses = array();
  602. foreach (array_merge($invalidaddresses, $unicodeaddresses) as $address) {
  603. if (PHPMailer::validateAddress($address)) {
  604. $badpasses[] = $address;
  605. }
  606. }
  607. $err = '';
  608. if (count($goodfails) > 0) {
  609. $err .= "Good addresses that failed validation:\n";
  610. $err .= implode("\n", $goodfails);
  611. }
  612. if (count($badpasses) > 0) {
  613. if (!empty($err)) {
  614. $err .= "\n\n";
  615. }
  616. $err .= "Bad addresses that passed validation:\n";
  617. $err .= implode("\n", $badpasses);
  618. }
  619. $this->assertEmpty($err, $err);
  620. //For coverage
  621. $this->assertTrue(PHPMailer::validateAddress('test@example.com', 'auto'));
  622. $this->assertFalse(PHPMailer::validateAddress('test@example.com.', 'auto'));
  623. $this->assertTrue(PHPMailer::validateAddress('test@example.com', 'pcre'));
  624. $this->assertFalse(PHPMailer::validateAddress('test@example.com.', 'pcre'));
  625. $this->assertTrue(PHPMailer::validateAddress('test@example.com', 'pcre8'));
  626. $this->assertFalse(PHPMailer::validateAddress('test@example.com.', 'pcre8'));
  627. $this->assertTrue(PHPMailer::validateAddress('test@example.com', 'php'));
  628. $this->assertFalse(PHPMailer::validateAddress('test@example.com.', 'php'));
  629. $this->assertTrue(PHPMailer::validateAddress('test@example.com', 'noregex'));
  630. $this->assertFalse(PHPMailer::validateAddress('bad', 'noregex'));
  631. }
  632. /**
  633. * Test injecting a custom validator.
  634. */
  635. public function testCustomValidator()
  636. {
  637. //Inject a one-off custom validator
  638. $this->assertTrue(
  639. PHPMailer::validateAddress(
  640. 'user@example.com',
  641. function ($address) {
  642. return (strpos($address, '@') !== false);
  643. }
  644. ),
  645. 'Custom validator false negative'
  646. );
  647. $this->assertFalse(
  648. PHPMailer::validateAddress(
  649. 'userexample.com',
  650. function ($address) {
  651. return (strpos($address, '@') !== false);
  652. }
  653. ),
  654. 'Custom validator false positive'
  655. );
  656. //Set the default validator to an injected function
  657. PHPMailer::$validator = function ($address) {
  658. return ('user@example.com' === $address);
  659. };
  660. $this->assertTrue(
  661. $this->Mail->addAddress('user@example.com'),
  662. 'Custom default validator false negative'
  663. );
  664. $this->assertFalse(
  665. //Need to pick a failing value which would pass all other validators
  666. //to be sure we're using our custom one
  667. $this->Mail->addAddress('bananas@example.com'),
  668. 'Custom default validator false positive'
  669. );
  670. //Set default validator to PHP built-in
  671. PHPMailer::$validator = 'php';
  672. $this->assertFalse(
  673. //This is a valid address that FILTER_VALIDATE_EMAIL thinks is invalid
  674. $this->Mail->addAddress('first.last@example.123'),
  675. 'PHP validator not behaving as expected'
  676. );
  677. }
  678. /**
  679. * Word-wrap an ASCII message.
  680. */
  681. public function testWordWrap()
  682. {
  683. $this->Mail->WordWrap = 40;
  684. $my_body = str_repeat(
  685. 'Here is the main body of this message. It should ' .
  686. 'be quite a few lines. It should be wrapped at ' .
  687. '40 characters. Make sure that it is. ',
  688. 10
  689. );
  690. $nBodyLen = strlen($my_body);
  691. $my_body .= "\n\nThis is the above body length: " . $nBodyLen;
  692. $this->Mail->Body = $my_body;
  693. $this->Mail->Subject .= ': Wordwrap';
  694. $this->buildBody();
  695. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  696. }
  697. /**
  698. * Word-wrap a multibyte message.
  699. */
  700. public function testWordWrapMultibyte()
  701. {
  702. $this->Mail->WordWrap = 40;
  703. $my_body = str_repeat(
  704. '飛兒樂 團光茫 飛兒樂 團光茫 飛兒樂 團光茫 飛兒樂 團光茫 ' .
  705. '飛飛兒樂 團光茫兒樂 團光茫飛兒樂 團光飛兒樂 團光茫飛兒樂 團光茫兒樂 團光茫 ' .
  706. '飛兒樂 團光茫飛兒樂 團飛兒樂 團光茫光茫飛兒樂 團光茫. ',
  707. 10
  708. );
  709. $nBodyLen = strlen($my_body);
  710. $my_body .= "\n\nThis is the above body length: " . $nBodyLen;
  711. $this->Mail->Body = $my_body;
  712. $this->Mail->Subject .= ': Wordwrap multibyte';
  713. $this->buildBody();
  714. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  715. }
  716. /**
  717. * Test low priority.
  718. */
  719. public function testLowPriority()
  720. {
  721. $this->Mail->Priority = 5;
  722. $this->Mail->Body = 'Here is the main body. There should be ' .
  723. 'a reply to address in this message.';
  724. $this->Mail->Subject .= ': Low Priority';
  725. $this->Mail->addReplyTo('nobody@nobody.com', 'Nobody (Unit Test)');
  726. $this->buildBody();
  727. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  728. }
  729. /**
  730. * Simple plain file attachment test.
  731. */
  732. public function testMultiplePlainFileAttachment()
  733. {
  734. $this->Mail->Body = 'Here is the text body';
  735. $this->Mail->Subject .= ': Plain + Multiple FileAttachments';
  736. if (!$this->Mail->addAttachment(realpath(self::INCLUDE_DIR . 'examples/images/phpmailer.png'))) {
  737. $this->assertTrue(false, $this->Mail->ErrorInfo);
  738. return;
  739. }
  740. if (!$this->Mail->addAttachment(__FILE__, 'test.txt')) {
  741. $this->assertTrue(false, $this->Mail->ErrorInfo);
  742. return;
  743. }
  744. $this->buildBody();
  745. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  746. }
  747. /**
  748. * Simple plain string attachment test.
  749. */
  750. public function testPlainStringAttachment()
  751. {
  752. $this->Mail->Body = 'Here is the text body';
  753. $this->Mail->Subject .= ': Plain + StringAttachment';
  754. $sAttachment = 'These characters are the content of the ' .
  755. "string attachment.\nThis might be taken from a " .
  756. 'database or some other such thing. ';
  757. $this->Mail->addStringAttachment($sAttachment, 'string_attach.txt');
  758. $this->buildBody();
  759. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  760. }
  761. /**
  762. * Plain quoted-printable message.
  763. */
  764. public function testQuotedPrintable()
  765. {
  766. $this->Mail->Body = 'Here is the main body';
  767. $this->Mail->Subject .= ': Plain + Quoted-printable';
  768. $this->Mail->Encoding = 'quoted-printable';
  769. $this->buildBody();
  770. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  771. //Check that a quoted printable encode and decode results in the same as went in
  772. $t = file_get_contents(__FILE__); //Use this file as test content
  773. //Force line breaks to UNIX-style
  774. $t = str_replace(array("\r\n", "\r"), "\n", $t);
  775. $this->assertEquals(
  776. $t,
  777. quoted_printable_decode($this->Mail->encodeQP($t)),
  778. 'Quoted-Printable encoding round-trip failed'
  779. );
  780. $this->assertEquals(
  781. $this->Mail->encodeQP($t),
  782. $this->Mail->encodeQPphp($t),
  783. 'Quoted-Printable BC wrapper failed'
  784. );
  785. //Force line breaks to Windows-style
  786. $t = str_replace("\n", "\r\n", $t);
  787. $this->assertEquals(
  788. $t,
  789. quoted_printable_decode($this->Mail->encodeQP($t)),
  790. 'Quoted-Printable encoding round-trip failed (Windows line breaks)'
  791. );
  792. }
  793. /**
  794. * Send an HTML message.
  795. */
  796. public function testHtml()
  797. {
  798. $this->Mail->isHTML(true);
  799. $this->Mail->Subject .= ": HTML only";
  800. $this->Mail->Body = <<<EOT
  801. <html>
  802. <head>
  803. <title>HTML email test</title>
  804. </head>
  805. <body>
  806. <h1>PHPMailer does HTML!</h1>
  807. <p>This is a <strong>test message</strong> written in HTML.<br>
  808. Go to <a href="https://github.com/PHPMailer/PHPMailer/">https://github.com/PHPMailer/PHPMailer/</a>
  809. for new versions of PHPMailer.</p>
  810. <p>Thank you!</p>
  811. </body>
  812. </html>
  813. EOT;
  814. $this->buildBody();
  815. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  816. $msg = $this->Mail->getSentMIMEMessage();
  817. $this->assertNotContains("\r\n\r\nMIME-Version:", $msg, 'Incorrect MIME headers');
  818. }
  819. /**
  820. * Send a message containing ISO-8859-1 text.
  821. */
  822. public function testHtmlIso8859()
  823. {
  824. $this->Mail->isHTML(true);
  825. $this->Mail->Subject .= ": ISO-8859-1 HTML";
  826. $this->Mail->CharSet = 'iso-8859-1';
  827. //This file is in ISO-8859-1 charset
  828. //Needs to be external because this file is in UTF-8
  829. $content = file_get_contents(realpath('../examples/contents.html'));
  830. // This is the string 'éèîüçÅñæß' in ISO-8859-1, base-64 encoded
  831. $check = base64_decode('6eju/OfF8ebf');
  832. //Make sure it really is in ISO-8859-1!
  833. $this->Mail->msgHTML(
  834. mb_convert_encoding(
  835. $content,
  836. "ISO-8859-1",
  837. mb_detect_encoding($content, "UTF-8, ISO-8859-1, ISO-8859-15", true)
  838. ),
  839. realpath(self::INCLUDE_DIR . 'examples')
  840. );
  841. $this->buildBody();
  842. $this->assertTrue(
  843. strpos($this->Mail->Body, $check) !== false,
  844. 'ISO message body does not contain expected text'
  845. );
  846. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  847. }
  848. /**
  849. * Send a message containing multilingual UTF-8 text.
  850. */
  851. public function testHtmlUtf8()
  852. {
  853. $this->Mail->isHTML(true);
  854. $this->Mail->Subject .= ": UTF-8 HTML";
  855. $this->Mail->CharSet = 'UTF-8';
  856. $this->Mail->Body = <<<EOT
  857. <html>
  858. <head>
  859. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  860. <title>HTML email test</title>
  861. </head>
  862. <body>
  863. <p>Chinese text: 郵件內容為空</p>
  864. <p>Russian text: Пустое тело сообщения</p>
  865. <p>Armenian text: Հաղորդագրությունը դատարկ է</p>
  866. <p>Czech text: Prázdné tělo zprávy</p>
  867. </body>
  868. </html>
  869. EOT;
  870. $this->buildBody();
  871. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  872. $msg = $this->Mail->getSentMIMEMessage();
  873. $this->assertNotContains("\r\n\r\nMIME-Version:", $msg, 'Incorrect MIME headers');
  874. }
  875. /**
  876. * Send a message containing multilingual UTF-8 text with an embedded image.
  877. */
  878. public function testUtf8WithEmbeddedImage()
  879. {
  880. $this->Mail->isHTML(true);
  881. $this->Mail->Subject .= ": UTF-8 with embedded image";
  882. $this->Mail->CharSet = 'UTF-8';
  883. $this->Mail->Body = <<<EOT
  884. <html>
  885. <head>
  886. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  887. <title>HTML email test</title>
  888. </head>
  889. <body>
  890. <p>Chinese text: 郵件內容為空</p>
  891. <p>Russian text: Пустое тело сообщения</p>
  892. <p>Armenian text: Հաղորդագրությունը դատարկ է</p>
  893. <p>Czech text: Prázdné tělo zprávy</p>
  894. Embedded Image: <img alt="phpmailer" src="cid:my-attach">
  895. </body>
  896. </html>
  897. EOT;
  898. $this->Mail->addEmbeddedImage(
  899. realpath(self::INCLUDE_DIR . 'examples/images/phpmailer.png'),
  900. 'my-attach',
  901. 'phpmailer.png',
  902. 'base64',
  903. 'image/png'
  904. );
  905. $this->buildBody();
  906. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  907. }
  908. /**
  909. * Send a message containing multilingual UTF-8 text.
  910. */
  911. public function testPlainUtf8()
  912. {
  913. $this->Mail->isHTML(false);
  914. $this->Mail->Subject .= ": UTF-8 plain text";
  915. $this->Mail->CharSet = 'UTF-8';
  916. $this->Mail->Body = <<<EOT
  917. Chinese text: 郵件內容為空
  918. Russian text: Пустое тело сообщения
  919. Armenian text: Հաղորդագրությունը դատարկ է
  920. Czech text: Prázdné tělo zprávy
  921. EOT;
  922. $this->buildBody();
  923. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  924. $msg = $this->Mail->getSentMIMEMessage();
  925. $this->assertNotContains("\r\n\r\nMIME-Version:", $msg, 'Incorrect MIME headers');
  926. }
  927. /**
  928. * Test simple message builder and html2text converters
  929. */
  930. public function testMsgHTML()
  931. {
  932. $message = file_get_contents(realpath(self::INCLUDE_DIR . 'examples/contentsutf8.html'));
  933. $this->Mail->CharSet = 'utf-8';
  934. $this->Mail->Body = '';
  935. $this->Mail->AltBody = '';
  936. //Uses internal HTML to text conversion
  937. $this->Mail->msgHTML($message, realpath(self::INCLUDE_DIR . 'examples'));
  938. $this->Mail->Subject .= ': msgHTML';
  939. $this->assertNotEmpty($this->Mail->Body, 'Body not set by msgHTML');
  940. $this->assertNotEmpty($this->Mail->AltBody, 'AltBody not set by msgHTML');
  941. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  942. //Again, using a custom HTML to text converter
  943. $this->Mail->AltBody = '';
  944. $this->Mail->msgHTML($message, realpath(self::INCLUDE_DIR . 'examples'), function ($html) {
  945. return strtoupper(strip_tags($html));
  946. });
  947. $this->Mail->Subject .= ' + custom html2text';
  948. $this->assertNotEmpty($this->Mail->AltBody, 'Custom AltBody not set by msgHTML');
  949. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  950. }
  951. /**
  952. * Simple HTML and attachment test
  953. */
  954. public function testHTMLAttachment()
  955. {
  956. $this->Mail->Body = 'This is the <strong>HTML</strong> part of the email.';
  957. $this->Mail->Subject .= ': HTML + Attachment';
  958. $this->Mail->isHTML(true);
  959. if (!$this->Mail->addAttachment(
  960. realpath(self::INCLUDE_DIR . 'examples/images/phpmailer_mini.png'), 'phpmailer_mini.png')
  961. ) {
  962. $this->assertTrue(false, $this->Mail->ErrorInfo);
  963. return;
  964. }
  965. //Make sure that trying to attach a nonexistent file fails
  966. $this->assertFalse($this->Mail->addAttachment(__FILE__ . md5(microtime()), 'nonexistent_file.txt'));
  967. $this->buildBody();
  968. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  969. }
  970. /**
  971. * Test embedded image without a name
  972. */
  973. public function testHTMLStringEmbedNoName()
  974. {
  975. $this->Mail->Body = 'This is the <strong>HTML</strong> part of the email.';
  976. $this->Mail->Subject .= ': HTML + unnamed embedded image';
  977. $this->Mail->isHTML(true);
  978. if (!$this->Mail->addStringEmbeddedImage(
  979. file_get_contents(realpath(self::INCLUDE_DIR . 'examples/images/phpmailer_mini.png')),
  980. md5('phpmailer_mini.png').'@phpmailer.0',
  981. '', //intentionally empty name
  982. 'base64',
  983. 'image/png',
  984. 'inline')
  985. ) {
  986. $this->assertTrue(false, $this->Mail->ErrorInfo);
  987. return;
  988. }
  989. $this->buildBody();
  990. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  991. }
  992. /**
  993. * Simple HTML and multiple attachment test
  994. */
  995. public function testHTMLMultiAttachment()
  996. {
  997. $this->Mail->Body = 'This is the <strong>HTML</strong> part of the email.';
  998. $this->Mail->Subject .= ': HTML + multiple Attachment';
  999. $this->Mail->isHTML(true);
  1000. if (!$this->Mail->addAttachment(realpath(self::INCLUDE_DIR . 'examples/images/phpmailer_mini.png'), 'phpmailer_mini.png')) {
  1001. $this->assertTrue(false, $this->Mail->ErrorInfo);
  1002. return;
  1003. }
  1004. if (!$this->Mail->addAttachment(realpath(self::INCLUDE_DIR . 'examples/images/phpmailer.png'), 'phpmailer.png')) {
  1005. $this->assertTrue(false, $this->Mail->ErrorInfo);
  1006. return;
  1007. }
  1008. $this->buildBody();
  1009. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  1010. }
  1011. /**
  1012. * An embedded attachment test.
  1013. */
  1014. public function testEmbeddedImage()
  1015. {
  1016. $this->Mail->Body = 'Embedded Image: <img alt="phpmailer" src="cid:my-attach">' .
  1017. 'Here is an image!';
  1018. $this->Mail->Subject .= ': Embedded Image';
  1019. $this->Mail->isHTML(true);
  1020. if (!$this->Mail->addEmbeddedImage(
  1021. realpath(self::INCLUDE_DIR . 'examples/images/phpmailer.png'),
  1022. 'my-attach',
  1023. 'phpmailer.png',
  1024. 'base64',
  1025. 'image/png'
  1026. )
  1027. ) {
  1028. $this->assertTrue(false, $this->Mail->ErrorInfo);
  1029. return;
  1030. }
  1031. $this->buildBody();
  1032. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  1033. //For code coverage
  1034. $this->Mail->addEmbeddedImage('thisfiledoesntexist', 'xyz'); //Non-existent file
  1035. $this->Mail->addEmbeddedImage(__FILE__, '123'); //Missing name
  1036. }
  1037. /**
  1038. * An embedded attachment test.
  1039. */
  1040. public function testMultiEmbeddedImage()
  1041. {
  1042. $this->Mail->Body = 'Embedded Image: <img alt="phpmailer" src="cid:my-attach">' .
  1043. 'Here is an image!</a>';
  1044. $this->Mail->Subject .= ': Embedded Image + Attachment';
  1045. $this->Mail->isHTML(true);
  1046. if (!$this->Mail->addEmbeddedImage(
  1047. realpath(self::INCLUDE_DIR . 'examples/images/phpmailer.png'),
  1048. 'my-attach',
  1049. 'phpmailer.png',
  1050. 'base64',
  1051. 'image/png'
  1052. )
  1053. ) {
  1054. $this->assertTrue(false, $this->Mail->ErrorInfo);
  1055. return;
  1056. }
  1057. if (!$this->Mail->addAttachment(__FILE__, 'test.txt')) {
  1058. $this->assertTrue(false, $this->Mail->ErrorInfo);
  1059. return;
  1060. }
  1061. $this->buildBody();
  1062. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  1063. }
  1064. /**
  1065. * Simple multipart/alternative test.
  1066. */
  1067. public function testAltBody()
  1068. {
  1069. $this->Mail->Body = 'This is the <strong>HTML</strong> part of the email.';
  1070. $this->Mail->AltBody = 'Here is the text body of this message. ' .
  1071. 'It should be quite a few lines. It should be wrapped at the ' .
  1072. '40 characters. Make sure that it is.';
  1073. $this->Mail->WordWrap = 40;
  1074. $this->addNote('This is a mulipart alternative email');
  1075. $this->Mail->Subject .= ': AltBody + Word Wrap';
  1076. $this->buildBody();
  1077. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  1078. }
  1079. /**
  1080. * Simple HTML and attachment test
  1081. */
  1082. public function testAltBodyAttachment()
  1083. {
  1084. $this->Mail->Body = 'This is the <strong>HTML</strong> part of the email.';
  1085. $this->Mail->AltBody = 'This is the text part of the email.';
  1086. $this->Mail->Subject .= ': AltBody + Attachment';
  1087. $this->Mail->isHTML(true);
  1088. if (!$this->Mail->addAttachment(__FILE__, 'test_attach.txt')) {
  1089. $this->assertTrue(false, $this->Mail->ErrorInfo);
  1090. return;
  1091. }
  1092. $this->buildBody();
  1093. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  1094. }
  1095. /**
  1096. * iCal event test.
  1097. */
  1098. public function testIcal()
  1099. {
  1100. //Standalone ICS tests
  1101. require_once realpath(self::INCLUDE_DIR . 'extras/EasyPeasyICS.php');
  1102. $ICS = new EasyPeasyICS("PHPMailer test calendar");
  1103. $this->assertNotEmpty(
  1104. $ICS->addEvent(
  1105. strtotime('tomorrow 10:00 Europe/Paris'),
  1106. strtotime('tomorrow 11:00 Europe/Paris'),
  1107. 'PHPMailer iCal test',
  1108. 'A test of PHPMailer iCal support',
  1109. 'https://github.com/PHPMailer/PHPMailer'
  1110. ),
  1111. 'Generated event string is empty'
  1112. );
  1113. $ICS->addEvent(
  1114. strtotime('tomorrow 10:00 Europe/Paris'),
  1115. strtotime('tomorrow 11:00 Europe/Paris'),
  1116. 'PHPMailer iCal test',
  1117. 'A test of PHPMailer iCal support',
  1118. 'https://github.com/PHPMailer/PHPMailer'
  1119. );
  1120. $events = $ICS->getEvents();
  1121. $this->assertEquals(2, count($events), 'Event count mismatch');
  1122. $ICS->clearEvents();
  1123. $events = $ICS->getEvents();
  1124. $this->assertEquals(0, count($events), 'Event clearing failed');
  1125. $ICS->setName('test');
  1126. $this->assertEquals('test', $ICS->getName(), 'Setting ICS name failed');
  1127. $this->assertNotEmpty($ICS->render(false), 'Empty calendar');
  1128. //Need to test generated output but PHPUnit isn't playing nice
  1129. //$rendered = $ICS->render();
  1130. //Test sending an ICS
  1131. $this->Mail->Body = 'This is the <strong>HTML</strong> part of the email.';
  1132. $this->Mail->AltBody = 'This is the text part of the email.';
  1133. $this->Mail->Subject .= ': iCal';
  1134. $this->Mail->isHTML(true);
  1135. $this->buildBody();
  1136. $this->Mail->Ical = $ICS->render(false);
  1137. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  1138. }
  1139. /**
  1140. * Test sending multiple messages with separate connections.
  1141. */
  1142. public function testMultipleSend()
  1143. {
  1144. $this->Mail->Body = 'Sending two messages without keepalive';
  1145. $this->buildBody();
  1146. $subject = $this->Mail->Subject;
  1147. $this->Mail->Subject = $subject . ': SMTP 1';
  1148. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  1149. $this->Mail->Subject = $subject . ': SMTP 2';
  1150. $this->Mail->Sender = 'blah@example.com';
  1151. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  1152. }
  1153. /**
  1154. * Test sending using SendMail.
  1155. */
  1156. public function testSendmailSend()
  1157. {
  1158. $this->Mail->Body = 'Sending via sendmail';
  1159. $this->buildBody();
  1160. $subject = $this->Mail->Subject;
  1161. $this->Mail->Subject = $subject . ': sendmail';
  1162. $this->Mail->isSendmail();
  1163. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  1164. }
  1165. /**
  1166. * Test sending using Qmail.
  1167. */
  1168. public function testQmailSend()
  1169. {
  1170. //Only run if we have qmail installed
  1171. if (file_exists('/var/qmail/bin/qmail-inject')) {
  1172. $this->Mail->Body = 'Sending via qmail';
  1173. $this->BuildBody();
  1174. $subject = $this->Mail->Subject;
  1175. $this->Mail->Subject = $subject . ': qmail';
  1176. $this->Mail->IsQmail();
  1177. $this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
  1178. } else {
  1179. $this->markTestSkipped('Qmail is not installed');
  1180. }
  1181. }
  1182. /**
  1183. * Test sending using PHP mail() function.
  1184. */
  1185. public function testMailSend()
  1186. {
  1187. $sendmail = ini_get('sendmail_path');
  1188. //No path in sendmail_path
  1189. if (strpos($sendmail, '/') === false) {
  1190. ini_set('sendmail_path', '/usr/sbin/sendmail -t -i ');
  1191. }
  1192. $this->Mail->Body = 'Sending via mail()';
  1193. $this->buildBody();
  1194. $this->Mail->Subject = $this->Mail->Subject . ': mail()';
  1195. $this->Mail->isMail();
  1196. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  1197. $msg = $this->Mail->getSentMIMEMessage();
  1198. $this->assertNotContains("\r\n\r\nMIME-Version:", $msg, 'Incorrect MIME headers');
  1199. }
  1200. /**
  1201. * Test sending an empty body.
  1202. */
  1203. public function testEmptyBody()
  1204. {
  1205. $this->buildBody();
  1206. $this->Mail->Body = '';
  1207. $this->Mail->Subject = $this->Mail->Subject . ': Empty Body';
  1208. $this->Mail->isMail();
  1209. $this->Mail->AllowEmpty = true;
  1210. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  1211. $this->Mail->AllowEmpty = false;
  1212. $this->assertFalse($this->Mail->send(), $this->Mail->ErrorInfo);
  1213. }
  1214. /**
  1215. * Test constructing a multipart message that contains lines that are too long for RFC compliance.
  1216. */
  1217. public function testLongBody()
  1218. {
  1219. $oklen = str_repeat(str_repeat('0', PHPMailer::MAX_LINE_LENGTH) . PHPMailer::CRLF, 2);
  1220. $badlen = str_repeat(str_repeat('1', PHPMailer::MAX_LINE_LENGTH + 1) . PHPMailer::CRLF, 2);
  1221. $this->Mail->Body = "This message contains lines that are too long.".
  1222. PHPMailer::CRLF . $oklen . $badlen . $oklen;
  1223. $this->assertTrue(
  1224. PHPMailer::hasLineLongerThanMax($this->Mail->Body),
  1225. 'Test content does not contain long lines!'
  1226. );
  1227. $this->Mail->isHTML();
  1228. $this->buildBody();
  1229. $this->Mail->AltBody = $this->Mail->Body;
  1230. $this->Mail->Encoding = '8bit';
  1231. $this->Mail->preSend();
  1232. $message = $this->Mail->getSentMIMEMessage();
  1233. $this->assertFalse(PHPMailer::hasLineLongerThanMax($message), 'Long line not corrected.');
  1234. $this->assertContains(
  1235. 'Content-Transfer-Encoding: quoted-printable',
  1236. $message,
  1237. 'Long line did not cause transfer encoding switch.'
  1238. );
  1239. }
  1240. /**
  1241. * Test constructing a message that does NOT contain lines that are too long for RFC compliance.
  1242. */
  1243. public function testShortBody()
  1244. {
  1245. $oklen = str_repeat(str_repeat('0', PHPMailer::MAX_LINE_LENGTH) . PHPMailer::CRLF, 10);
  1246. $this->Mail->Body = "This message does not contain lines that are too long.".
  1247. PHPMailer::CRLF . $oklen;
  1248. $this->assertFalse(
  1249. PHPMailer::hasLineLongerThanMax($this->Mail->Body),
  1250. 'Test content contains long lines!'
  1251. );
  1252. $this->buildBody();
  1253. $this->Mail->Encoding = '8bit';
  1254. $this->Mail->preSend();
  1255. $message = $this->Mail->getSentMIMEMessage();
  1256. $this->assertFalse(PHPMailer::hasLineLongerThanMax($message), 'Long line not corrected.');
  1257. $this->assertNotContains(
  1258. 'Content-Transfer-Encoding: quoted-printable',
  1259. $message,
  1260. 'Short line caused transfer encoding switch.'
  1261. );
  1262. }
  1263. /**
  1264. * Test keepalive (sending multiple messages in a single connection).
  1265. */
  1266. public function testSmtpKeepAlive()
  1267. {
  1268. $this->Mail->Body = 'SMTP keep-alive test.';
  1269. $this->buildBody();
  1270. $subject = $this->Mail->Subject;
  1271. $this->Mail->SMTPKeepAlive = true;
  1272. $this->Mail->Subject = $subject . ': SMTP keep-alive 1';
  1273. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  1274. $this->Mail->Subject = $subject . ': SMTP keep-alive 2';
  1275. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  1276. $this->Mail->smtpClose();
  1277. }
  1278. /**
  1279. * Tests this denial of service attack:
  1280. * @link http://www.cybsec.com/vuln/PHPMailer-DOS.pdf
  1281. */
  1282. public function testDenialOfServiceAttack()
  1283. {
  1284. $this->Mail->Body = 'This should no longer cause a denial of service.';
  1285. $this->buildBody();
  1286. $this->Mail->Subject = substr(str_repeat('0123456789', 100), 0, 998);
  1287. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  1288. }
  1289. /**
  1290. * Tests this denial of service attack:
  1291. * @link https://sourceforge.net/p/phpmailer/bugs/383/
  1292. * According to the ticket, this should get stuck in a loop, though I can't make it happen.
  1293. * @TODO No assertions in here - how can you assert for this?
  1294. */
  1295. public function testDenialOfServiceAttack2()
  1296. {
  1297. //Encoding name longer than 68 chars
  1298. $this->Mail->Encoding = '1234567890123456789012345678901234567890123456789012345678901234567890';
  1299. //Call wrapText with a zero length value
  1300. $this->Mail->wrapText(str_repeat('This should no longer cause a denial of service. ', 30), 0);
  1301. }
  1302. /**
  1303. * Test error handling.
  1304. */
  1305. public function testError()
  1306. {
  1307. $this->Mail->Subject .= ': Error handling test - this should be sent ok';
  1308. $this->buildBody();
  1309. $this->Mail->clearAllRecipients(); // no addresses should cause an error
  1310. $this->assertTrue($this->Mail->isError() == false, 'Error found');
  1311. $this->assertTrue($this->Mail->send() == false, 'send succeeded');
  1312. $this->assertTrue($this->Mail->isError(), 'No error found');
  1313. $this->assertEquals('You must provide at least one recipient email address.', $this->Mail->ErrorInfo);
  1314. $this->Mail->addAddress($_REQUEST['mail_to']);
  1315. $this->assertTrue($this->Mail->send(), 'send failed');
  1316. }
  1317. /**
  1318. * Test addressing.
  1319. */
  1320. public function testAddressing()
  1321. {
  1322. $this->assertFalse($this->Mail->addAddress(''), 'Empty address accepted');
  1323. $this->assertFalse($this->Mail->addAddress('', 'Nobody'), 'Empty address with name accepted');
  1324. $this->assertFalse($this->Mail->addAddress('a@example..com'), 'Invalid address accepted');
  1325. $this->assertTrue($this->Mail->addAddress('a@example.com'), 'Addressing failed');
  1326. $this->assertFalse($this->Mail->addAddress('a@example.com'), 'Duplicate addressing failed');
  1327. $this->assertTrue($this->Mail->addCC('b@example.com'), 'CC addressing failed');
  1328. $this->assertFalse($this->Mail->addCC('b@example.com'), 'CC duplicate addressing failed');
  1329. $this->assertFalse($this->Mail->addCC('a@example.com'), 'CC duplicate addressing failed (2)');
  1330. $this->assertTrue($this->Mail->addBCC('c@example.com'), 'BCC addressing failed');
  1331. $this->assertFalse($this->Mail->addBCC('c@example.com'), 'BCC duplicate addressing failed');
  1332. $this->assertFalse($this->Mail->addBCC('a@example.com'), 'BCC duplicate addressing failed (2)');
  1333. $this->assertTrue($this->Mail->addReplyTo('a@example.com'), 'Replyto Addressing failed');
  1334. $this->assertFalse($this->Mail->addReplyTo('a@example..com'), 'Invalid Replyto address accepted');
  1335. $this->assertTrue($this->Mail->setFrom('a@example.com', 'some name'), 'setFrom failed');
  1336. $this->assertFalse($this->Mail->setFrom('a@example.com.', 'some name'), 'setFrom accepted invalid address');
  1337. $this->Mail->Sender = '';
  1338. $this->Mail->setFrom('a@example.com', 'some name', true);
  1339. $this->assertEquals($this->Mail->Sender, 'a@example.com', 'setFrom failed to set sender');
  1340. $this->Mail->Sender = '';
  1341. $this->Mail->setFrom('a@example.com', 'some name', false);
  1342. $this->assertEquals($this->Mail->Sender, '', 'setFrom should not have set sender');
  1343. $this->Mail->clearCCs();
  1344. $this->Mail->clearBCCs();
  1345. $this->Mail->clearReplyTos();
  1346. }
  1347. /**
  1348. * Test RFC822 address splitting.
  1349. */
  1350. public function testAddressSplitting()
  1351. {
  1352. //Test built-in address parser
  1353. $this->assertCount(
  1354. 2,
  1355. $this->Mail->parseAddresses(
  1356. 'Joe User <joe@example.com>, Jill User <jill@example.net>'
  1357. ),
  1358. 'Failed to recognise address list (IMAP parser)'
  1359. );
  1360. $this->assertEquals(
  1361. array(
  1362. array("name" => 'Joe User', 'address' => 'joe@example.com'),
  1363. array("name" => 'Jill User', 'address' => 'jill@example.net'),
  1364. array("name" => '', 'address' => 'frank@example.com'),
  1365. ),
  1366. $this->Mail->parseAddresses(
  1367. 'Joe User <joe@example.com>,'
  1368. . 'Jill User <jill@example.net>,'
  1369. . 'frank@example.com,'
  1370. ),
  1371. 'Parsed addresses'
  1372. );
  1373. //Test simple address parser
  1374. $this->assertCount(
  1375. 2,
  1376. $this->Mail->parseAddresses(
  1377. 'Joe User <joe@example.com>, Jill User <jill@example.net>',
  1378. false
  1379. ),
  1380. 'Failed to recognise address list'
  1381. );
  1382. //Test single address
  1383. $this->assertNotEmpty(
  1384. $this->Mail->parseAddresses(
  1385. 'Joe User <joe@example.com>',
  1386. false
  1387. ),
  1388. 'Failed to recognise single address'
  1389. );
  1390. //Test quoted name IMAP
  1391. $this->assertNotEmpty(
  1392. $this->Mail->parseAddresses(
  1393. 'Tim "The Book" O\'Reilly <foo@example.com>'
  1394. ),
  1395. 'Failed to recognise quoted name (IMAP)'
  1396. );
  1397. //Test quoted name
  1398. $this->assertNotEmpty(
  1399. $this->Mail->parseAddresses(
  1400. 'Tim "The Book" O\'Reilly <foo@example.com>',
  1401. false
  1402. ),
  1403. 'Failed to recognise quoted name'
  1404. );
  1405. //Test single address IMAP
  1406. $this->assertNotEmpty(
  1407. $this->Mail->parseAddresses(
  1408. 'Joe User <joe@example.com>'
  1409. ),
  1410. 'Failed to recognise single address (IMAP)'
  1411. );
  1412. //Test unnamed address
  1413. $this->assertNotEmpty(
  1414. $this->Mail->parseAddresses(
  1415. 'joe@example.com',
  1416. false
  1417. ),
  1418. 'Failed to recognise unnamed address'
  1419. );
  1420. //Test unnamed address IMAP
  1421. $this->assertNotEmpty(
  1422. $this->Mail->parseAddresses(
  1423. 'joe@example.com'
  1424. ),
  1425. 'Failed to recognise unnamed address (IMAP)'
  1426. );
  1427. //Test invalid addresses
  1428. $this->assertEmpty(
  1429. $this->Mail->parseAddresses(
  1430. 'Joe User <joe@example.com.>, Jill User <jill.@example.net>'
  1431. ),
  1432. 'Failed to recognise invalid addresses (IMAP)'
  1433. );
  1434. //Test invalid addresses
  1435. $this->assertEmpty(
  1436. $this->Mail->parseAddresses(
  1437. 'Joe User <joe@example.com.>, Jill User <jill.@example.net>',
  1438. false
  1439. ),
  1440. 'Failed to recognise invalid addresses'
  1441. );
  1442. }
  1443. /**
  1444. * Test address escaping.
  1445. */
  1446. public function testAddressEscaping()
  1447. {
  1448. $this->Mail->Subject .= ': Address escaping';
  1449. $this->Mail->clearAddresses();
  1450. $this->Mail->addAddress('foo@example.com', 'Tim "The Book" O\'Reilly');
  1451. $this->Mail->Body = 'Test correct escaping of quotes in addresses.';
  1452. $this->buildBody();
  1453. $this->Mail->preSend();
  1454. $b = $this->Mail->getSentMIMEMessage();
  1455. $this->assertTrue((strpos($b, 'To: "Tim \"The Book\" O\'Reilly" <foo@example.com>') !== false));
  1456. }
  1457. /**
  1458. * Test MIME structure assembly.
  1459. */
  1460. public function testMIMEStructure()
  1461. {
  1462. $this->Mail->Subject .= ': MIME structure';
  1463. $this->Mail->Body = '<h3>MIME structure test.</h3>';
  1464. $this->Mail->AltBody = 'MIME structure test.';
  1465. $this->buildBody();
  1466. $this->Mail->preSend();
  1467. $this->assertRegExp(
  1468. "/Content-Transfer-Encoding: 8bit\r\n\r\n".
  1469. "This is a multi-part message in MIME format./",
  1470. $this->Mail->getSentMIMEMessage(),
  1471. 'MIME structure broken'
  1472. );
  1473. }
  1474. /**
  1475. * Test BCC-only addressing.
  1476. */
  1477. public function testBCCAddressing()
  1478. {
  1479. $this->Mail->Subject .= ': BCC-only addressing';
  1480. $this->buildBody();
  1481. $this->Mail->clearAllRecipients();
  1482. $this->assertTrue($this->Mail->addBCC('a@example.com'), 'BCC addressing failed');
  1483. $this->assertTrue($this->Mail->send(), 'send failed');
  1484. }
  1485. /**
  1486. * Encoding and charset tests.
  1487. */
  1488. public function testEncodings()
  1489. {
  1490. $this->Mail->CharSet = 'iso-8859-1';
  1491. $this->assertEquals(
  1492. '=A1Hola!_Se=F1or!',
  1493. $this->Mail->encodeQ("\xa1Hola! Se\xf1or!", 'text'),
  1494. 'Q Encoding (text) failed'
  1495. );
  1496. $this->assertEquals(
  1497. '=A1Hola!_Se=F1or!',
  1498. $this->Mail->encodeQ("\xa1Hola! Se\xf1or!", 'comment'),
  1499. 'Q Encoding (comment) failed'
  1500. );
  1501. $this->assertEquals(
  1502. '=A1Hola!_Se=F1or!',
  1503. $this->Mail->encodeQ("\xa1Hola! Se\xf1or!", 'phrase'),
  1504. 'Q Encoding (phrase) failed'
  1505. );
  1506. $this->Mail->CharSet = 'UTF-8';
  1507. $this->assertEquals(
  1508. '=C2=A1Hola!_Se=C3=B1or!',
  1509. $this->Mail->encodeQ("\xc2\xa1Hola! Se\xc3\xb1or!", 'text'),
  1510. 'Q Encoding (text) failed'
  1511. );
  1512. //Strings containing '=' are a special case
  1513. $this->assertEquals(
  1514. 'Nov=C3=A1=3D',
  1515. $this->Mail->encodeQ("Nov\xc3\xa1=", 'text'),
  1516. 'Q Encoding (text) failed 2'
  1517. );
  1518. }
  1519. /**
  1520. * Test base-64 encoding.
  1521. */
  1522. public function testBase64()
  1523. {
  1524. $this->Mail->Subject .= ': Base-64 encoding';
  1525. $this->Mail->Encoding = 'base64';
  1526. $this->buildBody();
  1527. $this->assertTrue($this->Mail->send(), 'Base64 encoding failed');
  1528. }
  1529. /**
  1530. * S/MIME Signing tests (self-signed).
  1531. * @requires extension openssl
  1532. */
  1533. public function testSigning()
  1534. {
  1535. $this->Mail->Subject .= ': S/MIME signing';
  1536. $this->Mail->Body = 'This message is S/MIME signed.';
  1537. $this->buildBody();
  1538. $dn = array(
  1539. 'countryName' => 'UK',
  1540. 'stateOrProvinceName' => 'Here',
  1541. 'localityName' => 'There',
  1542. 'organizationName' => 'PHP',
  1543. 'organizationalUnitName' => 'PHPMailer',
  1544. 'commonName' => 'PHPMailer Test',
  1545. 'emailAddress' => 'phpmailer@example.com'
  1546. );
  1547. $keyconfig = array(
  1548. "digest_alg" => "sha256",
  1549. "private_key_bits" => 2048,
  1550. "private_key_type" => OPENSSL_KEYTYPE_RSA,
  1551. );
  1552. $password = 'password';
  1553. $certfile = 'certfile.pem';
  1554. $keyfile = 'keyfile.pem';
  1555. //Make a new key pair
  1556. $pk = openssl_pkey_new($keyconfig);
  1557. //Create a certificate signing request
  1558. $csr = openssl_csr_new($dn, $pk);
  1559. //Create a self-signed cert
  1560. $cert = openssl_csr_sign($csr, null, $pk, 1);
  1561. //Save the cert
  1562. openssl_x509_export($cert, $certout);
  1563. file_put_contents($certfile, $certout);
  1564. //Save the key
  1565. openssl_pkey_export($pk, $pkeyout, $password);
  1566. file_put_contents($keyfile, $pkeyout);
  1567. $this->Mail->sign(
  1568. $certfile,
  1569. $keyfile,
  1570. $password
  1571. );
  1572. $this->assertTrue($this->Mail->send(), 'S/MIME signing failed');
  1573. $msg = $this->Mail->getSentMIMEMessage();
  1574. $this->assertNotContains("\r\n\r\nMIME-Version:", $msg, 'Incorrect MIME headers');
  1575. unlink($certfile);
  1576. unlink($keyfile);
  1577. }
  1578. /**
  1579. * S/MIME Signing tests using a CA chain cert.
  1580. * To test that a generated message is signed correctly, save the message in a file
  1581. * and use openssl along with the certs generated by this script:
  1582. * `openssl smime -verify -in signed.eml -signer certfile.pem -CAfile cacertfile.pem`
  1583. * @requires extension openssl
  1584. */
  1585. public function testSigningWithCA()
  1586. {
  1587. $this->Mail->Subject .= ': S/MIME signing with CA';
  1588. $this->Mail->Body = 'This message is S/MIME signed with an extra CA cert.';
  1589. $this->buildBody();
  1590. $certprops = array(
  1591. 'countryName' => 'UK',
  1592. 'stateOrProvinceName' => 'Here',
  1593. 'localityName' => 'There',
  1594. 'organizationName' => 'PHP',
  1595. 'organizationalUnitName' => 'PHPMailer',
  1596. 'commonName' => 'PHPMailer Test',
  1597. 'emailAddress' => 'phpmailer@example.com'
  1598. );
  1599. $cacertprops = array(
  1600. 'countryName' => 'UK',
  1601. 'stateOrProvinceName' => 'Here',
  1602. 'localityName' => 'There',
  1603. 'organizationName' => 'PHP',
  1604. 'organizationalUnitName' => 'PHPMailer CA',
  1605. 'commonName' => 'PHPMailer Test CA',
  1606. 'emailAddress' => 'phpmailer@example.com'
  1607. );
  1608. $keyconfig = array(
  1609. "digest_alg" => "sha256",
  1610. "private_key_bits" => 2048,
  1611. "private_key_type" => OPENSSL_KEYTYPE_RSA,
  1612. );
  1613. $password = 'password';
  1614. $cacertfile = 'cacertfile.pem';
  1615. $cakeyfile = 'cakeyfile.pem';
  1616. $certfile = 'certfile.pem';
  1617. $keyfile = 'keyfile.pem';
  1618. //Create a CA cert
  1619. //Make a new key pair
  1620. $capk = openssl_pkey_new($keyconfig);
  1621. //Create a certificate signing request
  1622. $csr = openssl_csr_new($cacertprops, $capk);
  1623. //Create a self-signed cert
  1624. $cert = openssl_csr_sign($csr, null, $capk, 1);
  1625. //Save the CA cert
  1626. openssl_x509_export($cert, $certout);
  1627. file_put_contents($cacertfile, $certout);
  1628. //Save the CA key
  1629. openssl_pkey_export($capk, $pkeyout, $password);
  1630. file_put_contents($cakeyfile, $pkeyout);
  1631. //Create a cert signed by our CA
  1632. //Make a new key pair
  1633. $pk = openssl_pkey_new($keyconfig);
  1634. //Create a certificate signing request
  1635. $csr = openssl_csr_new($certprops, $pk);
  1636. //Create a self-signed cert
  1637. $cert = openssl_csr_sign($csr, 'file://' . $cacertfile, $capk, 1);
  1638. //Save the cert
  1639. openssl_x509_export($cert, $certout);
  1640. file_put_contents($certfile, $certout);
  1641. //Save the key
  1642. openssl_pkey_export($pk, $pkeyout, $password);
  1643. file_put_contents($keyfile, $pkeyout);
  1644. $this->Mail->sign(
  1645. $certfile,
  1646. $keyfile,
  1647. $password,
  1648. $cacertfile
  1649. );
  1650. $this->assertTrue($this->Mail->send(), 'S/MIME signing with CA failed');
  1651. unlink($cacertfile);
  1652. unlink($cakeyfile);
  1653. unlink($certfile);
  1654. unlink($keyfile);
  1655. }
  1656. /**
  1657. * DKIM Signing tests.
  1658. * @requires extension openssl
  1659. */
  1660. public function testDKIM()
  1661. {
  1662. $this->Mail->Subject .= ': DKIM signing';
  1663. $this->Mail->Body = 'This message is DKIM signed.';
  1664. $this->buildBody();
  1665. $privatekeyfile = 'dkim_private.key';
  1666. //Make a new key pair
  1667. //(2048 bits is the recommended minimum key length -
  1668. //gmail won't accept less than 1024 bits)
  1669. $pk = openssl_pkey_new(
  1670. array(
  1671. 'private_key_bits' => 2048,
  1672. 'private_key_type' => OPENSSL_KEYTYPE_RSA
  1673. )
  1674. );
  1675. openssl_pkey_export_to_file($pk, $privatekeyfile);
  1676. $this->Mail->DKIM_domain = 'example.com';
  1677. $this->Mail->DKIM_private = $privatekeyfile;
  1678. $this->Mail->DKIM_selector = 'phpmailer';
  1679. $this->Mail->DKIM_passphrase = ''; //key is not encrypted
  1680. $this->assertTrue($this->Mail->send(), 'DKIM signed mail failed');
  1681. unlink($privatekeyfile);
  1682. }
  1683. /**
  1684. * Test line break reformatting.
  1685. */
  1686. public function testLineBreaks()
  1687. {
  1688. $unixsrc = "hello\nWorld\nAgain\n";
  1689. $macsrc = "hello\rWorld\rAgain\r";
  1690. $windowssrc = "hello\r\nWorld\r\nAgain\r\n";
  1691. $mixedsrc = "hello\nWorld\rAgain\r\n";
  1692. $target = "hello\r\nWorld\r\nAgain\r\n";
  1693. $this->assertEquals($target, PHPMailer::normalizeBreaks($unixsrc), 'UNIX break reformatting failed');
  1694. $this->assertEquals($target, PHPMailer::normalizeBreaks($macsrc), 'Mac break reformatting failed');
  1695. $this->assertEquals($target, PHPMailer::normalizeBreaks($windowssrc), 'Windows break reformatting failed');
  1696. $this->assertEquals($target, PHPMailer::normalizeBreaks($mixedsrc), 'Mixed break reformatting failed');
  1697. }
  1698. /**
  1699. * Test line length detection
  1700. */
  1701. public function testLineLength()
  1702. {
  1703. $oklen = str_repeat(str_repeat('0', PHPMailer::MAX_LINE_LENGTH)."\r\n", 2);
  1704. $badlen = str_repeat(str_repeat('1', PHPMailer::MAX_LINE_LENGTH + 1) . "\r\n", 2);
  1705. $this->assertTrue(PHPMailer::hasLineLongerThanMax($badlen), 'Long line not detected (only)');
  1706. $this->assertTrue(PHPMailer::hasLineLongerThanMax($oklen . $badlen), 'Long line not detected (first)');
  1707. $this->assertTrue(PHPMailer::hasLineLongerThanMax($badlen . $oklen), 'Long line not detected (last)');
  1708. $this->assertTrue(
  1709. PHPMailer::hasLineLongerThanMax($oklen . $badlen . $oklen),
  1710. 'Long line not detected (middle)'
  1711. );
  1712. $this->assertFalse(PHPMailer::hasLineLongerThanMax($oklen), 'Long line false positive');
  1713. $this->Mail->isHTML(false);
  1714. $this->Mail->Subject .= ": Line length test";
  1715. $this->Mail->CharSet = 'UTF-8';
  1716. $this->Mail->Encoding = '8bit';
  1717. $this->Mail->Body = $oklen . $badlen . $oklen . $badlen;
  1718. $this->buildBody();
  1719. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  1720. $this->assertEquals('quoted-printable', $this->Mail->Encoding, 'Long line did not override transfer encoding');
  1721. }
  1722. /**
  1723. * Test setting and retrieving message ID.
  1724. */
  1725. public function testMessageID()
  1726. {
  1727. $this->Mail->Body = 'Test message ID.';
  1728. $id = md5(12345);
  1729. $this->Mail->MessageID = $id;
  1730. $this->buildBody();
  1731. $this->Mail->preSend();
  1732. $lastid = $this->Mail->getLastMessageID();
  1733. $this->assertNotEquals($lastid, $id, 'Invalid Message ID allowed');
  1734. $id = '<'.md5(12345).'@example.com>';
  1735. $this->Mail->MessageID = $id;
  1736. $this->buildBody();
  1737. $this->Mail->preSend();
  1738. $lastid = $this->Mail->getLastMessageID();
  1739. $this->assertEquals($lastid, $id, 'Custom Message ID not used');
  1740. $this->Mail->MessageID = '';
  1741. $this->buildBody();
  1742. $this->Mail->preSend();
  1743. $lastid = $this->Mail->getLastMessageID();
  1744. $this->assertRegExp('/^<.*@.*>$/', $lastid, 'Invalid default Message ID');
  1745. }
  1746. /**
  1747. * Miscellaneous calls to improve test coverage and some small tests.
  1748. */
  1749. public function testMiscellaneous()
  1750. {
  1751. $this->assertEquals('application/pdf', PHPMailer::_mime_types('pdf'), 'MIME TYPE lookup failed');
  1752. $this->Mail->addCustomHeader('SomeHeader: Some Value');
  1753. $this->Mail->clearCustomHeaders();
  1754. $this->Mail->clearAttachments();
  1755. $this->Mail->isHTML(false);
  1756. $this->Mail->isSMTP();
  1757. $this->Mail->isMail();
  1758. $this->Mail->isSendmail();
  1759. $this->Mail->isQmail();
  1760. $this->Mail->setLanguage('fr');
  1761. $this->Mail->Sender = '';
  1762. $this->Mail->createHeader();
  1763. $this->assertFalse($this->Mail->set('x', 'y'), 'Invalid property set succeeded');
  1764. $this->assertTrue($this->Mail->set('Timeout', 11), 'Valid property set failed');
  1765. $this->assertTrue($this->Mail->set('AllowEmpty', null), 'Null property set failed');
  1766. $this->assertTrue($this->Mail->set('AllowEmpty', false), 'Valid property set of null property failed');
  1767. //Test pathinfo
  1768. $a = '/mnt/files/飛兒樂 團光茫.mp3';
  1769. $q = PHPMailer::mb_pathinfo($a);
  1770. $this->assertEquals($q['dirname'], '/mnt/files', 'UNIX dirname not matched');
  1771. $this->assertEquals($q['basename'], '飛兒樂 團光茫.mp3', 'UNIX basename not matched');
  1772. $this->assertEquals($q['extension'], 'mp3', 'UNIX extension not matched');
  1773. $this->assertEquals($q['filename'], '飛兒樂 團光茫', 'UNIX filename not matched');
  1774. $this->assertEquals(
  1775. PHPMailer::mb_pathinfo($a, PATHINFO_DIRNAME),
  1776. '/mnt/files',
  1777. 'Dirname path element not matched'
  1778. );
  1779. $this->assertEquals(PHPMailer::mb_pathinfo($a, 'filename'), '飛兒樂 團光茫', 'Filename path element not matched');
  1780. $a = 'c:\mnt\files\飛兒樂 團光茫.mp3';
  1781. $q = PHPMailer::mb_pathinfo($a);
  1782. $this->assertEquals($q['dirname'], 'c:\mnt\files', 'Windows dirname not matched');
  1783. $this->assertEquals($q['basename'], '飛兒樂 團光茫.mp3', 'Windows basename not matched');
  1784. $this->assertEquals($q['extension'], 'mp3', 'Windows extension not matched');
  1785. $this->assertEquals($q['filename'], '飛兒樂 團光茫', 'Windows filename not matched');
  1786. }
  1787. public function testBadSMTP()
  1788. {
  1789. $this->Mail->smtpConnect();
  1790. $smtp = $this->Mail->getSMTPInstance();
  1791. $this->assertFalse($smtp->mail("somewhere\nbad"), 'Bad SMTP command containing breaks accepted');
  1792. }
  1793. /**
  1794. * Tests the Custom header getter
  1795. */
  1796. public function testCustomHeaderGetter()
  1797. {
  1798. $this->Mail->addCustomHeader('foo', 'bar');
  1799. $this->assertEquals(array(array('foo', 'bar')), $this->Mail->getCustomHeaders());
  1800. $this->Mail->addCustomHeader('foo', 'baz');
  1801. $this->assertEquals(array(
  1802. array('foo', 'bar'),
  1803. array('foo', 'baz')
  1804. ), $this->Mail->getCustomHeaders());
  1805. $this->Mail->clearCustomHeaders();
  1806. $this->assertEmpty($this->Mail->getCustomHeaders());
  1807. $this->Mail->addCustomHeader('yux');
  1808. $this->assertEquals(array(array('yux')), $this->Mail->getCustomHeaders());
  1809. $this->Mail->addCustomHeader('Content-Type: application/json');
  1810. $this->assertEquals(array(
  1811. array('yux'),
  1812. array('Content-Type', ' application/json')
  1813. ), $this->Mail->getCustomHeaders());
  1814. }
  1815. /**
  1816. * Tests setting and retrieving ConfirmReadingTo address, also known as "read receipt" address.
  1817. */
  1818. public function testConfirmReadingTo()
  1819. {
  1820. $this->Mail->CharSet = 'utf-8';
  1821. $this->buildBody();
  1822. $this->Mail->ConfirmReadingTo = 'test@example..com'; //Invalid address
  1823. $this->assertFalse($this->Mail->send(), $this->Mail->ErrorInfo);
  1824. $this->Mail->ConfirmReadingTo = ' test@example.com'; //Extra space to trim
  1825. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  1826. $this->assertEquals(
  1827. 'test@example.com',
  1828. $this->Mail->ConfirmReadingTo,
  1829. 'Unexpected read receipt address');
  1830. $this->Mail->ConfirmReadingTo = 'test@françois.ch'; //Address with IDN
  1831. if ($this->Mail->idnSupported()) {
  1832. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  1833. $this->assertEquals(
  1834. 'test@xn--franois-xxa.ch',
  1835. $this->Mail->ConfirmReadingTo,
  1836. 'IDN address not converted to punycode');
  1837. } else {
  1838. $this->assertFalse($this->Mail->send(), $this->Mail->ErrorInfo);
  1839. }
  1840. }
  1841. /**
  1842. * Tests CharSet and Unicode -> ASCII conversions for addresses with IDN.
  1843. */
  1844. public function testConvertEncoding()
  1845. {
  1846. if (!$this->Mail->idnSupported()) {
  1847. $this->markTestSkipped('intl and/or mbstring extensions are not available');
  1848. }
  1849. $this->Mail->clearAllRecipients();
  1850. $this->Mail->clearReplyTos();
  1851. // This file is UTF-8 encoded. Create a domain encoded in "iso-8859-1".
  1852. $domain = '@' . mb_convert_encoding('françois.ch', 'ISO-8859-1', 'UTF-8');
  1853. $this->Mail->addAddress('test' . $domain);
  1854. $this->Mail->addCC('test+cc' . $domain);
  1855. $this->Mail->addBCC('test+bcc' . $domain);
  1856. $this->Mail->addReplyTo('test+replyto' . $domain);
  1857. // Queued addresses are not returned by get*Addresses() before send() call.
  1858. $this->assertEmpty($this->Mail->getToAddresses(), 'Bad "to" recipients');
  1859. $this->assertEmpty($this->Mail->getCcAddresses(), 'Bad "cc" recipients');
  1860. $this->assertEmpty($this->Mail->getBccAddresses(), 'Bad "bcc" recipients');
  1861. $this->assertEmpty($this->Mail->getReplyToAddresses(), 'Bad "reply-to" recipients');
  1862. // Clear queued BCC recipient.
  1863. $this->Mail->clearBCCs();
  1864. $this->buildBody();
  1865. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  1866. // Addresses with IDN are returned by get*Addresses() after send() call.
  1867. $domain = $this->Mail->punyencodeAddress($domain);
  1868. $this->assertEquals(
  1869. array(array('test' . $domain, '')),
  1870. $this->Mail->getToAddresses(),
  1871. 'Bad "to" recipients');
  1872. $this->assertEquals(
  1873. array(array('test+cc' . $domain, '')),
  1874. $this->Mail->getCcAddresses(),
  1875. 'Bad "cc" recipients');
  1876. $this->assertEmpty($this->Mail->getBccAddresses(), 'Bad "bcc" recipients');
  1877. $this->assertEquals(
  1878. array('test+replyto' . $domain => array('test+replyto' . $domain, '')),
  1879. $this->Mail->getReplyToAddresses(),
  1880. 'Bad "reply-to" addresses');
  1881. }
  1882. /**
  1883. * Tests removal of duplicate recipients and reply-tos.
  1884. */
  1885. public function testDuplicateIDNRemoved()
  1886. {
  1887. if (!$this->Mail->idnSupported()) {
  1888. $this->markTestSkipped('intl and/or mbstring extensions are not available');
  1889. }
  1890. $this->Mail->clearAllRecipients();
  1891. $this->Mail->clearReplyTos();
  1892. $this->Mail->CharSet = 'utf-8';
  1893. $this->assertTrue($this->Mail->addAddress('test@françois.ch'));
  1894. $this->assertFalse($this->Mail->addAddress('test@françois.ch'));
  1895. $this->assertTrue($this->Mail->addAddress('test@FRANÇOIS.CH'));
  1896. $this->assertFalse($this->Mail->addAddress('test@FRANÇOIS.CH'));
  1897. $this->assertTrue($this->Mail->addAddress('test@xn--franois-xxa.ch'));
  1898. $this->assertFalse($this->Mail->addAddress('test@xn--franois-xxa.ch'));
  1899. $this->assertFalse($this->Mail->addAddress('test@XN--FRANOIS-XXA.CH'));
  1900. $this->assertTrue($this->Mail->addReplyTo('test+replyto@françois.ch'));
  1901. $this->assertFalse($this->Mail->addReplyTo('test+replyto@françois.ch'));
  1902. $this->assertTrue($this->Mail->addReplyTo('test+replyto@FRANÇOIS.CH'));
  1903. $this->assertFalse($this->Mail->addReplyTo('test+replyto@FRANÇOIS.CH'));
  1904. $this->assertTrue($this->Mail->addReplyTo('test+replyto@xn--franois-xxa.ch'));
  1905. $this->assertFalse($this->Mail->addReplyTo('test+replyto@xn--franois-xxa.ch'));
  1906. $this->assertFalse($this->Mail->addReplyTo('test+replyto@XN--FRANOIS-XXA.CH'));
  1907. $this->buildBody();
  1908. $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
  1909. // There should be only one "To" address and one "Reply-To" address.
  1910. $this->assertEquals(
  1911. 1,
  1912. count($this->Mail->getToAddresses()),
  1913. 'Bad count of "to" recipients');
  1914. $this->assertEquals(
  1915. 1,
  1916. count($this->Mail->getReplyToAddresses()),
  1917. 'Bad count of "reply-to" addresses');
  1918. }
  1919. /**
  1920. * Use a fake POP3 server to test POP-before-SMTP auth.
  1921. * With a known-good login
  1922. */
  1923. public function testPopBeforeSmtpGood()
  1924. {
  1925. //Start a fake POP server
  1926. $pid = shell_exec('nohup ./runfakepopserver.sh >/dev/null 2>/dev/null & printf "%u" $!');
  1927. $this->pids[] = $pid;
  1928. sleep(2);
  1929. //Test a known-good login
  1930. $this->assertTrue(
  1931. POP3::popBeforeSmtp('localhost', 1100, 10, 'user', 'test', $this->Mail->SMTPDebug),
  1932. 'POP before SMTP failed'
  1933. );
  1934. //Kill the fake server
  1935. shell_exec('kill -TERM ' . escapeshellarg($pid));
  1936. sleep(2);
  1937. }
  1938. /**
  1939. * Use a fake POP3 server to test POP-before-SMTP auth
  1940. * with a known-bad login.
  1941. */
  1942. public function testPopBeforeSmtpBad()
  1943. {
  1944. //Start a fake POP server on a different port
  1945. //so we don't inadvertently connect to the previous instance
  1946. $pid = shell_exec('nohup ./runfakepopserver.sh 1101 >/dev/null 2>/dev/null & printf "%u" $!');
  1947. $this->pids[] = $pid;
  1948. sleep(2);
  1949. //Test a known-bad login
  1950. $this->assertFalse(
  1951. POP3::popBeforeSmtp('localhost', 1101, 10, 'user', 'xxx', $this->Mail->SMTPDebug),
  1952. 'POP before SMTP should have failed'
  1953. );
  1954. shell_exec('kill -TERM ' . escapeshellarg($pid));
  1955. sleep(2);
  1956. }
  1957. /**
  1958. * Test SMTP host connections.
  1959. * This test can take a long time, so run it last
  1960. */
  1961. public function testSmtpConnect()
  1962. {
  1963. $this->Mail->SMTPDebug = 4; //Show connection-level errors
  1964. $this->assertTrue($this->Mail->smtpConnect(), 'SMTP single connect failed');
  1965. $this->Mail->smtpClose();
  1966. $this->Mail->Host = "ssl://localhost:12345;tls://localhost:587;10.10.10.10:54321;localhost:12345;10.10.10.10";
  1967. $this->assertFalse($this->Mail->smtpConnect(), 'SMTP bad multi-connect succeeded');
  1968. $this->Mail->smtpClose();
  1969. $this->Mail->Host = "localhost:12345;10.10.10.10:54321;" . $_REQUEST['mail_host'];
  1970. $this->assertTrue($this->Mail->smtpConnect(), 'SMTP multi-connect failed');
  1971. $this->Mail->smtpClose();
  1972. $this->Mail->Host = " localhost:12345 ; " . $_REQUEST['mail_host'] . ' ';
  1973. $this->assertTrue($this->Mail->smtpConnect(), 'SMTP hosts with stray spaces failed');
  1974. $this->Mail->smtpClose();
  1975. $this->Mail->Host = $_REQUEST['mail_host'];
  1976. //Need to pick a harmless option so as not cause problems of its own! socket:bind doesn't work with Travis-CI
  1977. $this->assertTrue(
  1978. $this->Mail->smtpConnect(array('ssl' => array('verify_depth' => 10))),
  1979. 'SMTP connect with options failed'
  1980. );
  1981. }
  1982. }
  1983. /**
  1984. * This is a sample form for setting appropriate test values through a browser
  1985. * These values can also be set using a file called testbootstrap.php (not in repo) in the same folder as this script
  1986. * which is probably more useful if you run these tests a lot
  1987. * <html>
  1988. * <body>
  1989. * <h3>phpmailer Unit Test</h3>
  1990. * By entering a SMTP hostname it will automatically perform tests with SMTP.
  1991. *
  1992. * <form name="phpmailer_unit" action=__FILE__ method="get">
  1993. * <input type="hidden" name="submitted" value="1"/>
  1994. * From Address: <input type="text" size="50" name="mail_from" value="<?php echo get("mail_from"); ?>"/>
  1995. * <br/>
  1996. * To Address: <input type="text" size="50" name="mail_to" value="<?php echo get("mail_to"); ?>"/>
  1997. * <br/>
  1998. * Cc Address: <input type="text" size="50" name="mail_cc" value="<?php echo get("mail_cc"); ?>"/>
  1999. * <br/>
  2000. * SMTP Hostname: <input type="text" size="50" name="mail_host" value="<?php echo get("mail_host"); ?>"/>
  2001. * <p/>
  2002. * <input type="submit" value="Run Test"/>
  2003. *
  2004. * </form>
  2005. * </body>
  2006. * </html>
  2007. */