Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function get_html_body(){
- $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
- $username = $this->config->item('competitor_email_sync')['email'];
- $password = $this->config->item('competitor_email_sync')['password'];
- $inbox = imap_open($hostname,$username,$password);
- if(!$inbox) {
- echo 'connot connec to email server'.PHP_EOL.imap_last_error();
- die;
- }
- $emails = imap_search($inbox,'UNFLAGGED BODY "'.$keyword.'"');
- if($emails) {
- foreach ($emails as $email_number) {
- list($email_from, $subject) = $this->handle_overview($inbox, $email_number);
- if(!isset($configs[$email_from])){
- echo 'not configged email sender: '.$email_from.PHP_EOL;
- continue;
- }
- if (strpos($subject, 'Fwd') === false) {
- echo 'wrong subject:'. $subject. PHP_EOL;
- //TODO determine the filter critiria
- continue;
- }
- $html_body = $this->get_html($inbox, $email_number);
- $b64_test = base64_decode($html_body);
- if($b64_test){
- $html_body = $b64_test;
- }
- }
- }else{
- echo PHP_EOL.' no email found.'.PHP_EOL;
- }
- /* close the connection */
- imap_close($inbox);
- }
- function get_html($inbox, $email_number){
- $message = imap_fetchbody($inbox,$email_number,'2');
- $html_body = quoted_printable_decode($message);
- $html_body = mb_convert_encoding($html_body, 'utf-8', mb_detect_encoding($html_body));
- $html_body = mb_convert_encoding($html_body, 'html-entities', 'utf-8');
- if(!$html_body){
- $message = imap_fetchbody($inbox,$email_number,2);
- $html_body = quoted_printable_decode($message);
- $html_body = mb_convert_encoding($html_body, 'utf-8', mb_detect_encoding($html_body));
- $html_body = mb_convert_encoding($html_body, 'html-entities', 'utf-8');
- }
- return $html_body;
- }
- function handle_overview($inbox, $email_number){
- $overview = imap_fetch_overview($inbox, $email_number);
- $email_from = $overview[0]->from;
- $email_from = explode('<', $email_from)[1];
- $email_from = strtolower(trim(str_replace('>', '', $email_from)));
- $subject = imap_utf8($overview[0]->subject);
- return [$email_from, $subject];
- }
Add Comment
Please, Sign In to add comment