# IMAP: unexpected token LPAR (expected QUOTED or LITERAL)

**URL:** <https://www.ruby-forum.com/t/imap-unexpected-token-lpar-expected-quoted-or-literal/193406>\
**Category:** Ruby\
**Created:** [August 17, 2010, 8:13am UTC](https://www.ruby-forum.com/t/imap-unexpected-token-lpar-expected-quoted-or-literal/193406 "2010-08-17T08:13:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![voxom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9a140/32.png) [@voxom](https://www.ruby-forum.com/u/voxom)\
**Post date:** [August 17, 2010, 8:13am UTC](https://www.ruby-forum.com/t/imap-unexpected-token-lpar-expected-quoted-or-literal/193406/1 "2010-08-17T08:13:49Z")

</div>

I’m writing an IMAP parser that uses BODYSTRUCTURE to fetch only the  
text (not attachments) part of an email. On a few messages, ruby errors  
out on imap.fetch id, “BODYSTRUCTURE”, with  
“Net::IMAP::ResponseParseError: unexpected token LPAR (expected QUOTED  
or LITERAL)”. I would just rescue it, but it breaks the connection as  
well, raising “OpenSSL::SSL::SSLError: SSL\_write:: unexpected record”  
for any further fetches.

The only two other topics on this I have found have patches, but for  
earlier versions of ruby, which I have been unable to adapt.  
[http://rubyforge.org/tracker/index.php?func=detail&aid=3983&group\_id=426&atid=1698](http://rubyforge.org/tracker/index.php?func=detail&aid=3983&group_id=426&atid=1698)  
[https://www.ruby-forum.com/topic/87177](https://www.ruby-forum.com/topic/87177)

Help?

---

<div class="post-metadata">

**Author:** ![voxom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9a140/32.png) [@voxom](https://www.ruby-forum.com/u/voxom)\
**Post date:** [August 17, 2010, 8:14am UTC](https://www.ruby-forum.com/t/imap-unexpected-token-lpar-expected-quoted-or-literal/193406/2 "2010-08-17T08:14:54Z")

</div>

Heres an example message that triggers it.

---

<div class="post-metadata">

**Author:** ![voxom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9a140/32.png) [@voxom](https://www.ruby-forum.com/u/voxom)\
**Post date:** [August 17, 2010, 7:11pm UTC](https://www.ruby-forum.com/t/imap-unexpected-token-lpar-expected-quoted-or-literal/193406/3 "2010-08-17T19:11:28Z")

</div>

Heres the full trace, with Net::IMAP.debug = true

C: RUBY0003 FETCH 3398 bodystructure  
S: \* 3398 FETCH (BODYSTRUCTURE ((“ALTERNATIVE” (“BOUNDARY”  
“==alternative\_xad5934455aeex”) NIL NIL)(“TEXT” “HTML” (“CHARSET”  
“iso-8859-1” “NAME” “seti\_letter.html”) NIL NIL “7BIT” 6777 171 NIL NIL  
NIL) “ALTERNATIVE” (“BOUNDARY” “==alternative\_xad5934455aeex”) NIL NIL))  
@str: “\* 3398 FETCH (BODYSTRUCTURE ((“ALTERNATIVE” (“BOUNDARY”  
“==alternative\_xad5934455aeex”) NIL NIL)(“TEXT” “HTML”  
(“CHARSET” “iso-8859-1” “NAME” “seti\_letter.html”) NIL NIL  
“7BIT” 6777 171 NIL NIL NIL) “ALTERNATIVE” (“BOUNDARY”  
“==alternative\_xad5934455aeex”) NIL NIL))\r\n”  
@pos: 45  
@lex\_state: EXPR\_DATA  
@token.symbol: LPAR  
@token.value: “(”  
Net::IMAP::ResponseParseError: unexpected token LPAR (expected QUOTED or  
LITERAL)  
from  
/Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:3156:in  
`parse_error' from /Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:3008:in`match’  
from  
/Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:2956:in  
`case_insensitive_string' from /Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:2286:in`media\_type’  
from  
/Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:2219:in  
`body_type_basic' from /Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:2214:in`body\_type\_1part’  
from  
/Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:2198:in  
`body' from /Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:2273:in`body\_type\_mpart’  
from  
/Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:2196:in  
`body' from /Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:2173:in`body\_data’  
from  
/Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:2085:in  
`msg_att' from /Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:2055:in`numeric\_response’  
from  
/Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:1997:in  
`response_untagged' from /Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:1977:in`response’  
from  
/Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:1903:in  
`parse' from /Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:1028:in`get\_response’  
from  
/Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:936:in  
`receive_responses' from /Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:926:in`initialize’  
from  
/Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:925:in  
`start' from /Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:925:in`initialize’  
from (irb):3:in `new’  
from (irb):3
