I’m new to CI and trying to build my first app using it. I’d like to use the feature, which enable my users to add content to site via email. I discovered this wonderful imap pop class and tried to use it, but it returned a blank page after I called a function from the controller which was a copy of a func provided on imap pop class wiki-page. I tried to search in error log for error entries but there were none of them. So later Using mailhandler for drupal I discovered that my hoster won’t install IMAP module for PHP by default.
So my question/proposal that if it’ll be useful if imap pop class will return some error message in case IMAP module for PHP isn’t installed? Mailhandler for drupal provide such functionality and it’s very useful. Don’t you think that it’ll be nice to include it in imap pop class?
i returned here because someone was asking about license terms and i found that there is a discussion attached to this wiki page i made.
But, amazingly, but i was not automatically subscribed to it. seems like a feature request is in order for the EE forums… something like this:
“auto-subscribe me to any ensuing thread off of this wiki page”
anyhow, i’ve posted the license terms (X11 - commonly called MIT license).
regarding problems with code, always post code if you want help. as in, post your code if you want help. did i mention posting code is a good idea if you want help? this code has been working for me and others for over a year, so if you have problems, they can probably be worked out.
Well, not to seem like a total noob, but I’ve read through your class several times, and honestly am having a difficult time determining which functions are to be called from the controller and which are internal to the class.
does not actually retrieve the message count from the class function (function simply returns TRUE or FALSE), and these two functions, while similar, are not accessible from the controller:
/** * Get the number of emails at server * Calling this function updates msg_count var */ function count_messages() { $this->msg_count = imap_num_msg($this->IMAP_resource); return $this->msg_count; }
/** * Get count of messages returned by latest * call to the imap_num_msg() function that * was stored in the property * ACCESSOR method */ function get_message_count() { return $this->msg_count; }
SO… the gist of this post is: could we get some documentation for this library? Failing that, is there an easy-to-use IMAP lib that could be “ported” into CI that has full documentation?
thanks for the feedback. i made this library precisely because i couldn’t find any IMAP or POP library that was just the right size for my needs (basically moblog functionality). you could check out squirrelmail or roundcube which are two fully-featured, interface laden webmail readers with open source. but, i looked at their code base and i felt exactly the way you feel about this attempt!
anyhow, i am working on a rebuild of this class into a more object-oriented format so i am not going to document this iteration of it; it’s sort of simple, really, just a few functions with variations. it looks like you’ve got stuck into it… one question: have you been able to get your emails from your mailserver?
to answer your specific notes: yes, the connect_and_count() function does return TRUE or FALSE. i am updating the wiki now. there. thanks for noticing
yes, the two message count functions are similar except one is used when you want to get the “freshest” number from the mailserver and one is when you just want the “stored” number from the most recent “fresh” count request. this design (while a little confusing on the surface) lets a developer avoid network chatter everytime they want to figure out how many messages are at the account. the idea is that you use the “fresh” count function once and then in other subsequent calls, you use the “stored” function.
i made this library precisely because i couldn’t find any IMAP or POP library that was just the right size for my needs (basically moblog functionality). you could check out squirrelmail or roundcube which are two fully-featured, interface laden webmail readers with open source. but, i looked at their code base and i felt exactly the way you feel about this attempt!
Yep! I’m also looking for similar functionality for a web app. Full-fledged readers like Squirrelmail, et.al. are too much for my needs. Basically I need to import all messages for a particular account into MySQL. I’ve looked at a number of similar libs (almost all found on PHPClasses, FWIW) but none are well-documented and I’ve had little success bringing them into CI.
sophistry - 26 February 2009 08:30 AM
anyhow, i am working on a rebuild of this class into a more object-oriented format so i am not going to document this iteration of it; it’s sort of simple, really, just a few functions with variations. it looks like you’ve got stuck into it… one question: have you been able to get your emails from your mailserver?
Yes, I was able to make your example work just fine, except that it only pulls a single message. I goofed around with it a bit to try to make it cycle through and open all (only 2 on the server at this time in a test account) but no luck.
sophistry - 26 February 2009 08:30 AM
to answer your specific notes: yes, the connect_and_count() function does return TRUE or FALSE. i am updating the wiki now. there. thanks for noticing
No problem!
sophistry - 26 February 2009 08:30 AM
yes, the two message count functions are similar except one is used when you want to get the “freshest” number from the mailserver and one is when you just want the “stored” number from the most recent “fresh” count request. this design (while a little confusing on the surface) lets a developer avoid network chatter everytime they want to figure out how many messages are at the account. the idea is that you use the “fresh” count function once and then in other subsequent calls, you use the “stored” function.
Sure, that makes total sense.
I look forward to your updated version. So far, what you’ve put together _looks_ like it might work perfectly for my needs. I’m a pretty good developer, IMHO, but just need a little more documentation to know what’s what.
i scoured the web a few years back and found some of the stuff i needed on PHPClasses but as you said, not very well documented.
so, if you want to pull multiple messages you can do two things:
1) run the single message grabber and then delete_and_expunge() the message. then, the next time you run the single message grabber, you will get the next single message, and so on until the INBOX is empty. after delete_and_expunge(), depending on the mail server (and the settings in it) you are pulling email from, the message could get deleted, archived or remain “in” the inbox (those are the three choices in gmail anyway).
2) run the grab_emails_as_nested_array() function (or the grab_emails_as_nested_array_and_store() function) to grab multiple emails.
i have been using the imap_pop class in my set up for about two years now and it’s fine. my suggestion is do not wait for the new OO classes because they are even more complicated (but more general purpose - e.g., i just buit a listserv using the new lib and the CI email lib). my suggestion is to just forge ahead with the simple imap_pop class. once you get it running and get the array it returns it just runs pretty much in the background.
here’s how i get the email ‘strings’ array into a table called email (note: the email table has the fieldnames specified in the strings array).
this is in a model, but the code could be in a controller too. the actual function in my email_model is more complicated because it deals with the addresses too, but you should be able to get the idea about how simple it is to get the $em array data into a sql table by looking at this simple code.
function insert_email($em) { // $em comes in as two arrays items 'strings' and 'arrays' // strings has all the data that is bound for email db // arrays has the data for the address table and emailaddress join table // insert the items that are not arrays // throw the whole thing at it first // so that we get an email_id to use for linking $this->db->insert('email', $em['strings']); $email_id = $this->db->insert_id();
i love good docs (it’s what CI’s reputation is built on), but haven’t had time to buckle down and do it - rather be coding! same old story right?
if you want to pull multiple messages you can do two things:
1) run the single message grabber and then delete_and_expunge() the message. then, the next time you run the single message grabber, you will get the next single message, and so on until the INBOX is empty.
If this were a simple setup just for me, I might do it that way. However, I’m building a multi-user site, so that’s not going to work.
sophistry - 26 February 2009 10:15 AM
run the grab_emails_as_nested_array() function (or the grab_emails_as_nested_array_and_store() function) to grab multiple emails.
That’s a little more like it. If the user can grab all their mail at once, that’s the way to go. Once it’s in the DB they can do with it as they please.
sophistry - 26 February 2009 10:15 AM
i love good docs (it’s what CI’s reputation is built on), but haven’t had time to buckle down and do it - rather be coding! same old story right?
Absolutely! Thanks for the extra snippets there, too. Those are helpful.
I have a group of scripts that I found a few years ago and was able to cobble together a script that does basically what I need, except that it doesn’t handle attachments (other than to note that there was one, along with the name/size, etc.). The problem with those is that (a) again, no clear docs, and (b) no clear method of dealing with attachments.
I may yet end up taking everything I’ve found, and producing something that does exactly what I need. But like you said, who has the time?
It didn’t when I tested it last night, but it was a permissions issue more than anything else, IIRC.
No matter what I end up with, I’ll have to tweak that function to write to a different destination based on the user ID. Idea is to keep each user’s attachments in their own directory. Also probvably will want to rename attachments, and perform a few other tweaks like that.
so, you’ll use the set_IMAP_attachment_dir() function? the lib currently creates a directory made from the ‘fingerprint’ of the email and then just writes the files as named in the MIME parts.
if you come up with some nice utility code, please do share.
EDIT: oh yes, i just remembered that when i moved this lib to another platform, the chown() step failed because the lib assumes user www is running your webserver. you can just take out that chown() step and it will probably work because the directory is created by the webserver process regardless of the name so another webserver process can surely write to it..