# Net/imap - imap.search "TO" can't find email

**URL:** <https://www.ruby-forum.com/t/net-imap-imap-search-to-cant-find-email/134918>\
**Category:** Ruby\
**Created:** [April 16, 2008, 5:51am UTC](https://www.ruby-forum.com/t/net-imap-imap-search-to-cant-find-email/134918 "2008-04-16T05:51:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasonnaylor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b487fb/32.png) [@jasonnaylor](https://www.ruby-forum.com/u/jasonnaylor)\
**Post date:** [April 16, 2008, 5:51am UTC](https://www.ruby-forum.com/t/net-imap-imap-search-to-cant-find-email/134918/1 "2008-04-16T05:51:32Z")

</div>

I’m having trouble with the “search” feature of Net::IMAP. I’m  
attempting to search for a message “TO” a particular recipient.

The email recipient is something like:

removed\_email\_address@domain.invalid

A search function of:

imap.search([“TO”, “removed\_email\_address@domain.invalid”])

… simply returns nothing. By removing the mailbox details and only  
searching for the domain:

imap.search([“TO”, “@this.dom.ain”])

… I return results. In fact, I can build the “TO” string as long  
as:

imap.search([“TO”, “removed\_email\_address@domain.invalid”])

… and results are returned.

What don’t I understand? Is it my limited knowledge of Ruby, or the  
imap.search I’m not understanding???

Note: Here’s the fetch “ENVELOPE” of the mail I’m after:

irb(main):073:0\* imap.fetch(1179, “ENVELOPE”)  
\<… snip…\>  
, to=[#\<struct Net::IMAP::Address name=“removed\_email\_address@domain.invalid”,  
route=nil, mailbox=“080416w\_test”, host=“this.dom.ain”\>]  
\<… snip…\>

---

<div class="post-metadata">

**Author:** ![jasonnaylor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b487fb/32.png) [@jasonnaylor](https://www.ruby-forum.com/u/jasonnaylor)\
**Post date:** [April 16, 2008, 6:10am UTC](https://www.ruby-forum.com/t/net-imap-imap-search-to-cant-find-email/134918/2 "2008-04-16T06:10:51Z")

</div>

Interestingly, I can find the following (another entirely different  
message):

irb(main):107:0\> imap.search([“TO”, “removed\_email\_address@domain.invalid”])  
=\> [1174]

So it appears something about the “\_” character in the mailbox of the  
previous search might be the culprit:

`removed_email_address@domain.invalid`
