Part of the EllisLab Network
This thread is a discussion for the wiki article: imap pop class
   
2 of 3
2
imap pop class
Posted: 14 June 2009 08:11 PM   [ Ignore ]   [ # 16 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1158
Joined  08-06-2006

how about other POP servers? can you connect to anything else from the remote host? even a non SSL POP?

 Signature 

imap_pop get email | site_migrate port sites | OOCalendar | PhotoBox2 gallery | CI/EE 2 word_limiter, yep, wrote it

Profile
 
 
Posted: 15 June 2009 03:34 AM   [ Ignore ]   [ # 17 ]  
Grad Student
Avatar
Rank
Total Posts:  31
Joined  07-14-2007

After some serious testing ... I found out the server will only do an imap_open() to localhost. All other remote hosts will timeout. ssl or not. :(

Some googling then took me to this thread on my host’s forum…

http://www.bluehostforum.com/showthread.php?t=11548&highlight=imap+php

... which left me even more frustrated. :(
Guess there is not much I can do… other than nagging them to change the server config. :(

 Signature 

feedbleed.com - Saving music… one download at a time.
mmmmail.com - Disposable Email to RSS service.

Profile
 
 
Posted: 15 June 2009 03:25 PM   [ Ignore ]   [ # 18 ]  
Grad Student
Avatar
Rank
Total Posts:  31
Joined  07-14-2007

Hi sophistry

I managed to find a workaround for getting email through to your class… which led me to some other issues I hope you can shed some light on.

I am trying to store the retrieved email in mysql. I am doing this not from gmail but from a regular pop3 mailbox.

What’s the most effective way of deleting the messages from the pop3 mailbox after grabbing them? Right now I am doing the following…

$ems = $this->imap_pop->grab_emails_as_nested_array(); // array of email messages
foreach($ems as $em){ // delete all retrieved msgs from server using same connection
    
$this->imap_pop->delete_and_expunge($em["strings"]['temp_msg_id']);
}
$this
->imap_pop->close();

... and using the arrays to insert directly to the db much like it seems your approach was.

Does your class do any sort of charset conversion or check (like converting all strings to utf-8 for instance)?. I am having encoding problems with correctly inserting the messages into mysql. Some characters get completely garbled or even lost (message subject/text can come with a multitude of chars/encodings). Can you give me a hand on how you handled this issue?

And finally… can long messages be truncated to avoid unnecessary long entries in the database?

Hope you can give me some tips.

Cheers

 Signature 

feedbleed.com - Saving music… one download at a time.
mmmmail.com - Disposable Email to RSS service.

Profile
 
 
Posted: 15 June 2009 04:05 PM   [ Ignore ]   [ # 19 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1158
Joined  08-06-2006

hi deadelvis…

i’ve moved the imap_pop class into a better organized set of libs so these mime decode functions are in the main class - you might have to change the method syntax to get it to work inyour set up.

anyway, this is what i use. i found some of this code on php.net and then adapted and expanded it until it works for a whole range of email strings. basically, i send everything through this. the main thing is that it just goes out into “HTML entity land” to acheive the goal of full UTF-8 decoding coverage. since you are using up-to-date PHP5 some of these problems addressed by this custom mime_decode code might be fixed. but, these work for me.

cheers.

/**
    * Decode a string, return string
    * Just concatenate any multiple decoded strings
    *
    * generally workarounds to the limitations
    * of the various encoding functions
    * some of these problems are fixed in PHP5
    *
    * the standard imap_mime_header_decode() function
    * doesn't decode UTF8 properly, so we have to
    * convert to HTML entities and then back again
    * to make it handle both.
    *
    * send it a MIME encoded header string
    */
    
function decode_mime($raw_header)
    
{
        $t_arr
= imap_mime_header_decode($raw_header);
        
// concatenate the multiple strings
        // check default charset is empty or linebreak etc...
        // if it is empty don't include it
        
$text = '';
        foreach (
$t_arr as $item)
        
{    
            
if ($item->charset === 'default')
            
{
                
if ( trim($item->text) == '' ) continue;
            
}
            $text
.= $item->text;
        
}
        
// supress 'Invalid multibyte sequence in argument' errors
        //$t = @htmlentities($text, ENT_QUOTES, 'UTF-8');
        
$t = htmlentities($text, ENT_QUOTES, 'UTF-8');
        
$decoded_text = $this->_html_entity_decode_expanded($t);
        return
$decoded_text;
    
}
    
    
/**
    * the standard html_entity_decode() function
    * doesn't decode enough chars, so use the expanded
    * translation table for entities
    */
    
function _html_entity_decode_expanded($str)
    
{
        
return str_replace($this->html_entity_strings, $this->html_entity_chrs, $str);
    
}
    
    
/**
    * return single-byte character codes from
    * HTML encoded entities
    * for optimization, build this just once
    * when creating this class
    * this is used by mime_decode for custom decoding
    *
    * the standard html_entity_decode() function
    * doesn't decode enough chars, so expand the
    * translation table for entities and then
    * str_replace() which mimics the decode
    * decode a wider range of entites than the standard PHP code
    * get the expanded translation table
    * then do string replace to change values
    */
    
function _build_custom_html_translation_table()
    
{
        
// from php.net comments for get_html_translation_table() function
        // It adds to the standard get_html_translation_table the codes of
        // the characters usually M$ Word replaces into typed text.
        // Otherwise those characters would never be displayed correctly
        // in html output
        // this also lets us take care of numerically encoded entities
        // by hand like ' for single-quote which seem to be left out
        
$trans = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);
        
$trans[chr(39)] = ''';    // Single Quotation Mark (numerical entity)
        
        
$trans[chr(130)] = '‚';    // Single Low-9 Quotation Mark
        
$trans[chr(131)] = 'ƒ';    // Latin Small Letter F With Hook
        
$trans[chr(132)] = '„';    // Double Low-9 Quotation Mark
        
$trans[chr(133)] = '…';    // Horizontal Ellipsis
        
$trans[chr(134)] = '†';    // Dagger
        
$trans[chr(135)] = '‡';    // Double Dagger
        
$trans[chr(136)] = 'ˆ';    // Modifier Letter Circumflex Accent
        
$trans[chr(137)] = '‰';    // Per Mille Sign
        
$trans[chr(138)] = 'Š';    // Latin Capital Letter S With Caron
        
$trans[chr(139)] = '‹';    // Single Left-Pointing Angle Quotation Mark
        
$trans[chr(140)] = 'Œ';    // Latin Capital Ligature OE
        
$trans[chr(145)] = '‘';    // Left Single Quotation Mark
        
$trans[chr(146)] = '’';    // Right Single Quotation Mark
        
$trans[chr(147)] = '“';    // Left Double Quotation Mark
        
$trans[chr(148)] = '”';    // Right Double Quotation Mark
        
$trans[chr(149)] = '•';    // Bullet
        
$trans[chr(150)] = '–';    // En Dash
        
$trans[chr(151)] = '—';    // Em Dash
        
$trans[chr(152)] = '˜';    // Small Tilde
        
$trans[chr(153)] = '™';    // Trade Mark Sign
        
$trans[chr(154)] = 'š';    // Latin Small Letter S With Caron
        
$trans[chr(155)] = '›';    // Single Right-Pointing Angle Quotation Mark
        
$trans[chr(156)] = 'œ';    // Latin Small Ligature OE
        
$trans[chr(159)] = 'Ÿ';    // Latin Capital Letter Y With Diaeresis
        
ksort($trans);
        
$this->html_entity_strings = array_values($trans);
        
$this->html_entity_chrs = array_keys($trans);
    
}
 Signature 

imap_pop get email | site_migrate port sites | OOCalendar | PhotoBox2 gallery | CI/EE 2 word_limiter, yep, wrote it

Profile
 
 
Posted: 15 June 2009 04:43 PM   [ Ignore ]   [ # 20 ]  
Grad Student
Avatar
Rank
Total Posts:  31
Joined  07-14-2007

Sweet… I’ll implement into imap_pop lib and give these a run.
Cheers

 Signature 

feedbleed.com - Saving music… one download at a time.
mmmmail.com - Disposable Email to RSS service.

Profile
 
 
Posted: 02 July 2009 09:16 AM   [ Ignore ]   [ # 21 ]  
Summer Student
Total Posts:  25
Joined  06-08-2008

can’t seem to get the wiki code sample to work with gmail…

can anybody send a code sample that can connect to gmail and yahoo?

 Signature 

one… take control of me… you’re messing with the enemy… said it’s two… it’s another trick… messing with my mind i wake up…

Profile
 
 
Posted: 02 July 2009 09:57 AM   [ Ignore ]   [ # 22 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1158
Joined  08-06-2006

post code and errors.

 Signature 

imap_pop get email | site_migrate port sites | OOCalendar | PhotoBox2 gallery | CI/EE 2 word_limiter, yep, wrote it

Profile
 
 
Posted: 05 July 2009 01:36 AM   [ Ignore ]   [ # 23 ]  
Summer Student
Total Posts:  25
Joined  06-08-2008

firstly… kudos to your library… lifesaver… it works on my other pop servers… i just can’t figure out the connection to gmail part…

this is my controller code… exactly from the wiki sample…

$this->load->library('imap_pop');
        
// settings for gmail, you must have
        // SSL support compiled into PHP
        // and turn on POP in gmail
        
$config['server']='pop.gmail.com:995';
        
$config['login']='fawefe@gmail.com';
        
$config['pass']='fawefe';
        
$config['service_flags'] = '/pop3/ssl/novalidate-cert';
        
$config['mailbox']='INBOX';
        
$connected = $this->imap_pop->connect_and_count($config);
        if(
$connected)
        
{
        $em
= $this->imap_pop->grab_email_as_array(1);
        
$this->imap_pop->close();
        
}
        print_r
($em);


and this is the error i get…

A PHP Error was encountered

Severity
: Notice

Message
: Undefined variable: em

Filename
: controllers/ihome.php

Line Number
: 77

A PHP Error was encountered

Severity
: Notice

Message
: Unknown: Can't connect to gmail-pop.l.google.com,995: Connection timed out (errflg=1)

Filename: Unknown

Line Number: 0

A PHP Error was encountered

Severity: Notice

Message: Unknown: Can'
t connect to gmail-pop.l.google.com,995: Connection timed out (errflg=2)

Filename: Unknown

Line Number
: 0
 Signature 

one… take control of me… you’re messing with the enemy… said it’s two… it’s another trick… messing with my mind i wake up…

Profile
 
 
Posted: 05 July 2009 08:35 AM   [ Ignore ]   [ # 24 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1158
Joined  08-06-2006

i’ve seen that error before when the ISP is blocking the port. can you verify that port 995 is open on the machine that is making the request to gmail?

 Signature 

imap_pop get email | site_migrate port sites | OOCalendar | PhotoBox2 gallery | CI/EE 2 word_limiter, yep, wrote it

Profile
 
 
Posted: 06 July 2009 08:26 AM   [ Ignore ]   [ # 25 ]  
Summer Student
Total Posts:  25
Joined  06-08-2008

hmmmm… the code is actually deployed to a shared hosting… i’m still waiting for my provider’s answer regarding open/blocked ports… i’ll update this thread as soon as i get it…

 Signature 

one… take control of me… you’re messing with the enemy… said it’s two… it’s another trick… messing with my mind i wake up…

Profile
 
 
Posted: 06 July 2009 12:38 PM   [ Ignore ]   [ # 26 ]  
Grad Student
Avatar
Rank
Total Posts:  31
Joined  07-14-2007

I had the same symptoms. Probably your ISP only allows imap_open() calls to localhost… to avoid spammers using that method to send spam.

If you have shell access you can easily try if the port is blocked by doing a “telnet localhost 995” and “telnet pop.gmail.com 995” and see if there’s a connect with any of them.

I managed to workaround this situation by forwarding all mail to an account on localhost, and then retrieve it from localhost with this lib.

Good luck.

 Signature 

feedbleed.com - Saving music… one download at a time.
mmmmail.com - Disposable Email to RSS service.

Profile
 
 
Posted: 07 July 2009 08:07 AM   [ Ignore ]   [ # 27 ]  
Summer Student
Total Posts:  25
Joined  06-08-2008

@sophistry - my host confirmed that port 995 is only open for incoming tcp connections… any ideas how to work around this?

@deadelvis - it will probably be my last resort… but for now i still have time to explore other options… any ideas?

thanks for the replies guys… i appreciate it very much… salamat(thanks in tagalog)!

 Signature 

one… take control of me… you’re messing with the enemy… said it’s two… it’s another trick… messing with my mind i wake up…

Profile
 
 
Posted: 07 July 2009 09:09 AM   [ Ignore ]   [ # 28 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1158
Joined  08-06-2006

what about IMAP port 993? is that open? you can use the imap_pop class over IMAP too.

 Signature 

imap_pop get email | site_migrate port sites | OOCalendar | PhotoBox2 gallery | CI/EE 2 word_limiter, yep, wrote it

Profile
 
 
Posted: 07 September 2009 08:14 PM   [ Ignore ]   [ # 29 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1158
Joined  08-06-2006

moblog mini-demo… enjoy.
controller

<?php

// handle requests for images sent
// up and collected by the pop controller
class Moblog extends Controller {
    
    
function Moblog()
    
{
        parent
::Controller();
    
}
    
    
function index()
    
{
        
// get the sorted list of directories in the attachments folder
        
$this->load->helper('directory');
        
$this->load->helper('html');
        
$dir = 'attachments/';
        
$file_path = APPPATH.$dir;
        
$server_path = $this->config->system_url().'application/'.$dir;
                
        
// use TRUE to map only one level
        
$map = directory_map($file_path, TRUE);

        
// get the last one using end()
        
$last_dir = end($map).'/';
        
$attachments = directory_map($file_path.$last_dir, TRUE);
        
// load the variable with an empty string
        // so the view doesn't cry about missing variable
        
$data['image_file_name'] = $attachments[0];
        
// for some reason a slash is being added
        
$data['image_server_path'] = rtrim($server_path.$last_dir.$attachments[0],'/');
        
$this->load->view('single_image', $data);
    
}
    
}
?>

view

<?php echo img($image_server_path);?>
 Signature 

imap_pop get email | site_migrate port sites | OOCalendar | PhotoBox2 gallery | CI/EE 2 word_limiter, yep, wrote it

Profile
 
 
Posted: 16 September 2009 03:13 PM   [ Ignore ]   [ # 30 ]  
Summer Student
Total Posts:  6
Joined  07-08-2009

First off: props for this class! Really what I needed (searching the web for ready to use classes didn’t bring much).

I have the following question though:

- if I try to recieve mail from an empty pop box I get the following error message:

A PHP Error was encountered

Severity
: Notice

Message
: Undefined variable: email_array

Filename
: libraries/imap_pop.php

Line Number
: 352

Anybody any clue?

Profile
 
 
   
2 of 3
2
 
‹‹ Multiple Applications      Inflector ››
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120128 Total Logged-in Users: 46
Total Topics: 126264 Total Anonymous Users: 2
Total Replies: 664082 Total Guests: 371
Total Posts: 790346    
Members ( View Memberlist )