0

I'm curious, can the httplib be jury rigged to connect to an IMAP email server?

I'm being forced to python connect to an IMAP server without using IMAPlib and while I've learned a lot about sockets and IMAP, I'm frankly growing tired of the exercise and this workaround would be totally awesome.

If it's not easily possible, I understand and accept that as a legitimate answer.

Gary Kerr
  • 13,650
  • 4
  • 48
  • 51
Pinwheeler
  • 1,061
  • 2
  • 13
  • 26
  • Check out Aurinko's Unified Email API, it supports IMAP, and it's REST, so it could simplify your integration efforts. – Alexey Aug 28 '21 at 16:22

1 Answers1

2

Sorry, but I don't think so. At least, if you manage to do it somehow, it won't be any easier than implementing IMAP over raw sockets yourself. HTTP and IMAP are very different protocols, and a library specialized in speaking one language will have a hard time communicating in the other one.

I don't really know about your objective and what you're allowed to use and why (Now really, why can't you use imaplib?), but let's look at a few options here:

  • You could implement IMAP by hand over raw sockets or whatever you're allowed to use. At best, you'll end up with something like Python's imaplib with spending much more time/work/money (again, who would forbid you from using a ready-to-use IMAP library if you want t oaccess IMAP? Is this some kind of trick?).
  • You could try and access the Email via POP,which I personally find a lot easier than IMAP. Actually, easy enough to speak to a mail server in person without a program in between. It's not as cool as IMAP, but if you just need to fetch mail, it's less work.
  • There are some excellent console email clients which speak IMAP. Mutt comes to mind, but that might not be your best choice (don't know, check out what options are available). They usually save emails in mbox or Maildir format. Both are tried, tested and well-defined, and reading out some files will probably be easier than full-on IMAP access. (If you're lucky, you might even be able to use some module to read those formats.) Update: Oh, here, look at Python's mailbox module. Can you use that?
Carsten
  • 17,991
  • 4
  • 48
  • 53
  • thanks for the info! The task is for getting an interview into a programming company. The task is parse an email (they mention from gmail, but don't know if that is the exact requirement) without using any "email specific modules or libraries". I actually asked about imaplib because I agree that it's a pretty silly task, although I am learning a LOT about sockets and such. – Pinwheeler May 03 '13 at 23:36
  • 1
    Ooooh I see. Parsing an Email is a task very much different from fetching Emails from a mail server. You should check out which one of them you need to do. – Carsten May 03 '13 at 23:45
  • I asked them if imaplib was allowed to be used and they said no... I feel like they were wrong though???? Ah well, I'll be a better programmer for having done it. I've already read the IMAP RFC and I'm actually having a bit of a breakthrough – Pinwheeler May 03 '13 at 23:53
  • 1
    @Pinwheeler: For a programming test (especially if you're applying for a job where you'll have to design and implement network protocols), asking you to write an IMAP library from scratch isn't too bad of a test. The key thing about IMAP is that it's a line-based protocol, so you can get away with calling `makefile` on the socket and just reading line by line, so you don't need to spend 80% of your time debugging an ad-hoc event loop/buffer/etc. But I'd still ask if I could use `twisted`/`tulip`/something so I only have to write the protocol code. – abarnert May 04 '13 at 00:31
  • 2
    @Pinwheeler: Also, even though you can't usefully _use_ `httplib`, you can still read [the source](http://hg.python.org/cpython/file/2.7/Lib/httplib.py) for ideas on how to write a line-based protocol with a simple state machine, and even borrow some code. – abarnert May 04 '13 at 00:33
  • 2
    @Pinwheeler: One last thing: If I were the interviewer, when you asked "can I use `imaplib`?", I'd give you +100 points for that, but then ask, what if `imaplib` didn't exist?" If you then said "Can I look on PyPI?", that's another +50 points. Ultimately, I'd still ask them to write it from scratch for the final 250 points… but you may already be ahead of the competition because you asked the smart question first (and, if you're not, you may not want this job). – abarnert May 04 '13 at 00:36
  • @abarnert aw gee, thanks! I'm actually already being heavily "inspired" by imaplib.py I'm having a bit of a moral dilemma distinguishing where the line marking off "wholesale theft" exists. I do believe that Twisted will actually work just fine. The spirit of the exercise is to not allow any "email specific" or email parsing doohickeys (EDIT: it appears twisted might be an email parsing doohickey). I did not know about Twisted until you brought it up. – Pinwheeler May 04 '13 at 01:13
  • @Carsten Just wanted to give you guys some follow-up: I asked the company and they said that they don't need me to fetch it from a server, they just want something that takes the "show original" from gmail as text and parses it... so about four times easier – Pinwheeler May 06 '13 at 17:10
  • @Pinwheeler: Well, `twisted` definitely _has_ an email parsing doohickey… but you can ignore that and use the rest of the framework. If you read the IMAP code from `twisted`, it's a lot simpler than `imaplib`, because it can assume you've got a protocol that feeds you lines as they come in, without having to write all that low-level networking stuff. If you'd never heard of it before this weekend, it's got a bit of a learning curve… but in the long run, learning `twisted` is a good idea even if you never use it (because it encourages new ways to think about things). – abarnert May 06 '13 at 18:22
  • 2
    @Pinwheeler: But it sounds like you're just writing [`rfc822`](http://docs.python.org/2/library/rfc822.html)/[`email`](http://docs.python.org/2/library/email.html), not the IMAP client side of things, in which case all of this was a side issue. Of course the code in `email.parser` and friends will be as helpful for that as the code in `imaplib` was for the other problem. – abarnert May 06 '13 at 18:25
  • @abarnert Thanks for helping me along in this next step to becoming a real programmer (TM). It's kind of a shame that they don't want me to do the networking portion since I've put so much time into it, but I definitely did learn a lot and this parsing thing is WAY easier – Pinwheeler May 06 '13 at 18:28
  • 1
    @Pinwheeler: Finally, for the line between inspiration and plagiarism: Normally, it doesn't matter: borrow what you need, make sure your license is compatible, attribute conscientiously, and everyone is happy. But when your license _can't_ be compatible, a common practice is to analyze the original code, taking any notes you need, then wait until you've forgotten the original code (or, better, hand the notes off to someone else), then write your code based on the notes. Don't take that as legal advice on the GPL or anything, but as ethical advice for a job interview, I think it's reasonable. – abarnert May 06 '13 at 18:30