mail.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * @group pluggable
  4. * @group mail
  5. */
  6. class Tests_Mail extends WP_UnitTestCase {
  7. function setUp() {
  8. parent::setUp();
  9. $_SERVER['SERVER_NAME'] = 'example.com';
  10. unset( $GLOBALS['phpmailer']->mock_sent );
  11. }
  12. function tearDown() {
  13. parent::tearDown();
  14. unset( $_SERVER['SERVER_NAME'] );
  15. }
  16. function test_wp_mail_custom_boundaries() {
  17. $to = 'user@example.com';
  18. $subject = 'Test email with custom boundaries';
  19. $headers = '' . "\n";
  20. $headers .= 'MIME-Version: 1.0' . "\n";
  21. $headers .= 'Content-Type: multipart/mixed; boundary="----=_Part_4892_25692638.1192452070893"' . "\n";
  22. $headers .= "\n";
  23. $body = "\n";
  24. $body .= '------=_Part_4892_25692638.1192452070893' . "\n";
  25. $body .= 'Content-Type: text/plain; charset=ISO-8859-1' . "\n";
  26. $body .= 'Content-Transfer-Encoding: 7bit' . "\n";
  27. $body .= 'Content-Disposition: inline' . "\n";
  28. $body .= "\n";
  29. $body .= 'Here is a message with an attachment of a binary file.' . "\n";
  30. $body .= "\n";
  31. $body .= '------=_Part_4892_25692638.1192452070893' . "\n";
  32. $body .= 'Content-Type: image/x-icon; name=favicon.ico' . "\n";
  33. $body .= 'Content-Transfer-Encoding: base64' . "\n";
  34. $body .= 'Content-Disposition: attachment; filename=favicon.ico' . "\n";
  35. $body .= "\n";
  36. $body .= 'AAABAAEAEBAAAAAAAABoBQAAFgAAACgAAAAQAAAAIAAAAAEACAAAAAAAAAAAAAAAAAAAAAAAAAAA' . "\n";
  37. $body .= 'AAAAAAAAAAAAAACAAACAAAAAgIAAgAAAAIAAgACAgAAAwMDAAICAgAAAAP8AAP8AAAD//wD/AAAA' . "\n";
  38. $body .= '/wD/AP//AAD///8A//3/AP39/wD6/f8A+P3/AP/8/wD9/P8A+vz/AP/7/wD/+v8A/vr/APz6/wD4' . "\n";
  39. $body .= '+v8A+/n/APP5/wD/+P8A+vj/AO/4/wDm+P8A2fj/AP/3/wD/9v8A9vb/AP/1/wD69f8A9PT/AO30' . "\n";
  40. $body .= '/wD/8/8A//L/APnx/wD28P8A///+APj//gD2//4A9P/+AOP//gD//f4A6f/9AP///AD2//wA8//8' . "\n";
  41. $body .= 'APf9/AD///sA/v/7AOD/+wD/+vsA9/X7APr/+gDv/voA///5AP/9+QD/+/kA+e35AP//+ADm//gA' . "\n";
  42. $body .= '4f/4AP/9+AD0+/gA///3APv/9wDz//cA8f/3AO3/9wD/8fcA//32AP369gDr+vYA8f/1AOv/9QD/' . "\n";
  43. $body .= '+/UA///0APP/9ADq//QA///zAP/18wD///IA/fzyAP//8QD///AA9//wAPjw8AD//+8A8//vAP//' . "\n";
  44. $body .= '7gD9/+4A9v/uAP/u7gD//+0A9v/tAP7/6wD/+eoA///pAP//6AD2/+gA//nnAP/45wD38eYA/fbl' . "\n";
  45. $body .= 'AP/25AD29uQA7N/hAPzm4AD/690AEhjdAAAa3AAaJdsA//LXAC8g1gANH9YA+dnTAP/n0gDh5dIA' . "\n";
  46. $body .= 'DyjSABkk0gAdH9EABxDRAP/l0AAAJs4AGRTOAPPczQAAKs0AIi7MAA4UywD56soA8tPKANTSygD/' . "\n";
  47. $body .= '18kA6NLHAAAjxwDj28QA/s7CAP/1wQDw3r8A/9e8APrSrwDCtqoAzamjANmPiQDQj4YA35mBAOme' . "\n";
  48. $body .= 'fgDHj3wA1qR6AO+sbwDpmm8A2IVlAKmEYgCvaFoAvHNXAEq2VgA5s1UAPbhQAFWtTwBStU0ARbNN' . "\n";
  49. $body .= 'AEGxTQA7tEwAObZIAEq5RwDKdEYAULhDANtuQgBEtTwA1ls3ALhgMQCxNzEA2FsvAEC3LQB0MCkA' . "\n";
  50. $body .= 'iyYoANZTJwDLWyYAtjMlALE6JACZNSMAuW4iANlgIgDoWCEAylwgAMUuIAD3Vh8A52gdALRCHQCx' . "\n";
  51. $body .= 'WhwAsEkcALU4HACMOBwA0V4bAMYyGgCPJRoA218ZAJM7FwC/PxYA0msVAM9jFQD2XBUAqioVAIAf' . "\n";
  52. $body .= 'FQDhYRQAujMTAMUxEwCgLBMAnxIPAMsqDgCkFgsA6GMHALE2BAC9JQAAliIAAFYTAAAAAAAAAAAA' . "\n";
  53. $body .= 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' . "\n";
  54. $body .= 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD/' . "\n";
  55. $body .= '//8AsbGxsbGxsbGxsbGxsbGxd7IrMg8PDw8PDw8PUBQeJXjQYE9PcKPM2NfP2sWhcg+BzTE7dLjb' . "\n";
  56. $body .= 'mG03YWaV4JYye8MPbsLZlEouKRRCg9SXMoW/U53enGRAFzCRtNO7mTiAyliw30gRTg9VbJCKfYs0' . "\n";
  57. $body .= 'j9VmuscfLTFbIy8SOhA0Inq5Y77GNBMYIxQUJzM2Vxx2wEmfyCYWMRldXCg5MU0aicRUms58SUVe' . "\n";
  58. $body .= 'RkwjPBRSNIfBMkSgvWkyPxVHFIaMSx1/0S9nkq7WdWo1a43Jt2UqgtJERGJ5m6K8y92znpNWIYS1' . "\n";
  59. $body .= 'UQ89Mmg5cXNaX0EkGyyI3KSsp6mvpaqosaatq7axsQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' . "\n";
  60. $body .= 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=' . "\n";
  61. $body .= '------=_Part_4892_25692638.1192452070893--' . "\n";
  62. $body .= "\n";
  63. wp_mail($to, $subject, $body, $headers);
  64. // We need some better assertions here but these catch the failure for now.
  65. $this->assertEquals($body, $GLOBALS['phpmailer']->mock_sent[0]['body']);
  66. $this->assertTrue(strpos($GLOBALS['phpmailer']->mock_sent[0]['header'], 'boundary="----=_Part_4892_25692638.1192452070893"') > 0);
  67. $this->assertTrue(strpos($GLOBALS['phpmailer']->mock_sent[0]['header'], 'charset=') > 0);
  68. }
  69. /**
  70. * @ticket 15448
  71. */
  72. function test_wp_mail_plain_and_html() {
  73. $to = 'user@example.com';
  74. $subject = 'Test email with plain text and html versions';
  75. $messages = array( 'text/plain' => 'Here is some plain text.',
  76. 'text/html' =>'<html><head></head><body>Here is the HTML ;-)<body></html>' );
  77. wp_mail( $to, $subject, $messages );
  78. preg_match( '/boundary="(.*)"/', $GLOBALS['phpmailer']->mock_sent[0]['header'], $matches);
  79. $boundry = $matches[1];
  80. $body = '--' . $boundry . '
  81. Content-Type: text/plain; charset = "UTF-8"
  82. Content-Transfer-Encoding: 8bit
  83. Here is some plain text.
  84. --' . $boundry . '
  85. Content-Type: text/html; charset = "UTF-8"
  86. Content-Transfer-Encoding: 8bit
  87. <html><head></head><body>Here is the HTML ;-)<body></html>
  88. --' . $boundry . '--
  89. ';
  90. // We need some better assertions here but these test the behaviour for now.
  91. $this->assertEquals($body, $GLOBALS['phpmailer']->mock_sent[0]['body']);
  92. }
  93. /**
  94. * @ticket 17305
  95. */
  96. function test_wp_mail_rfc2822_addresses() {
  97. $to = "Name <address@tld.com>";
  98. $from = "Another Name <another_address@different-tld.com>";
  99. $cc = "The Carbon Guy <cc@cc.com>";
  100. $bcc = "The Blind Carbon Guy <bcc@bcc.com>";
  101. $subject = "RFC2822 Testing";
  102. $message = "My RFC822 Test Message";
  103. $headers[] = "From: {$from}";
  104. $headers[] = "CC: {$cc}";
  105. $headers[] = "BCC: {$bcc}";
  106. wp_mail( $to, $subject, $message, $headers );
  107. // WordPress 3.2 and later correctly split the address into the two parts and send them seperately to PHPMailer
  108. // Earlier versions of PHPMailer were not touchy about the formatting of these arguments.
  109. $this->assertEquals('address@tld.com', $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0]);
  110. $this->assertEquals('Name', $GLOBALS['phpmailer']->mock_sent[0]['to'][0][1]);
  111. $this->assertEquals('cc@cc.com', $GLOBALS['phpmailer']->mock_sent[0]['cc'][0][0]);
  112. $this->assertEquals('The Carbon Guy', $GLOBALS['phpmailer']->mock_sent[0]['cc'][0][1]);
  113. $this->assertEquals('bcc@bcc.com', $GLOBALS['phpmailer']->mock_sent[0]['bcc'][0][0]);
  114. $this->assertEquals('The Blind Carbon Guy', $GLOBALS['phpmailer']->mock_sent[0]['bcc'][0][1]);
  115. $this->assertEquals($message . "\n", $GLOBALS['phpmailer']->mock_sent[0]['body']);
  116. }
  117. /**
  118. * @ticket 17305
  119. */
  120. function test_wp_mail_multiple_rfc2822_to_addresses() {
  121. $to = "Name <address@tld.com>, Another Name <another_address@different-tld.com>";
  122. $subject = "RFC2822 Testing";
  123. $message = "My RFC822 Test Message";
  124. wp_mail( $to, $subject, $message );
  125. // WordPress 3.2 and later correctly split the address into the two parts and send them seperately to PHPMailer
  126. // Earlier versions of PHPMailer were not touchy about the formatting of these arguments.
  127. $this->assertEquals('address@tld.com', $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0]);
  128. $this->assertEquals('Name', $GLOBALS['phpmailer']->mock_sent[0]['to'][0][1]);
  129. $this->assertEquals('another_address@different-tld.com', $GLOBALS['phpmailer']->mock_sent[0]['to'][1][0]);
  130. $this->assertEquals('Another Name', $GLOBALS['phpmailer']->mock_sent[0]['to'][1][1]);
  131. $this->assertEquals($message . "\n", $GLOBALS['phpmailer']->mock_sent[0]['body']);
  132. }
  133. function test_wp_mail_multiple_to_addresses() {
  134. $to = "address@tld.com, another_address@different-tld.com";
  135. $subject = "RFC2822 Testing";
  136. $message = "My RFC822 Test Message";
  137. wp_mail( $to, $subject, $message );
  138. $this->assertEquals('address@tld.com', $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0]);
  139. $this->assertEquals('another_address@different-tld.com', $GLOBALS['phpmailer']->mock_sent[0]['to'][1][0]);
  140. $this->assertEquals($message . "\n", $GLOBALS['phpmailer']->mock_sent[0]['body']);
  141. }
  142. /**
  143. * @ticket 18463
  144. */
  145. function test_wp_mail_to_address_no_name() {
  146. $to = "<address@tld.com>";
  147. $subject = "RFC2822 Testing";
  148. $message = "My RFC822 Test Message";
  149. wp_mail( $to, $subject, $message );
  150. $this->assertEquals('address@tld.com', $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0]);
  151. $this->assertEquals($message . "\n", $GLOBALS['phpmailer']->mock_sent[0]['body']);
  152. }
  153. /**
  154. * @ticket 23642
  155. */
  156. function test_wp_mail_return_value() {
  157. // No errors
  158. $this->assertTrue( wp_mail( 'valid@address.com', 'subject', 'body' ) );
  159. // Non-fatal errors
  160. $this->assertTrue( wp_mail( 'valid@address.com', 'subject', 'body', "Cc: invalid-address\nBcc: @invalid.address", ABSPATH . '/non-existant-file.html' ) );
  161. // Fatal errors
  162. $this->assertFalse( wp_mail( 'invalid.address', 'subject', 'body', '', array() ) );
  163. }
  164. }