Guest User

Untitled

a guest
Nov 2nd, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. function get_html_body(){
  2. $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
  3. $username = $this->config->item('competitor_email_sync')['email'];
  4. $password = $this->config->item('competitor_email_sync')['password'];
  5.  
  6. $inbox = imap_open($hostname,$username,$password);
  7. if(!$inbox) {
  8. echo 'connot connec to email server'.PHP_EOL.imap_last_error();
  9. die;
  10. }
  11.  
  12. $emails = imap_search($inbox,'UNFLAGGED BODY "'.$keyword.'"');
  13.  
  14. if($emails) {
  15. foreach ($emails as $email_number) {
  16. list($email_from, $subject) = $this->handle_overview($inbox, $email_number);
  17.  
  18. if(!isset($configs[$email_from])){
  19. echo 'not configged email sender: '.$email_from.PHP_EOL;
  20. continue;
  21. }
  22. if (strpos($subject, 'Fwd') === false) {
  23. echo 'wrong subject:'. $subject. PHP_EOL;
  24. //TODO determine the filter critiria
  25. continue;
  26. }
  27.  
  28. $html_body = $this->get_html($inbox, $email_number);
  29. $b64_test = base64_decode($html_body);
  30. if($b64_test){
  31. $html_body = $b64_test;
  32. }
  33. }
  34. }else{
  35. echo PHP_EOL.' no email found.'.PHP_EOL;
  36. }
  37.  
  38. /* close the connection */
  39. imap_close($inbox);
  40.  
  41. }
  42.  
  43. function get_html($inbox, $email_number){
  44. $message = imap_fetchbody($inbox,$email_number,'2');
  45. $html_body = quoted_printable_decode($message);
  46. $html_body = mb_convert_encoding($html_body, 'utf-8', mb_detect_encoding($html_body));
  47. $html_body = mb_convert_encoding($html_body, 'html-entities', 'utf-8');
  48.  
  49. if(!$html_body){
  50. $message = imap_fetchbody($inbox,$email_number,2);
  51. $html_body = quoted_printable_decode($message);
  52. $html_body = mb_convert_encoding($html_body, 'utf-8', mb_detect_encoding($html_body));
  53. $html_body = mb_convert_encoding($html_body, 'html-entities', 'utf-8');
  54. }
  55.  
  56. return $html_body;
  57. }
  58. function handle_overview($inbox, $email_number){
  59. $overview = imap_fetch_overview($inbox, $email_number);
  60. $email_from = $overview[0]->from;
  61. $email_from = explode('<', $email_from)[1];
  62. $email_from = strtolower(trim(str_replace('>', '', $email_from)));
  63.  
  64. $subject = imap_utf8($overview[0]->subject);
  65. return [$email_from, $subject];
  66. }
Add Comment
Please, Sign In to add comment