Advertisement
Guest User

Untitled

a guest
Dec 14th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. <?php
  2. error_reporting(1);
  3.  
  4. class Email_reader
  5. {
  6. // imap server connection
  7. public $conn;
  8.  
  9. // inbox storage and inbox message count
  10. private $inbox;
  11. private $msg_cnt;
  12.  
  13. // email login credentials
  14. private $server = 'SERVER_NAME';
  15. private $user = 'USER_NAME';
  16. private $pass = 'PASSWORD';
  17.  
  18. private $port = 110; // adjust according to server settings
  19.  
  20. // connect to the server and get the inbox emails
  21. function __construct()
  22. {
  23. $this->connect();
  24. // $this->inbox();
  25. }
  26.  
  27. // close the server connection
  28. function close()
  29. {
  30. $this->inbox = array();
  31. $this->msg_cnt = 0;
  32.  
  33. imap_close($this->conn);
  34. }
  35.  
  36. // open the server connection
  37. // the imap_open function parameters will need to be changed for the particular server
  38. // these are laid out to connect to a Dreamhost IMAP server
  39. function connect()
  40. {
  41. $this->conn = imap_open('{' . $this->server . '/notls}', $this->user, $this->pass);
  42. }
  43.  
  44. // move the message to a new folder
  45. function move($msg_index, $folder = 'INBOX.Processed')
  46. {
  47. // move on server
  48. imap_mail_move($this->conn, $msg_index, $folder);
  49. imap_expunge($this->conn);
  50.  
  51. // re-read the inbox
  52. $this->inbox();
  53. }
  54.  
  55. // get a specific message (1 = first email, 2 = second email, etc.)
  56. function get($msg_index = NULL)
  57. {
  58. if (count($this->inbox) <= 0) {
  59. return array();
  60. } elseif (!is_null($msg_index) && isset($this->inbox[$msg_index])) {
  61. return $this->inbox[$msg_index];
  62. }
  63.  
  64. return $this->inbox[0];
  65. }
  66.  
  67. // read the inbox
  68. function inbox()
  69. {
  70. $this->msg_cnt = imap_num_msg($this->conn);
  71.  
  72. $in = array();
  73.  
  74. // for ($i = $this->msg_cnt; $i > 0; $i--) { // Get messages in reverse order
  75. for ($i = 0; $i <= $this->msg_cnt; $i++) {
  76. $in[] = array(
  77. 'index' => $i,
  78. 'header' => imap_headerinfo($this->conn, $i),
  79. 'body' => imap_fetchbody($this->conn, $i,"1"),
  80. 'structure' => imap_fetchstructure($this->conn, $i)
  81. );
  82.  
  83. /*
  84. * Testing Purpose Only: Below code will limit the mails to given counter
  85. */
  86. // if($i == $this->msg_cnt){
  87. // break;
  88. // }
  89.  
  90. }
  91.  
  92. $this->inbox = $in;
  93.  
  94. return $in;
  95. }
  96.  
  97. function extract_msg_without_quoted_reply($mail_content){
  98. // Stripping Out Quoted Text
  99. $mail_content = preg_replace('/.*((^>+\s{1}.*$)+\n?)+/mi', '', $mail_content);
  100.  
  101. // Stripping Out Unwanted elements
  102. $mail_content = str_replace('>:','',$mail_content);
  103. $mail_content = str_replace('>','',$mail_content);
  104. $mail_content = str_replace('<','',$mail_content);
  105. $mail_content = str_replace('wrote:','',$mail_content);
  106. $mail_content = trim($mail_content);
  107. $mail_content = str_replace("'", "\\'", $mail_content);
  108.  
  109. return $mail_content;
  110. }
  111. }
  112.  
  113. $obj = new Email_reader;
  114. echo "<pre>",print_r($obj->inbox()),"</pre>";die;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement