Advertisement
Guest User

Untitled

a guest
Sep 1st, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. <?php
  2.  
  3. class Email_reader {
  4.  
  5. // imap server connection
  6. private $conn;
  7.  
  8. // inbox storage and inbox message count
  9. public $inbox;
  10. public $msg_cnt;
  11.  
  12. // email login credentials
  13. private $server;
  14. private $user;
  15. private $pass;
  16. private $port;
  17.  
  18. // connect to the server and get the inbox emails
  19. function __construct( $user, $pass, $server = '127.0.0.1', $port = 25 ) {
  20.  
  21. $this -> server = $server;
  22. $this -> port = $port;
  23. $this -> pass = $pass;
  24. $this -> user = $user;
  25.  
  26. $this->connect();
  27. $this->inbox();
  28. }
  29.  
  30. // close the server connection
  31. function close() {
  32. $this->inbox = array();
  33. $this->msg_cnt = 0;
  34.  
  35. $error = imap_errors();
  36. imap_alerts();
  37. if (count($error) > 1 || $error[0] != 'SECURITY PROBLEM: insecure server advertised AUTH=PLAIN') {
  38. // More than 1 error or not the expected error
  39. var_dump($error);
  40. throw new Exception('IMAP error detected');
  41. }
  42.  
  43. imap_close($this->conn);
  44. }
  45.  
  46. // open the server connection
  47. // the imap_open function parameters will need to be changed for the particular server
  48. // these are laid out to connect to a Dreamhost IMAP server
  49. function connect() {
  50. $this->conn = @imap_open('{'.$this->server.'/notls}', $this->user, $this->pass);
  51. }
  52.  
  53. // move the message to a new folder
  54. function move($msg_index, $folder='INBOX.Processed') {
  55. // move on server
  56. imap_mail_move($this->conn, $msg_index, $folder);
  57. imap_expunge($this->conn);
  58.  
  59. // re-read the inbox
  60. $this->inbox();
  61. }
  62.  
  63. // get a specific message (1 = first email, 2 = second email, etc.)
  64. function get($msg_index=NULL) {
  65. if (count($this->inbox) <= 0) {
  66. return array();
  67. }
  68. elseif ( ! is_null($msg_index) && isset($this->inbox[$msg_index])) {
  69. return $this->inbox[$msg_index];
  70. }
  71.  
  72. return $this->inbox[0];
  73. }
  74.  
  75. // read the inbox
  76. function inbox() {
  77. $this->msg_cnt = imap_num_msg($this->conn);
  78.  
  79. $in = array();
  80. for($i = 1; $i <= $this->msg_cnt; $i++) {
  81. $in[] = array(
  82. 'index' => $i,
  83. 'header' => imap_headerinfo($this->conn, $i),
  84. 'body' => imap_body($this->conn, $i),
  85. 'structure' => imap_fetchstructure($this->conn, $i)
  86. );
  87. }
  88.  
  89. $this->inbox = $in;
  90. }
  91.  
  92.  
  93. function get_attachments( $email ) {
  94.  
  95. $attachments = array();
  96. // check for attachments
  97. if ( isset( $email['structure']->parts ) && count( $email['structure']->parts ) ) {
  98. // loop through all attachments
  99. for ($i = 0; $i < count($email['structure']->parts); $i++) {
  100. // set up an empty attachment
  101. $attachments[$i] = array(
  102. 'is_attachment' => FALSE,
  103. 'filename' => '',
  104. 'name' => '',
  105. 'attachment' => ''
  106. );
  107.  
  108. // if this attachment has idfparameters, then proceed
  109. if ($email['structure']->parts[$i]->ifdparameters) {
  110. foreach ($email['structure']->parts[$i]->dparameters as $object) {
  111. // if this attachment is a file, mark the attachment and filename
  112. if (strtolower($object->attribute) == 'filename') {
  113. $attachments[$i]['is_attachment'] = TRUE;
  114. $attachments[$i]['filename'] = $object->value;
  115. }
  116. }
  117. }
  118.  
  119. // if this attachment has ifparameters, then proceed as above
  120. if ($email['structure']->parts[$i]->ifparameters) {
  121. foreach ($email['structure']->parts[$i]->parameters as $object) {
  122. if (strtolower($object->attribute) == 'name') {
  123. $attachments[$i]['is_attachment'] = TRUE;
  124. $attachments[$i]['name'] = $object->value;
  125. }
  126. }
  127. }
  128.  
  129. // if we found a valid attachment for this 'part' of the email, process the attachment
  130. if ($attachments[$i]['is_attachment']) {
  131. // get the content of the attachment
  132. $attachments[$i]['attachment'] = imap_fetchbody($this->conn, $email['index'], $i+1);
  133.  
  134. // check if this is base64 encoding
  135. if ($email['structure']->parts[$i]->encoding == 3) { // 3 = BASE64
  136. $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
  137. }
  138. // otherwise, check if this is "quoted-printable" format
  139. elseif ($email['structure']->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE
  140. $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
  141. }
  142. }
  143.  
  144. if( empty( implode( '', $attachments[$i] ) ) ){
  145. unset( $attachments[$i] );
  146. }
  147. }
  148. }
  149.  
  150. if( !empty( $attachments ) ){
  151. return $attachments;
  152. }
  153.  
  154. return false;
  155. }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement