Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Module for fetching body of email from Gmail from msg id?

by nysus (Parson)
on Sep 09, 2019 at 14:54 UTC ( [id://11105880]=perlquestion: print w/replies, xml ) Need Help??

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

I'm looking at cpan and there are tons of gmail modules out there. Most of them are pretty old. Wondering if anyone knows a good, simple and working module that would allow me to get the body of an email when passed the MSG ID as found in the last part of the url for a gmail email: https://mail.google.com/mail/u/0/#inbox/ZNfwlxwDqxHTTMhhRpTznnBJDsmKgsWM

UPDATE: Solution added in comments.

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: Module for fetching body of email from Gmail from msg id?
by Corion (Patriarch) on Sep 09, 2019 at 14:58 UTC

    Gmail supports IMAP, at least if you disable 2fa and click the right options. Just use (say) Mail::IMAPClient or maybe Mail::Box to access the mail account, search by message id, and download the body.

        Ah - if you cannot use the rfc822msgid links (as I do in the inverse direction with Mail::URLFor), then that post is bad news in the sense that you can't get there from here :-/

      Thanks, I'll check those out. I tried Net::IMAP::Simple::Gmail so far but that didn't have a way to look up the body of the msg from the long form of the msg id.

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
      $nysus = $PM . ' ' . $MCF;
      Click here if you love Perl Monks

Re: Module for fetching body of email from Gmail from msg id?
by nysus (Parson) on Sep 10, 2019 at 03:46 UTC

    Here is a solution, though it's a bit convoluted:

    1. Navigate to the gmail message thread of interest in your browser

    2. Run the following JS in the developer console (or better, programmatically with Applescript, if using Safari, or some other browser automation program like WWW::Mechanize::Chrome) to get the "legacy" thread id: document.querySelector('[data-legacy-thread-id]').getAttribute('data-legacy-thread-id')

    Once you get the thread id, you then need to convert it to a decimal number. my $dec_num_id = sprintf("%d", hex($thread_id));

    3. Now that you have obtained the thread id, you can use the Perl Mail::IMAPClient cpan module to obtain the messages in the thread by searching on the thread id:

    use Mail::IMAPClient; my $imap = Mail::IMAPClient->new( Server => 'imap.gmail.com', User => 'me', Password => 'blah', Ssl => 1, Uid => 1, ); my $folders = $imap->select('INBOX'); my $msgs = $imap->search("X-GM-THRID", $dec_num_id); foreach my $msg (@$msgs) { my $msg_st = $imap->message_string($msg); # slice and dice messages with modules listed below }

    4. Now you can use cpan modules like Email::MIME and Email::MIME::Attachment::Stripper and Email::MIME::Encodings to parse the emails in $msgs and decode them as necessary.

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11105880]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-19 20:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found