treize has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks,

I come before you today seeking your wisdom and advice on using the Mail::IMAPClient module.

I have an email with the following subject:

test email "abc123" approved

When I search for this email as follows, I find the email:

my @uids = $imap->search("SUBJECT \"test email \"abc123\"\"")

However, if I try to search for the full subject name, I cannot find the email:

my @uids = $imap->search("SUBJECT \"test email \"abc123\" approved\"")

I've tried various other ways to search but to no avail. I only have this issue when double quotes are in the subject.

Has anyone experienced this before? Any help/advice would be great.

Thanks!

Update:

Here is the code I am using to search for a new email on my work's exchange server based on subject, to and from email addresses:

my $searchSubject = 'test email "abc123" approved'; sub search { my ($mailbox, $folder, $searchSubject, $searchFromAddress, $se +archToAddress) = @_; #Connecting to mailbox ... $imap->examine($folder); $imap->Uid(1); my @uids = $imap->search('UNSEEN', 'SUBJECT', $searchSubject, +'HEADER', 'FROM', $searchFromAddress, 'HEADER', 'TO', $searchToAddres +s);

If $searchSubject has double quotes in it, my code hangs at the $imap->search command.

Replies are listed 'Best First'.
Re: Mail::IMAPClient and searching email subjects with double quotes
by Corion (Patriarch) on May 14, 2014 at 09:11 UTC

    This seems to be a backwards compatibility issue with Mail::IMAPClient. Looking at ->search, it always calls ->_quote_search to quote its arguments. And ->_quote_search passes through everything unchanged if it receives only one argument except the object.

    This makes passing only one unquoted argument to ->search inconvenient.

    I used the following code to play around with this:

    >perl -MData::Dumper -MMail::IMAPClient -we "print Dumper [@ARGV], Mai +l::IMAPClient->_quote_search(@ARGV)" "1 \"2\" foo" 3 $VAR1 = [ '1 "2" foo', '3' ]; $VAR2 = '{9} 1 "2" foo'; $VAR3 = '3'; >perl -MData::Dumper -MMail::IMAPClient -we "print Dumper [@ARGV], Mai +l::IMAPClient->_quote_search(@ARGV)" "\"2\" foo" $VAR1 = [ '"2" foo' ]; $VAR2 = '"2" foo';

    So ideally, if you want to search only for the SUBJECT attribute, you can split up your query arguments into the two strings SUBJECT and the actual, unquoted target string. That should trigger the appropriate argument quoting in Mail::IMAPClient.

    use strict; use Mail::IMAPClient; use Data::Dumper; sub test_quote { print "Parameters: " . Dumper \@_; my @quoted= Mail::IMAPClient->_quote_search(@_); print "Result : " . Dumper \@quoted; }; test_quote( q(SUBJECT "SUBJECT \"test email \"abc123\" approved\")); test_quote( SUBJECT => q(test email "abc123" approved) ); __END__ Parameters: $VAR1 = [ 'SUBJECT "SUBJECT \\"test email \\"abc123\\" approved\\"' ]; Result : $VAR1 = [ 'SUBJECT "SUBJECT \\"test email \\"abc123\\" approved\\"' ]; Parameters: $VAR1 = [ 'SUBJECT', 'test email "abc123" approved' ]; Result : $VAR1 = [ 'SUBJECT', '{28} test email "abc123" approved' ];

    (Maybe your assumption was that IMAP quoting works like shell quoting?)

      Hi there and thank you for looking into this.

      I have actually tried to split up the search into two strings like you mentioned and I get another issue, the search hangs.

      I will update my original question with more information and more snippets of my code. One thing I forgot to mention is that I am searching on an exchange server.

Re: Mail::IMAPClient and searching email subjects with double quotes
by jeffa (Bishop) on May 13, 2014 at 21:39 UTC

    Did you attempt to pass your argument wrapped in single quotes?

    my @uids = $imap->search( 'SUBJECT "test email "abc123" approved"' );

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

      Thanks for your reply!

      I have tried that and the same issue is still there.

        Sorry it wasn't the answer. You should try to provide an example of the data you are searching for as well as the code -- that is, the subject line itself. If we can't see it, we can't rule out that the method is properly returning no results due to your search string correctly not matching. Perhaps the subject line contains control characters or such? The relevant docs might provide more insight as well. I'd try a few more variations myself ...

        UPDATE!

        Did you try removing that final, trailing double quote?

        my @uids = $imap->search( 'SUBJECT "test email "abc123" approved' );

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)