Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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

by nysus (Parson)
on Sep 10, 2019 at 03:46 UTC ( [id://11105923]=note: print w/replies, xml ) Need Help??


in reply to Module for fetching body of email from Gmail from msg id?

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: note [id://11105923]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-25 19:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found