SimpleMessageAcceptanceTest.php 42KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  1. <?php
  2. require_once 'Swift/Tests/SwiftUnitTestCase.php';
  3. class Swift_Mime_SimpleMessageAcceptanceTest
  4. extends Swift_Tests_SwiftUnitTestCase
  5. {
  6. public function setUp()
  7. {
  8. Swift_Preferences::getInstance()->setCharset(null); //TODO: Test with the charset defined
  9. }
  10. public function testBasicHeaders()
  11. {
  12. /* -- RFC 2822, 3.6.
  13. */
  14. $message = $this->_createMessage();
  15. $id = $message->getId();
  16. $date = $message->getDate();
  17. $this->assertEqual(
  18. 'Message-ID: <' . $id . '>' . "\r\n" .
  19. 'Date: ' . date('r', $date) . "\r\n" .
  20. 'From: ' . "\r\n" .
  21. 'MIME-Version: 1.0' . "\r\n" .
  22. 'Content-Type: text/plain' . "\r\n" .
  23. 'Content-Transfer-Encoding: quoted-printable' . "\r\n",
  24. $message->toString(),
  25. '%s: Only required headers, and non-empty headers should be displayed'
  26. );
  27. }
  28. public function testSubjectIsDisplayedIfSet()
  29. {
  30. $message = $this->_createMessage();
  31. $message->setSubject('just a test subject');
  32. $id = $message->getId();
  33. $date = $message->getDate();
  34. $this->assertEqual(
  35. 'Message-ID: <' . $id . '>' . "\r\n" .
  36. 'Date: ' . date('r', $date) . "\r\n" .
  37. 'Subject: just a test subject' . "\r\n" .
  38. 'From: ' . "\r\n" .
  39. 'MIME-Version: 1.0' . "\r\n" .
  40. 'Content-Type: text/plain' . "\r\n" .
  41. 'Content-Transfer-Encoding: quoted-printable' . "\r\n",
  42. $message->toString()
  43. );
  44. }
  45. public function testDateCanBeSet()
  46. {
  47. $message = $this->_createMessage();
  48. $message->setSubject('just a test subject');
  49. $id = $message->getId();
  50. $message->setDate(1234);
  51. $this->assertEqual(
  52. 'Message-ID: <' . $id . '>' . "\r\n" .
  53. 'Date: ' . date('r', 1234) . "\r\n" .
  54. 'Subject: just a test subject' . "\r\n" .
  55. 'From: ' . "\r\n" .
  56. 'MIME-Version: 1.0' . "\r\n" .
  57. 'Content-Type: text/plain' . "\r\n" .
  58. 'Content-Transfer-Encoding: quoted-printable' . "\r\n",
  59. $message->toString()
  60. );
  61. }
  62. public function testMessageIdCanBeSet()
  63. {
  64. $message = $this->_createMessage();
  65. $message->setSubject('just a test subject');
  66. $message->setId('foo@bar');
  67. $date = $message->getDate();
  68. $this->assertEqual(
  69. 'Message-ID: <foo@bar>' . "\r\n" .
  70. 'Date: ' . date('r', $date) . "\r\n" .
  71. 'Subject: just a test subject' . "\r\n" .
  72. 'From: ' . "\r\n" .
  73. 'MIME-Version: 1.0' . "\r\n" .
  74. 'Content-Type: text/plain' . "\r\n" .
  75. 'Content-Transfer-Encoding: quoted-printable' . "\r\n",
  76. $message->toString()
  77. );
  78. }
  79. public function testContentTypeCanBeChanged()
  80. {
  81. $message = $this->_createMessage();
  82. $message->setSubject('just a test subject');
  83. $message->setContentType('text/html');
  84. $id = $message->getId();
  85. $date = $message->getDate();
  86. $this->assertEqual(
  87. 'Message-ID: <' . $id . '>' . "\r\n" .
  88. 'Date: ' . date('r', $date) . "\r\n" .
  89. 'Subject: just a test subject' . "\r\n" .
  90. 'From: ' . "\r\n" .
  91. 'MIME-Version: 1.0' . "\r\n" .
  92. 'Content-Type: text/html' . "\r\n" .
  93. 'Content-Transfer-Encoding: quoted-printable' . "\r\n",
  94. $message->toString()
  95. );
  96. }
  97. public function testCharsetCanBeSet()
  98. {
  99. $message = $this->_createMessage();
  100. $message->setSubject('just a test subject');
  101. $message->setContentType('text/html');
  102. $message->setCharset('iso-8859-1');
  103. $id = $message->getId();
  104. $date = $message->getDate();
  105. $this->assertEqual(
  106. 'Message-ID: <' . $id . '>' . "\r\n" .
  107. 'Date: ' . date('r', $date) . "\r\n" .
  108. 'Subject: just a test subject' . "\r\n" .
  109. 'From: ' . "\r\n" .
  110. 'MIME-Version: 1.0' . "\r\n" .
  111. 'Content-Type: text/html; charset=iso-8859-1' . "\r\n" .
  112. 'Content-Transfer-Encoding: quoted-printable' . "\r\n",
  113. $message->toString()
  114. );
  115. }
  116. public function testFormatCanBeSet()
  117. {
  118. $message = $this->_createMessage();
  119. $message->setSubject('just a test subject');
  120. $message->setFormat('flowed');
  121. $id = $message->getId();
  122. $date = $message->getDate();
  123. $this->assertEqual(
  124. 'Message-ID: <' . $id . '>' . "\r\n" .
  125. 'Date: ' . date('r', $date) . "\r\n" .
  126. 'Subject: just a test subject' . "\r\n" .
  127. 'From: ' . "\r\n" .
  128. 'MIME-Version: 1.0' . "\r\n" .
  129. 'Content-Type: text/plain; format=flowed' . "\r\n" .
  130. 'Content-Transfer-Encoding: quoted-printable' . "\r\n",
  131. $message->toString()
  132. );
  133. }
  134. public function testEncoderCanBeSet()
  135. {
  136. $message = $this->_createMessage();
  137. $message->setSubject('just a test subject');
  138. $message->setContentType('text/html');
  139. $message->setEncoder(
  140. new Swift_Mime_ContentEncoder_PlainContentEncoder('7bit')
  141. );
  142. $id = $message->getId();
  143. $date = $message->getDate();
  144. $this->assertEqual(
  145. 'Message-ID: <' . $id . '>' . "\r\n" .
  146. 'Date: ' . date('r', $date) . "\r\n" .
  147. 'Subject: just a test subject' . "\r\n" .
  148. 'From: ' . "\r\n" .
  149. 'MIME-Version: 1.0' . "\r\n" .
  150. 'Content-Type: text/html' . "\r\n" .
  151. 'Content-Transfer-Encoding: 7bit' . "\r\n",
  152. $message->toString()
  153. );
  154. }
  155. public function testFromAddressCanBeSet()
  156. {
  157. $message = $this->_createMessage();
  158. $message->setSubject('just a test subject');
  159. $message->setFrom('chris.corbyn@swiftmailer.org');
  160. $id = $message->getId();
  161. $date = $message->getDate();
  162. $this->assertEqual(
  163. 'Message-ID: <' . $id . '>' . "\r\n" .
  164. 'Date: ' . date('r', $date) . "\r\n" .
  165. 'Subject: just a test subject' . "\r\n" .
  166. 'From: chris.corbyn@swiftmailer.org' . "\r\n" .
  167. 'MIME-Version: 1.0' . "\r\n" .
  168. 'Content-Type: text/plain' . "\r\n" .
  169. 'Content-Transfer-Encoding: quoted-printable' . "\r\n",
  170. $message->toString()
  171. );
  172. }
  173. public function testFromAddressCanBeSetWithName()
  174. {
  175. $message = $this->_createMessage();
  176. $message->setSubject('just a test subject');
  177. $message->setFrom(array('chris.corbyn@swiftmailer.org' => 'Chris Corbyn'));
  178. $id = $message->getId();
  179. $date = $message->getDate();
  180. $this->assertEqual(
  181. 'Message-ID: <' . $id . '>' . "\r\n" .
  182. 'Date: ' . date('r', $date) . "\r\n" .
  183. 'Subject: just a test subject' . "\r\n" .
  184. 'From: Chris Corbyn <chris.corbyn@swiftmailer.org>' . "\r\n" .
  185. 'MIME-Version: 1.0' . "\r\n" .
  186. 'Content-Type: text/plain' . "\r\n" .
  187. 'Content-Transfer-Encoding: quoted-printable' . "\r\n",
  188. $message->toString()
  189. );
  190. }
  191. public function testMultipleFromAddressesCanBeSet()
  192. {
  193. $message = $this->_createMessage();
  194. $message->setSubject('just a test subject');
  195. $message->setFrom(array(
  196. 'chris.corbyn@swiftmailer.org' => 'Chris Corbyn',
  197. 'mark@swiftmailer.org'
  198. ));
  199. $id = $message->getId();
  200. $date = $message->getDate();
  201. $this->assertEqual(
  202. 'Message-ID: <' . $id . '>' . "\r\n" .
  203. 'Date: ' . date('r', $date) . "\r\n" .
  204. 'Subject: just a test subject' . "\r\n" .
  205. 'From: Chris Corbyn <chris.corbyn@swiftmailer.org>, mark@swiftmailer.org' . "\r\n" .
  206. 'MIME-Version: 1.0' . "\r\n" .
  207. 'Content-Type: text/plain' . "\r\n" .
  208. 'Content-Transfer-Encoding: quoted-printable' . "\r\n",
  209. $message->toString()
  210. );
  211. }
  212. public function testReturnPathAddressCanBeSet()
  213. {
  214. $message = $this->_createMessage();
  215. $message->setReturnPath('chris@w3style.co.uk');
  216. $message->setSubject('just a test subject');
  217. $message->setFrom(array(
  218. 'chris.corbyn@swiftmailer.org' => 'Chris Corbyn'));
  219. $id = $message->getId();
  220. $date = $message->getDate();
  221. $this->assertEqual(
  222. 'Return-Path: <chris@w3style.co.uk>' . "\r\n" .
  223. 'Message-ID: <' . $id . '>' . "\r\n" .
  224. 'Date: ' . date('r', $date) . "\r\n" .
  225. 'Subject: just a test subject' . "\r\n" .
  226. 'From: Chris Corbyn <chris.corbyn@swiftmailer.org>' . "\r\n" .
  227. 'MIME-Version: 1.0' . "\r\n" .
  228. 'Content-Type: text/plain' . "\r\n" .
  229. 'Content-Transfer-Encoding: quoted-printable' . "\r\n",
  230. $message->toString()
  231. );
  232. }
  233. public function testEmptyReturnPathHeaderCanBeUsed()
  234. {
  235. $message = $this->_createMessage();
  236. $message->setReturnPath('');
  237. $message->setSubject('just a test subject');
  238. $message->setFrom(array(
  239. 'chris.corbyn@swiftmailer.org' => 'Chris Corbyn'));
  240. $id = $message->getId();
  241. $date = $message->getDate();
  242. $this->assertEqual(
  243. 'Return-Path: <>' . "\r\n" .
  244. 'Message-ID: <' . $id . '>' . "\r\n" .
  245. 'Date: ' . date('r', $date) . "\r\n" .
  246. 'Subject: just a test subject' . "\r\n" .
  247. 'From: Chris Corbyn <chris.corbyn@swiftmailer.org>' . "\r\n" .
  248. 'MIME-Version: 1.0' . "\r\n" .
  249. 'Content-Type: text/plain' . "\r\n" .
  250. 'Content-Transfer-Encoding: quoted-printable' . "\r\n",
  251. $message->toString()
  252. );
  253. }
  254. public function testSenderCanBeSet()
  255. {
  256. $message = $this->_createMessage();
  257. $message->setSubject('just a test subject');
  258. $message->setSender('chris.corbyn@swiftmailer.org');
  259. $id = $message->getId();
  260. $date = $message->getDate();
  261. $this->assertEqual(
  262. 'Sender: chris.corbyn@swiftmailer.org' . "\r\n" .
  263. 'Message-ID: <' . $id . '>' . "\r\n" .
  264. 'Date: ' . date('r', $date) . "\r\n" .
  265. 'Subject: just a test subject' . "\r\n" .
  266. 'From: ' . "\r\n" .
  267. 'MIME-Version: 1.0' . "\r\n" .
  268. 'Content-Type: text/plain' . "\r\n" .
  269. 'Content-Transfer-Encoding: quoted-printable' . "\r\n",
  270. $message->toString()
  271. );
  272. }
  273. public function testSenderCanBeSetWithName()
  274. {
  275. $message = $this->_createMessage();
  276. $message->setSubject('just a test subject');
  277. $message->setSender(array('chris.corbyn@swiftmailer.org'=>'Chris'));
  278. $id = $message->getId();
  279. $date = $message->getDate();
  280. $this->assertEqual(
  281. 'Sender: Chris <chris.corbyn@swiftmailer.org>' . "\r\n" .
  282. 'Message-ID: <' . $id . '>' . "\r\n" .
  283. 'Date: ' . date('r', $date) . "\r\n" .
  284. 'Subject: just a test subject' . "\r\n" .
  285. 'From: ' . "\r\n" .
  286. 'MIME-Version: 1.0' . "\r\n" .
  287. 'Content-Type: text/plain' . "\r\n" .
  288. 'Content-Transfer-Encoding: quoted-printable' . "\r\n",
  289. $message->toString()
  290. );
  291. }
  292. public function testReplyToCanBeSet()
  293. {
  294. $message = $this->_createMessage();
  295. $message->setSubject('just a test subject');
  296. $message->setFrom(array('chris.corbyn@swiftmailer.org'=>'Chris'));
  297. $message->setReplyTo(array('chris@w3style.co.uk'=>'Myself'));
  298. $id = $message->getId();
  299. $date = $message->getDate();
  300. $this->assertEqual(
  301. 'Message-ID: <' . $id . '>' . "\r\n" .
  302. 'Date: ' . date('r', $date) . "\r\n" .
  303. 'Subject: just a test subject' . "\r\n" .
  304. 'From: Chris <chris.corbyn@swiftmailer.org>' . "\r\n" .
  305. 'Reply-To: Myself <chris@w3style.co.uk>' . "\r\n" .
  306. 'MIME-Version: 1.0' . "\r\n" .
  307. 'Content-Type: text/plain' . "\r\n" .
  308. 'Content-Transfer-Encoding: quoted-printable' . "\r\n",
  309. $message->toString()
  310. );
  311. }
  312. public function testMultipleReplyAddressCanBeUsed()
  313. {
  314. $message = $this->_createMessage();
  315. $message->setSubject('just a test subject');
  316. $message->setFrom(array('chris.corbyn@swiftmailer.org'=>'Chris'));
  317. $message->setReplyTo(array(
  318. 'chris@w3style.co.uk' => 'Myself',
  319. 'my.other@address.com' => 'Me'
  320. ));
  321. $id = $message->getId();
  322. $date = $message->getDate();
  323. $this->assertEqual(
  324. 'Message-ID: <' . $id . '>' . "\r\n" .
  325. 'Date: ' . date('r', $date) . "\r\n" .
  326. 'Subject: just a test subject' . "\r\n" .
  327. 'From: Chris <chris.corbyn@swiftmailer.org>' . "\r\n" .
  328. 'Reply-To: Myself <chris@w3style.co.uk>, Me <my.other@address.com>' . "\r\n" .
  329. 'MIME-Version: 1.0' . "\r\n" .
  330. 'Content-Type: text/plain' . "\r\n" .
  331. 'Content-Transfer-Encoding: quoted-printable' . "\r\n",
  332. $message->toString()
  333. );
  334. }
  335. public function testToAddressCanBeSet()
  336. {
  337. $message = $this->_createMessage();
  338. $message->setSubject('just a test subject');
  339. $message->setFrom(array('chris.corbyn@swiftmailer.org'=>'Chris'));
  340. $message->setReplyTo(array(
  341. 'chris@w3style.co.uk' => 'Myself',
  342. 'my.other@address.com' => 'Me'
  343. ));
  344. $message->setTo('mark@swiftmailer.org');
  345. $id = $message->getId();
  346. $date = $message->getDate();
  347. $this->assertEqual(
  348. 'Message-ID: <' . $id . '>' . "\r\n" .
  349. 'Date: ' . date('r', $date) . "\r\n" .
  350. 'Subject: just a test subject' . "\r\n" .
  351. 'From: Chris <chris.corbyn@swiftmailer.org>' . "\r\n" .
  352. 'Reply-To: Myself <chris@w3style.co.uk>, Me <my.other@address.com>' . "\r\n" .
  353. 'To: mark@swiftmailer.org' . "\r\n" .
  354. 'MIME-Version: 1.0' . "\r\n" .
  355. 'Content-Type: text/plain' . "\r\n" .
  356. 'Content-Transfer-Encoding: quoted-printable' . "\r\n",
  357. $message->toString()
  358. );
  359. }
  360. public function testMultipleToAddressesCanBeSet()
  361. {
  362. $message = $this->_createMessage();
  363. $message->setSubject('just a test subject');
  364. $message->setFrom(array('chris.corbyn@swiftmailer.org'=>'Chris'));
  365. $message->setReplyTo(array(
  366. 'chris@w3style.co.uk' => 'Myself',
  367. 'my.other@address.com' => 'Me'
  368. ));
  369. $message->setTo(array(
  370. 'mark@swiftmailer.org', 'chris@swiftmailer.org' => 'Chris Corbyn'
  371. ));
  372. $id = $message->getId();
  373. $date = $message->getDate();
  374. $this->assertEqual(
  375. 'Message-ID: <' . $id . '>' . "\r\n" .
  376. 'Date: ' . date('r', $date) . "\r\n" .
  377. 'Subject: just a test subject' . "\r\n" .
  378. 'From: Chris <chris.corbyn@swiftmailer.org>' . "\r\n" .
  379. 'Reply-To: Myself <chris@w3style.co.uk>, Me <my.other@address.com>' . "\r\n" .
  380. 'To: mark@swiftmailer.org, Chris Corbyn <chris@swiftmailer.org>' . "\r\n" .
  381. 'MIME-Version: 1.0' . "\r\n" .
  382. 'Content-Type: text/plain' . "\r\n" .
  383. 'Content-Transfer-Encoding: quoted-printable' . "\r\n",
  384. $message->toString()
  385. );
  386. }
  387. public function testCcAddressCanBeSet()
  388. {
  389. $message = $this->_createMessage();
  390. $message->setSubject('just a test subject');
  391. $message->setFrom(array('chris.corbyn@swiftmailer.org'=>'Chris'));
  392. $message->setReplyTo(array(
  393. 'chris@w3style.co.uk' => 'Myself',
  394. 'my.other@address.com' => 'Me'
  395. ));
  396. $message->setTo(array(
  397. 'mark@swiftmailer.org', 'chris@swiftmailer.org' => 'Chris Corbyn'
  398. ));
  399. $message->setCc('john@some-site.com');
  400. $id = $message->getId();
  401. $date = $message->getDate();
  402. $this->assertEqual(
  403. 'Message-ID: <' . $id . '>' . "\r\n" .
  404. 'Date: ' . date('r', $date) . "\r\n" .
  405. 'Subject: just a test subject' . "\r\n" .
  406. 'From: Chris <chris.corbyn@swiftmailer.org>' . "\r\n" .
  407. 'Reply-To: Myself <chris@w3style.co.uk>, Me <my.other@address.com>' . "\r\n" .
  408. 'To: mark@swiftmailer.org, Chris Corbyn <chris@swiftmailer.org>' . "\r\n" .
  409. 'Cc: john@some-site.com' . "\r\n" .
  410. 'MIME-Version: 1.0' . "\r\n" .
  411. 'Content-Type: text/plain' . "\r\n" .
  412. 'Content-Transfer-Encoding: quoted-printable' . "\r\n",
  413. $message->toString()
  414. );
  415. }
  416. public function testMultipleCcAddressesCanBeSet()
  417. {
  418. $message = $this->_createMessage();
  419. $message->setSubject('just a test subject');
  420. $message->setFrom(array('chris.corbyn@swiftmailer.org'=>'Chris'));
  421. $message->setReplyTo(array(
  422. 'chris@w3style.co.uk' => 'Myself',
  423. 'my.other@address.com' => 'Me'
  424. ));
  425. $message->setTo(array(
  426. 'mark@swiftmailer.org', 'chris@swiftmailer.org' => 'Chris Corbyn'
  427. ));
  428. $message->setCc(array(
  429. 'john@some-site.com' => 'John West',
  430. 'fred@another-site.co.uk' => 'Big Fred'
  431. ));
  432. $id = $message->getId();
  433. $date = $message->getDate();
  434. $this->assertEqual(
  435. 'Message-ID: <' . $id . '>' . "\r\n" .
  436. 'Date: ' . date('r', $date) . "\r\n" .
  437. 'Subject: just a test subject' . "\r\n" .
  438. 'From: Chris <chris.corbyn@swiftmailer.org>' . "\r\n" .
  439. 'Reply-To: Myself <chris@w3style.co.uk>, Me <my.other@address.com>' . "\r\n" .
  440. 'To: mark@swiftmailer.org, Chris Corbyn <chris@swiftmailer.org>' . "\r\n" .
  441. 'Cc: John West <john@some-site.com>, Big Fred <fred@another-site.co.uk>' . "\r\n" .
  442. 'MIME-Version: 1.0' . "\r\n" .
  443. 'Content-Type: text/plain' . "\r\n" .
  444. 'Content-Transfer-Encoding: quoted-printable' . "\r\n",
  445. $message->toString()
  446. );
  447. }
  448. public function testBccAddressCanBeSet()
  449. {
  450. //Obviously Transports need to setBcc(array()) and send to each Bcc recipient
  451. // separately in accordance with RFC 2822/2821
  452. $message = $this->_createMessage();
  453. $message->setSubject('just a test subject');
  454. $message->setFrom(array('chris.corbyn@swiftmailer.org'=>'Chris'));
  455. $message->setReplyTo(array(
  456. 'chris@w3style.co.uk' => 'Myself',
  457. 'my.other@address.com' => 'Me'
  458. ));
  459. $message->setTo(array(
  460. 'mark@swiftmailer.org', 'chris@swiftmailer.org' => 'Chris Corbyn'
  461. ));
  462. $message->setCc(array(
  463. 'john@some-site.com' => 'John West',
  464. 'fred@another-site.co.uk' => 'Big Fred'
  465. ));
  466. $message->setBcc('x@alphabet.tld');
  467. $id = $message->getId();
  468. $date = $message->getDate();
  469. $this->assertEqual(
  470. 'Message-ID: <' . $id . '>' . "\r\n" .
  471. 'Date: ' . date('r', $date) . "\r\n" .
  472. 'Subject: just a test subject' . "\r\n" .
  473. 'From: Chris <chris.corbyn@swiftmailer.org>' . "\r\n" .
  474. 'Reply-To: Myself <chris@w3style.co.uk>, Me <my.other@address.com>' . "\r\n" .
  475. 'To: mark@swiftmailer.org, Chris Corbyn <chris@swiftmailer.org>' . "\r\n" .
  476. 'Cc: John West <john@some-site.com>, Big Fred <fred@another-site.co.uk>' . "\r\n" .
  477. 'Bcc: x@alphabet.tld' . "\r\n" .
  478. 'MIME-Version: 1.0' . "\r\n" .
  479. 'Content-Type: text/plain' . "\r\n" .
  480. 'Content-Transfer-Encoding: quoted-printable' . "\r\n",
  481. $message->toString()
  482. );
  483. }
  484. public function testMultipleBccAddressesCanBeSet()
  485. {
  486. //Obviously Transports need to setBcc(array()) and send to each Bcc recipient
  487. // separately in accordance with RFC 2822/2821
  488. $message = $this->_createMessage();
  489. $message->setSubject('just a test subject');
  490. $message->setFrom(array('chris.corbyn@swiftmailer.org'=>'Chris'));
  491. $message->setReplyTo(array(
  492. 'chris@w3style.co.uk' => 'Myself',
  493. 'my.other@address.com' => 'Me'
  494. ));
  495. $message->setTo(array(
  496. 'mark@swiftmailer.org', 'chris@swiftmailer.org' => 'Chris Corbyn'
  497. ));
  498. $message->setCc(array(
  499. 'john@some-site.com' => 'John West',
  500. 'fred@another-site.co.uk' => 'Big Fred'
  501. ));
  502. $message->setBcc(array('x@alphabet.tld', 'a@alphabet.tld' => 'A'));
  503. $id = $message->getId();
  504. $date = $message->getDate();
  505. $this->assertEqual(
  506. 'Message-ID: <' . $id . '>' . "\r\n" .
  507. 'Date: ' . date('r', $date) . "\r\n" .
  508. 'Subject: just a test subject' . "\r\n" .
  509. 'From: Chris <chris.corbyn@swiftmailer.org>' . "\r\n" .
  510. 'Reply-To: Myself <chris@w3style.co.uk>, Me <my.other@address.com>' . "\r\n" .
  511. 'To: mark@swiftmailer.org, Chris Corbyn <chris@swiftmailer.org>' . "\r\n" .
  512. 'Cc: John West <john@some-site.com>, Big Fred <fred@another-site.co.uk>' . "\r\n" .
  513. 'Bcc: x@alphabet.tld, A <a@alphabet.tld>' . "\r\n" .
  514. 'MIME-Version: 1.0' . "\r\n" .
  515. 'Content-Type: text/plain' . "\r\n" .
  516. 'Content-Transfer-Encoding: quoted-printable' . "\r\n",
  517. $message->toString()
  518. );
  519. }
  520. public function testStringBodyIsAppended()
  521. {
  522. $message = $this->_createMessage();
  523. $message->setReturnPath('chris@w3style.co.uk');
  524. $message->setSubject('just a test subject');
  525. $message->setFrom(array(
  526. 'chris.corbyn@swiftmailer.org' => 'Chris Corbyn'));
  527. $message->setBody(
  528. 'just a test body' . "\r\n" .
  529. 'with a new line'
  530. );
  531. $id = $message->getId();
  532. $date = $message->getDate();
  533. $this->assertEqual(
  534. 'Return-Path: <chris@w3style.co.uk>' . "\r\n" .
  535. 'Message-ID: <' . $id . '>' . "\r\n" .
  536. 'Date: ' . date('r', $date) . "\r\n" .
  537. 'Subject: just a test subject' . "\r\n" .
  538. 'From: Chris Corbyn <chris.corbyn@swiftmailer.org>' . "\r\n" .
  539. 'MIME-Version: 1.0' . "\r\n" .
  540. 'Content-Type: text/plain' . "\r\n" .
  541. 'Content-Transfer-Encoding: quoted-printable' . "\r\n" .
  542. "\r\n" .
  543. 'just a test body' . "\r\n" .
  544. 'with a new line',
  545. $message->toString()
  546. );
  547. }
  548. public function testStringBodyIsEncoded()
  549. {
  550. $message = $this->_createMessage();
  551. $message->setReturnPath('chris@w3style.co.uk');
  552. $message->setSubject('just a test subject');
  553. $message->setFrom(array(
  554. 'chris.corbyn@swiftmailer.org' => 'Chris Corbyn'));
  555. $message->setBody(
  556. 'Just s' . pack('C*', 0xC2, 0x01, 0x01) . 'me multi-' . "\r\n" .
  557. 'line message!'
  558. );
  559. $id = $message->getId();
  560. $date = $message->getDate();
  561. $this->assertEqual(
  562. 'Return-Path: <chris@w3style.co.uk>' . "\r\n" .
  563. 'Message-ID: <' . $id . '>' . "\r\n" .
  564. 'Date: ' . date('r', $date) . "\r\n" .
  565. 'Subject: just a test subject' . "\r\n" .
  566. 'From: Chris Corbyn <chris.corbyn@swiftmailer.org>' . "\r\n" .
  567. 'MIME-Version: 1.0' . "\r\n" .
  568. 'Content-Type: text/plain' . "\r\n" .
  569. 'Content-Transfer-Encoding: quoted-printable' . "\r\n" .
  570. "\r\n" .
  571. 'Just s=C2=01=01me multi-' . "\r\n" .
  572. 'line message!',
  573. $message->toString()
  574. );
  575. }
  576. public function testChildrenCanBeAttached()
  577. {
  578. $message = $this->_createMessage();
  579. $message->setReturnPath('chris@w3style.co.uk');
  580. $message->setSubject('just a test subject');
  581. $message->setFrom(array(
  582. 'chris.corbyn@swiftmailer.org' => 'Chris Corbyn'));
  583. $id = $message->getId();
  584. $date = $message->getDate();
  585. $boundary = $message->getBoundary();
  586. $part1 = $this->_createMimePart();
  587. $part1->setContentType('text/plain');
  588. $part1->setCharset('iso-8859-1');
  589. $part1->setBody('foo');
  590. $message->attach($part1);
  591. $part2 = $this->_createMimePart();
  592. $part2->setContentType('text/html');
  593. $part2->setCharset('iso-8859-1');
  594. $part2->setBody('test <b>foo</b>');
  595. $message->attach($part2);
  596. $this->assertEqual(
  597. 'Return-Path: <chris@w3style.co.uk>' . "\r\n" .
  598. 'Message-ID: <' . $id . '>' . "\r\n" .
  599. 'Date: ' . date('r', $date) . "\r\n" .
  600. 'Subject: just a test subject' . "\r\n" .
  601. 'From: Chris Corbyn <chris.corbyn@swiftmailer.org>' . "\r\n" .
  602. 'MIME-Version: 1.0' . "\r\n" .
  603. 'Content-Type: multipart/alternative;' . "\r\n" .
  604. ' boundary="' . $boundary . '"' . "\r\n" .
  605. "\r\n\r\n" .
  606. '--' . $boundary . "\r\n" .
  607. 'Content-Type: text/plain; charset=iso-8859-1' . "\r\n" .
  608. 'Content-Transfer-Encoding: quoted-printable' . "\r\n" .
  609. "\r\n" .
  610. 'foo' .
  611. "\r\n\r\n" .
  612. '--' . $boundary . "\r\n" .
  613. 'Content-Type: text/html; charset=iso-8859-1' . "\r\n" .
  614. 'Content-Transfer-Encoding: quoted-printable' . "\r\n" .
  615. "\r\n" .
  616. 'test <b>foo</b>' .
  617. "\r\n\r\n" .
  618. '--' . $boundary . '--' . "\r\n",
  619. $message->toString()
  620. );
  621. }
  622. public function testAttachmentsBeingAttached()
  623. {
  624. $message = $this->_createMessage();
  625. $message->setReturnPath('chris@w3style.co.uk');
  626. $message->setSubject('just a test subject');
  627. $message->setFrom(array(
  628. 'chris.corbyn@swiftmailer.org' => 'Chris Corbyn'));
  629. $id = $message->getId();
  630. $date = preg_quote(date('r', $message->getDate()), '~');
  631. $boundary = $message->getBoundary();
  632. $part = $this->_createMimePart();
  633. $part->setContentType('text/plain');
  634. $part->setCharset('iso-8859-1');
  635. $part->setBody('foo');
  636. $message->attach($part);
  637. $attachment = $this->_createAttachment();
  638. $attachment->setContentType('application/pdf');
  639. $attachment->setFilename('foo.pdf');
  640. $attachment->setBody('<pdf data>');
  641. $message->attach($attachment);
  642. $this->assertPattern(
  643. '~^' .
  644. 'Return-Path: <chris@w3style.co.uk>' . "\r\n" .
  645. 'Message-ID: <' . $id . '>' . "\r\n" .
  646. 'Date: ' . $date . "\r\n" .
  647. 'Subject: just a test subject' . "\r\n" .
  648. 'From: Chris Corbyn <chris.corbyn@swiftmailer.org>' . "\r\n" .
  649. 'MIME-Version: 1.0' . "\r\n" .
  650. 'Content-Type: multipart/mixed;' . "\r\n" .
  651. ' boundary="' . $boundary . '"' . "\r\n" .
  652. "\r\n\r\n" .
  653. '--' . $boundary . "\r\n" .
  654. 'Content-Type: multipart/alternative;' . "\r\n" .
  655. ' boundary="(.*?)"' . "\r\n" .
  656. "\r\n\r\n" .
  657. '--\\1' . "\r\n" .
  658. 'Content-Type: text/plain; charset=iso-8859-1' . "\r\n" .
  659. 'Content-Transfer-Encoding: quoted-printable' . "\r\n" .
  660. "\r\n" .
  661. 'foo' .
  662. "\r\n\r\n" .
  663. '--\\1--' . "\r\n" .
  664. "\r\n\r\n" .
  665. '--' . $boundary . "\r\n" .
  666. 'Content-Type: application/pdf; name=foo.pdf' . "\r\n" .
  667. 'Content-Transfer-Encoding: base64' . "\r\n" .
  668. 'Content-Disposition: attachment; filename=foo.pdf' . "\r\n" .
  669. "\r\n" .
  670. preg_quote(base64_encode('<pdf data>'), '~') .
  671. "\r\n\r\n" .
  672. '--' . $boundary . '--' . "\r\n" .
  673. '$~D',
  674. $message->toString()
  675. );
  676. }
  677. public function testAttachmentsAndEmbeddedFilesBeingAttached()
  678. {
  679. $message = $this->_createMessage();
  680. $message->setReturnPath('chris@w3style.co.uk');
  681. $message->setSubject('just a test subject');
  682. $message->setFrom(array(
  683. 'chris.corbyn@swiftmailer.org' => 'Chris Corbyn'));
  684. $id = $message->getId();
  685. $date = preg_quote(date('r', $message->getDate()), '~');
  686. $boundary = $message->getBoundary();
  687. $part = $this->_createMimePart();
  688. $part->setContentType('text/plain');
  689. $part->setCharset('iso-8859-1');
  690. $part->setBody('foo');
  691. $message->attach($part);
  692. $attachment = $this->_createAttachment();
  693. $attachment->setContentType('application/pdf');
  694. $attachment->setFilename('foo.pdf');
  695. $attachment->setBody('<pdf data>');
  696. $message->attach($attachment);
  697. $file = $this->_createEmbeddedFile();
  698. $file->setContentType('image/jpeg');
  699. $file->setFilename('myimage.jpg');
  700. $file->setBody('<image data>');
  701. $message->attach($file);
  702. $cid = $file->getId();
  703. $this->assertPattern(
  704. '~^' .
  705. 'Return-Path: <chris@w3style.co.uk>' . "\r\n" .
  706. 'Message-ID: <' . $id . '>' . "\r\n" .
  707. 'Date: ' . $date . "\r\n" .
  708. 'Subject: just a test subject' . "\r\n" .
  709. 'From: Chris Corbyn <chris.corbyn@swiftmailer.org>' . "\r\n" .
  710. 'MIME-Version: 1.0' . "\r\n" .
  711. 'Content-Type: multipart/mixed;' . "\r\n" .
  712. ' boundary="' . $boundary . '"' . "\r\n" .
  713. "\r\n\r\n" .
  714. '--' . $boundary . "\r\n" .
  715. 'Content-Type: multipart/alternative;' . "\r\n" .
  716. ' boundary="(.*?)"' . "\r\n" .
  717. "\r\n\r\n" .
  718. '--\\1' . "\r\n" .
  719. 'Content-Type: text/plain; charset=iso-8859-1' . "\r\n" .
  720. 'Content-Transfer-Encoding: quoted-printable' . "\r\n" .
  721. "\r\n" .
  722. 'foo' .
  723. "\r\n\r\n" .
  724. '--\\1' . "\r\n" .
  725. 'Content-Type: multipart/related;' . "\r\n" .
  726. ' boundary="(.*?)"' . "\r\n" .
  727. "\r\n\r\n" .
  728. '--\\2' . "\r\n" .
  729. 'Content-Type: image/jpeg; name=myimage.jpg' . "\r\n" .
  730. 'Content-Transfer-Encoding: base64' . "\r\n" .
  731. 'Content-Disposition: inline; filename=myimage.jpg' . "\r\n" .
  732. 'Content-ID: <' . $cid . '>' . "\r\n" .
  733. "\r\n" .
  734. preg_quote(base64_encode('<image data>'), '~') .
  735. "\r\n\r\n" .
  736. '--\\2--' . "\r\n" .
  737. "\r\n\r\n" .
  738. '--\\1--' . "\r\n" .
  739. "\r\n\r\n" .
  740. '--' . $boundary . "\r\n" .
  741. 'Content-Type: application/pdf; name=foo.pdf' . "\r\n" .
  742. 'Content-Transfer-Encoding: base64' . "\r\n" .
  743. 'Content-Disposition: attachment; filename=foo.pdf' . "\r\n" .
  744. "\r\n" .
  745. preg_quote(base64_encode('<pdf data>'), '~') .
  746. "\r\n\r\n" .
  747. '--' . $boundary . '--' . "\r\n" .
  748. '$~D',
  749. $message->toString()
  750. );
  751. }
  752. public function testComplexEmbeddingOfContent()
  753. {
  754. $message = $this->_createMessage();
  755. $message->setReturnPath('chris@w3style.co.uk');
  756. $message->setSubject('just a test subject');
  757. $message->setFrom(array(
  758. 'chris.corbyn@swiftmailer.org' => 'Chris Corbyn'));
  759. $id = $message->getId();
  760. $date = preg_quote(date('r', $message->getDate()), '~');
  761. $boundary = $message->getBoundary();
  762. $attachment = $this->_createAttachment();
  763. $attachment->setContentType('application/pdf');
  764. $attachment->setFilename('foo.pdf');
  765. $attachment->setBody('<pdf data>');
  766. $message->attach($attachment);
  767. $file = $this->_createEmbeddedFile();
  768. $file->setContentType('image/jpeg');
  769. $file->setFilename('myimage.jpg');
  770. $file->setBody('<image data>');
  771. $part = $this->_createMimePart();
  772. $part->setContentType('text/html');
  773. $part->setCharset('iso-8859-1');
  774. $part->setBody('foo <img src="' . $message->embed($file) . '" />');
  775. $message->attach($part);
  776. $cid = $file->getId();
  777. $this->assertPattern(
  778. '~^' .
  779. 'Return-Path: <chris@w3style.co.uk>' . "\r\n" .
  780. 'Message-ID: <' . $id . '>' . "\r\n" .
  781. 'Date: ' . $date . "\r\n" .
  782. 'Subject: just a test subject' . "\r\n" .
  783. 'From: Chris Corbyn <chris.corbyn@swiftmailer.org>' . "\r\n" .
  784. 'MIME-Version: 1.0' . "\r\n" .
  785. 'Content-Type: multipart/mixed;' . "\r\n" .
  786. ' boundary="' . $boundary . '"' . "\r\n" .
  787. "\r\n\r\n" .
  788. '--' . $boundary . "\r\n" .
  789. 'Content-Type: multipart/related;' . "\r\n" .
  790. ' boundary="(.*?)"' . "\r\n" .
  791. "\r\n\r\n" .
  792. '--\\1' . "\r\n" .
  793. 'Content-Type: text/html; charset=iso-8859-1' . "\r\n" .
  794. 'Content-Transfer-Encoding: quoted-printable' . "\r\n" .
  795. "\r\n" .
  796. 'foo <img src=3D"cid:' . $cid . '" />' . //=3D is just = in QP
  797. "\r\n\r\n" .
  798. '--\\1' . "\r\n" .
  799. 'Content-Type: image/jpeg; name=myimage.jpg' . "\r\n" .
  800. 'Content-Transfer-Encoding: base64' . "\r\n" .
  801. 'Content-Disposition: inline; filename=myimage.jpg' . "\r\n" .
  802. 'Content-ID: <' . $cid . '>' . "\r\n" .
  803. "\r\n" .
  804. preg_quote(base64_encode('<image data>'), '~') .
  805. "\r\n\r\n" .
  806. '--\\1--' . "\r\n" .
  807. "\r\n\r\n" .
  808. '--' . $boundary . "\r\n" .
  809. 'Content-Type: application/pdf; name=foo.pdf' . "\r\n" .
  810. 'Content-Transfer-Encoding: base64' . "\r\n" .
  811. 'Content-Disposition: attachment; filename=foo.pdf' . "\r\n" .
  812. "\r\n" .
  813. preg_quote(base64_encode('<pdf data>'), '~') .
  814. "\r\n\r\n" .
  815. '--' . $boundary . '--' . "\r\n" .
  816. '$~D',
  817. $message->toString()
  818. );
  819. }
  820. public function testAttachingAndDetachingContent()
  821. {
  822. $message = $this->_createMessage();
  823. $message->setReturnPath('chris@w3style.co.uk');
  824. $message->setSubject('just a test subject');
  825. $message->setFrom(array(
  826. 'chris.corbyn@swiftmailer.org' => 'Chris Corbyn'));
  827. $id = $message->getId();
  828. $date = preg_quote(date('r', $message->getDate()), '~');
  829. $boundary = $message->getBoundary();
  830. $part = $this->_createMimePart();
  831. $part->setContentType('text/plain');
  832. $part->setCharset('iso-8859-1');
  833. $part->setBody('foo');
  834. $message->attach($part);
  835. $attachment = $this->_createAttachment();
  836. $attachment->setContentType('application/pdf');
  837. $attachment->setFilename('foo.pdf');
  838. $attachment->setBody('<pdf data>');
  839. $message->attach($attachment);
  840. $file = $this->_createEmbeddedFile();
  841. $file->setContentType('image/jpeg');
  842. $file->setFilename('myimage.jpg');
  843. $file->setBody('<image data>');
  844. $message->attach($file);
  845. $cid = $file->getId();
  846. $message->detach($attachment);
  847. $this->assertPattern(
  848. '~^' .
  849. 'Return-Path: <chris@w3style.co.uk>' . "\r\n" .
  850. 'Message-ID: <' . $id . '>' . "\r\n" .
  851. 'Date: ' . $date . "\r\n" .
  852. 'Subject: just a test subject' . "\r\n" .
  853. 'From: Chris Corbyn <chris.corbyn@swiftmailer.org>' . "\r\n" .
  854. 'MIME-Version: 1.0' . "\r\n" .
  855. 'Content-Type: multipart/alternative;' . "\r\n" .
  856. ' boundary="' . $boundary . '"' . "\r\n" .
  857. "\r\n\r\n" .
  858. '--' . $boundary . "\r\n" .
  859. 'Content-Type: text/plain; charset=iso-8859-1' . "\r\n" .
  860. 'Content-Transfer-Encoding: quoted-printable' . "\r\n" .
  861. "\r\n" .
  862. 'foo' .
  863. "\r\n\r\n" .
  864. '--' . $boundary . "\r\n" .
  865. 'Content-Type: multipart/related;' . "\r\n" .
  866. ' boundary="(.*?)"' . "\r\n" .
  867. "\r\n\r\n" .
  868. '--\\1' . "\r\n" .
  869. 'Content-Type: image/jpeg; name=myimage.jpg' . "\r\n" .
  870. 'Content-Transfer-Encoding: base64' . "\r\n" .
  871. 'Content-Disposition: inline; filename=myimage.jpg' . "\r\n" .
  872. 'Content-ID: <' . $cid . '>' . "\r\n" .
  873. "\r\n" .
  874. preg_quote(base64_encode('<image data>'), '~') .
  875. "\r\n\r\n" .
  876. '--\\1--' . "\r\n" .
  877. "\r\n\r\n" .
  878. '--' . $boundary . '--' . "\r\n" .
  879. '$~D',
  880. $message->toString(),
  881. '%s: Attachment should have been detached'
  882. );
  883. }
  884. public function testBoundaryDoesNotAppearAfterAllPartsAreDetached()
  885. {
  886. $message = $this->_createMessage();
  887. $message->setReturnPath('chris@w3style.co.uk');
  888. $message->setSubject('just a test subject');
  889. $message->setFrom(array(
  890. 'chris.corbyn@swiftmailer.org' => 'Chris Corbyn'));
  891. $id = $message->getId();
  892. $date = $message->getDate();
  893. $boundary = $message->getBoundary();
  894. $part1 = $this->_createMimePart();
  895. $part1->setContentType('text/plain');
  896. $part1->setCharset('iso-8859-1');
  897. $part1->setBody('foo');
  898. $message->attach($part1);
  899. $part2 = $this->_createMimePart();
  900. $part2->setContentType('text/html');
  901. $part2->setCharset('iso-8859-1');
  902. $part2->setBody('test <b>foo</b>');
  903. $message->attach($part2);
  904. $message->detach($part1);
  905. $message->detach($part2);
  906. $this->assertEqual(
  907. 'Return-Path: <chris@w3style.co.uk>' . "\r\n" .
  908. 'Message-ID: <' . $id . '>' . "\r\n" .
  909. 'Date: ' . date('r', $date) . "\r\n" .
  910. 'Subject: just a test subject' . "\r\n" .
  911. 'From: Chris Corbyn <chris.corbyn@swiftmailer.org>' . "\r\n" .
  912. 'MIME-Version: 1.0' . "\r\n" .
  913. 'Content-Type: text/plain' . "\r\n" .
  914. 'Content-Transfer-Encoding: quoted-printable' . "\r\n",
  915. $message->toString(),
  916. '%s: Message should be restored to orignal state after parts are detached'
  917. );
  918. }
  919. public function testCharsetFormatOrDelSpAreNotShownWhenBoundaryIsSet()
  920. {
  921. $message = $this->_createMessage();
  922. $message->setReturnPath('chris@w3style.co.uk');
  923. $message->setSubject('just a test subject');
  924. $message->setFrom(array(
  925. 'chris.corbyn@swiftmailer.org' => 'Chris Corbyn'));
  926. $message->setCharset('utf-8');
  927. $message->setFormat('flowed');
  928. $message->setDelSp(true);
  929. $id = $message->getId();
  930. $date = $message->getDate();
  931. $boundary = $message->getBoundary();
  932. $part1 = $this->_createMimePart();
  933. $part1->setContentType('text/plain');
  934. $part1->setCharset('iso-8859-1');
  935. $part1->setBody('foo');
  936. $message->attach($part1);
  937. $part2 = $this->_createMimePart();
  938. $part2->setContentType('text/html');
  939. $part2->setCharset('iso-8859-1');
  940. $part2->setBody('test <b>foo</b>');
  941. $message->attach($part2);
  942. $this->assertEqual(
  943. 'Return-Path: <chris@w3style.co.uk>' . "\r\n" .
  944. 'Message-ID: <' . $id . '>' . "\r\n" .
  945. 'Date: ' . date('r', $date) . "\r\n" .
  946. 'Subject: just a test subject' . "\r\n" .
  947. 'From: Chris Corbyn <chris.corbyn@swiftmailer.org>' . "\r\n" .
  948. 'MIME-Version: 1.0' . "\r\n" .
  949. 'Content-Type: multipart/alternative;' . "\r\n" .
  950. ' boundary="' . $boundary . '"' . "\r\n" .
  951. "\r\n\r\n" .
  952. '--' . $boundary . "\r\n" .
  953. 'Content-Type: text/plain; charset=iso-8859-1' . "\r\n" .
  954. 'Content-Transfer-Encoding: quoted-printable' . "\r\n" .
  955. "\r\n" .
  956. 'foo' .
  957. "\r\n\r\n" .
  958. '--' . $boundary . "\r\n" .
  959. 'Content-Type: text/html; charset=iso-8859-1' . "\r\n" .
  960. 'Content-Transfer-Encoding: quoted-printable' . "\r\n" .
  961. "\r\n" .
  962. 'test <b>foo</b>' .
  963. "\r\n\r\n" .
  964. '--' . $boundary . '--' . "\r\n",
  965. $message->toString()
  966. );
  967. }
  968. public function testBodyCanBeSetWithAttachments()
  969. {
  970. $message = $this->_createMessage();
  971. $message->setReturnPath('chris@w3style.co.uk');
  972. $message->setSubject('just a test subject');
  973. $message->setFrom(array(
  974. 'chris.corbyn@swiftmailer.org' => 'Chris Corbyn'));
  975. $message->setContentType('text/html');
  976. $message->setCharset('iso-8859-1');
  977. $message->setBody('foo');
  978. $id = $message->getId();
  979. $date = date('r', $message->getDate());
  980. $boundary = $message->getBoundary();
  981. $attachment = $this->_createAttachment();
  982. $attachment->setContentType('application/pdf');
  983. $attachment->setFilename('foo.pdf');
  984. $attachment->setBody('<pdf data>');
  985. $message->attach($attachment);
  986. $this->assertEqual(
  987. 'Return-Path: <chris@w3style.co.uk>' . "\r\n" .
  988. 'Message-ID: <' . $id . '>' . "\r\n" .
  989. 'Date: ' . $date . "\r\n" .
  990. 'Subject: just a test subject' . "\r\n" .
  991. 'From: Chris Corbyn <chris.corbyn@swiftmailer.org>' . "\r\n" .
  992. 'MIME-Version: 1.0' . "\r\n" .
  993. 'Content-Type: multipart/mixed;' . "\r\n" .
  994. ' boundary="' . $boundary . '"' . "\r\n" .
  995. "\r\n\r\n" .
  996. '--' . $boundary . "\r\n" .
  997. 'Content-Type: text/html; charset=iso-8859-1' . "\r\n" .
  998. 'Content-Transfer-Encoding: quoted-printable' . "\r\n" .
  999. "\r\n" .
  1000. 'foo' .
  1001. "\r\n\r\n" .
  1002. '--' . $boundary . "\r\n" .
  1003. 'Content-Type: application/pdf; name=foo.pdf' . "\r\n" .
  1004. 'Content-Transfer-Encoding: base64' . "\r\n" .
  1005. 'Content-Disposition: attachment; filename=foo.pdf' . "\r\n" .
  1006. "\r\n" .
  1007. base64_encode('<pdf data>') .
  1008. "\r\n\r\n" .
  1009. '--' . $boundary . '--' . "\r\n",
  1010. $message->toString()
  1011. );
  1012. }
  1013. public function testHtmlPartAlwaysAppearsLast()
  1014. {
  1015. $message = $this->_createMessage();
  1016. $message->setReturnPath('chris@w3style.co.uk');
  1017. $message->setSubject('just a test subject');
  1018. $message->setFrom(array(
  1019. 'chris.corbyn@swiftmailer.org' => 'Chris Corbyn'));
  1020. $id = $message->getId();
  1021. $date = date('r', $message->getDate());
  1022. $boundary = $message->getBoundary();
  1023. $part1 = $this->_createMimePart();
  1024. $part1->setContentType('text/html');
  1025. $part1->setBody('foo');
  1026. $part2 = $this->_createMimePart();
  1027. $part2->setContentType('text/plain');
  1028. $part2->setBody('bar');
  1029. $message->attach($part1);
  1030. $message->attach($part2);
  1031. $this->assertEqual(
  1032. 'Return-Path: <chris@w3style.co.uk>' . "\r\n" .
  1033. 'Message-ID: <' . $id . '>' . "\r\n" .
  1034. 'Date: ' . $date . "\r\n" .
  1035. 'Subject: just a test subject' . "\r\n" .
  1036. 'From: Chris Corbyn <chris.corbyn@swiftmailer.org>' . "\r\n" .
  1037. 'MIME-Version: 1.0' . "\r\n" .
  1038. 'Content-Type: multipart/alternative;' . "\r\n" .
  1039. ' boundary="' . $boundary . '"' . "\r\n" .
  1040. "\r\n\r\n" .
  1041. '--' . $boundary . "\r\n" .
  1042. 'Content-Type: text/plain' . "\r\n" .
  1043. 'Content-Transfer-Encoding: quoted-printable' . "\r\n" .
  1044. "\r\n" .
  1045. 'bar' .
  1046. "\r\n\r\n" .
  1047. '--' . $boundary . "\r\n" .
  1048. 'Content-Type: text/html' . "\r\n" .
  1049. 'Content-Transfer-Encoding: quoted-printable' . "\r\n" .
  1050. "\r\n" .
  1051. 'foo' .
  1052. "\r\n\r\n" .
  1053. '--' . $boundary . '--' . "\r\n",
  1054. $message->toString()
  1055. );
  1056. }
  1057. public function testBodyBecomesPartIfOtherPartsAttached()
  1058. {
  1059. $message = $this->_createMessage();
  1060. $message->setReturnPath('chris@w3style.co.uk');
  1061. $message->setSubject('just a test subject');
  1062. $message->setFrom(array(
  1063. 'chris.corbyn@swiftmailer.org' => 'Chris Corbyn'));
  1064. $message->setContentType('text/html');
  1065. $message->setBody('foo');
  1066. $id = $message->getId();
  1067. $date = date('r', $message->getDate());
  1068. $boundary = $message->getBoundary();
  1069. $part2 = $this->_createMimePart();
  1070. $part2->setContentType('text/plain');
  1071. $part2->setBody('bar');
  1072. $message->attach($part2);
  1073. $this->assertEqual(
  1074. 'Return-Path: <chris@w3style.co.uk>' . "\r\n" .
  1075. 'Message-ID: <' . $id . '>' . "\r\n" .
  1076. 'Date: ' . $date . "\r\n" .
  1077. 'Subject: just a test subject' . "\r\n" .
  1078. 'From: Chris Corbyn <chris.corbyn@swiftmailer.org>' . "\r\n" .
  1079. 'MIME-Version: 1.0' . "\r\n" .
  1080. 'Content-Type: multipart/alternative;' . "\r\n" .
  1081. ' boundary="' . $boundary . '"' . "\r\n" .
  1082. "\r\n\r\n" .
  1083. '--' . $boundary . "\r\n" .
  1084. 'Content-Type: text/plain' . "\r\n" .
  1085. 'Content-Transfer-Encoding: quoted-printable' . "\r\n" .
  1086. "\r\n" .
  1087. 'bar' .
  1088. "\r\n\r\n" .
  1089. '--' . $boundary . "\r\n" .
  1090. 'Content-Type: text/html' . "\r\n" .
  1091. 'Content-Transfer-Encoding: quoted-printable' . "\r\n" .
  1092. "\r\n" .
  1093. 'foo' .
  1094. "\r\n\r\n" .
  1095. '--' . $boundary . '--' . "\r\n",
  1096. $message->toString()
  1097. );
  1098. }
  1099. public function testBodyIsCanonicalized()
  1100. {
  1101. $message = $this->_createMessage();
  1102. $message->setReturnPath('chris@w3style.co.uk');
  1103. $message->setSubject('just a test subject');
  1104. $message->setFrom(array(
  1105. 'chris.corbyn@swiftmailer.org' => 'Chris Corbyn'));
  1106. $message->setBody(
  1107. 'just a test body' . "\n" .
  1108. 'with a new line'
  1109. );
  1110. $id = $message->getId();
  1111. $date = $message->getDate();
  1112. $this->assertEqual(
  1113. 'Return-Path: <chris@w3style.co.uk>' . "\r\n" .
  1114. 'Message-ID: <' . $id . '>' . "\r\n" .
  1115. 'Date: ' . date('r', $date) . "\r\n" .
  1116. 'Subject: just a test subject' . "\r\n" .
  1117. 'From: Chris Corbyn <chris.corbyn@swiftmailer.org>' . "\r\n" .
  1118. 'MIME-Version: 1.0' . "\r\n" .
  1119. 'Content-Type: text/plain' . "\r\n" .
  1120. 'Content-Transfer-Encoding: quoted-printable' . "\r\n" .
  1121. "\r\n" .
  1122. 'just a test body' . "\r\n" .
  1123. 'with a new line',
  1124. $message->toString()
  1125. );
  1126. }
  1127. // -- Private helpers
  1128. protected function _createMessage()
  1129. {
  1130. return new Swift_Message();
  1131. }
  1132. protected function _createMimePart()
  1133. {
  1134. return new Swift_MimePart();
  1135. }
  1136. protected function _createAttachment()
  1137. {
  1138. return new Swift_Attachment();
  1139. }
  1140. protected function _createEmbeddedFile()
  1141. {
  1142. return new Swift_EmbeddedFile();
  1143. }
  1144. }