mail_include.inc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. // Change these to make tests run successfully
  3. $server = '{localhost}';
  4. $default_mailbox = $server . "INBOX";
  5. $domain = "example.com";
  6. $admin_user = "webmaster"; // a user with admin access
  7. $username = "$admin_user@$domain";
  8. $password = 'p4ssw0rd';
  9. $users = array("webmaster", "info", "admin", "foo"); // tests require 4 valid userids
  10. $mailbox_prefix = "phpttest"; // name used for test mailbox
  11. /**
  12. * Create a test mailbox and populate with msgs
  13. *
  14. * @para, string mailbox_suffix Suffix used to uniquely identify mailboxes
  15. * @param int message_count number of test msgs to be written to new mailbox
  16. *
  17. * @return IMAP stream to new mailbox on success; FALSE on failure
  18. */
  19. function setup_test_mailbox($mailbox_suffix, $message_count, &$new_mailbox = null, $msg_type = "simple"){
  20. global $server, $default_mailbox, $username, $password;
  21. // open a stream to default mailbox
  22. $imap_stream = imap_open($default_mailbox, $username, $password);
  23. if ($imap_stream === false) {
  24. echo "Cannot connect to IMAP server $server: " . imap_last_error() . "\n";
  25. return false;
  26. }
  27. echo "Create a temporary mailbox and add " . $message_count . " msgs\n";
  28. $new_mailbox = create_mailbox($imap_stream, $mailbox_suffix, $message_count, $msg_type);
  29. if ($new_mailbox === false) {
  30. echo "Can't create a temporary mailbox: " . imap_last_error(). "\n";
  31. return false;
  32. }
  33. echo ".. mailbox '$new_mailbox' created\n";
  34. // reopen stream to new mailbox
  35. if (imap_reopen($imap_stream, $new_mailbox) === false) {
  36. echo "can't re-open '$new_mailbox' mailbox: " . imap_last_error() . "\n";
  37. return false;
  38. }
  39. return $imap_stream;
  40. }
  41. /**
  42. * Create mailbox and fill with generic emails
  43. *
  44. * @param resource $imap_stream
  45. * @param string $mailbox
  46. */
  47. function create_mailbox($imap_stream, $mailbox_suffix, $message_count, $msg_type= "simple"){
  48. global $default_mailbox, $mailbox_prefix;
  49. $mailbox = $default_mailbox . "." . $mailbox_prefix . $mailbox_suffix;
  50. $mailboxes = imap_getmailboxes($imap_stream, $mailbox, '*');
  51. // check mailbox does not already exist
  52. if ($mailboxes) {
  53. foreach($mailboxes as $value) {
  54. if ($value->name == $mailbox) {
  55. exit ("TEST FAILED : Mailbox '$mailbox' already exists\n");
  56. }
  57. }
  58. }
  59. if (imap_createmailbox($imap_stream, $mailbox) === false) {
  60. return false;
  61. }
  62. // Add number of test msgs requested
  63. if ($message_count > 0) {
  64. populate_mailbox($imap_stream, $mailbox, $message_count, $msg_type);
  65. }
  66. return $mailbox;
  67. }
  68. /**
  69. * Populate a mailbox with generic emails
  70. *
  71. * @param resource $imap_stream
  72. * @param string $mailbox
  73. */
  74. function populate_mailbox($imap_stream, $mailbox, $message_count, $msg_type = "simple"){
  75. global $users, $domain;
  76. for($i = 1; $i <= $message_count; $i++) {
  77. if ($msg_type == "simple") {
  78. $msg = "From: foo@anywhere.com\r\n"
  79. . "To: $users[0]@$domain\r\n"
  80. . "Subject: test$i\r\n"
  81. . "\r\n"
  82. . "$i: this is a test message, please ignore\r\n";
  83. } else {
  84. $envelope["from"]= "foo@anywhere.com";
  85. $envelope["to"] = "$users[0]@$domain";
  86. $envelope["subject"] = "Test msg $i";
  87. $part1["type"] = TYPEMULTIPART;
  88. $part1["subtype"] = "mixed";
  89. $part2["type"] = TYPETEXT;
  90. $part2["subtype"] = "plain";
  91. $part2["description"] = "imap_mail_compose() function";
  92. $part2["contents.data"] = "message 1:xxxxxxxxxxxxxxxxxxxxxxxxxx";
  93. $part3["type"] = TYPETEXT;
  94. $part3["subtype"] = "plain";
  95. $part3["description"] = "Example";
  96. $part3["contents.data"] = "message 2:yyyyyyyyyyyyyyyyyyyyyyyyyy";
  97. $part4["type"] = TYPETEXT;
  98. $part4["subtype"] = "plain";
  99. $part4["description"] = "Return Values";
  100. $part4["contents.data"] = "message 3:zzzzzzzzzzzzzzzzzzzzzzzzzz";
  101. $body[1] = $part1;
  102. $body[2] = $part2;
  103. $body[3] = $part3;
  104. $body[4] = $part4;
  105. $msg = imap_mail_compose($envelope, $body);
  106. }
  107. imap_append($imap_stream, $mailbox, $msg);
  108. }
  109. }
  110. /**
  111. * Get the mailbox name from a mailbox description, i.e strip off server details.
  112. *
  113. * @param string mailbox complete mailbox name
  114. * @return mailbox name
  115. */
  116. function get_mailbox_name($mailbox){
  117. if (preg_match('/\{.*?\}(.*)/', $mailbox, $match) != 1) {
  118. echo "Unrecpognized mailbox name\n";
  119. return false;
  120. }
  121. return $match[1];
  122. }
  123. ?>