How to read messages in a Gmail account with Perl?

I used the Mail :: Webmail :: Gmail module to read new messages in my Gmail account.

I wrote the following code for this purpose:

use strict; use warnings; use Data::Dumper; use Mail::Webmail::Gmail; my $gmail = Mail::Webmail::Gmail->new( username => 'username', password => 'password', ); my $messages = $gmail->get_messages( label => $Mail::Webmail::Gmail::FOLDERS{ 'INBOX' } ); foreach ( @{ $messages } ) { if ( $_->{ 'new' } ) { print "Subject: " . $_->{ 'subject' } . " / Blurb: " . $_->{ 'blurb' } . "\n"; } } 

But he did not print anything.

Can someone help me with this or suggest any other module for this?

Thanks in advance.

+4
source share
3 answers

You can use the Mail :: POP3Client module . It is used to receive messages from a Gmail account.

+3
source

This is almost a word taken from a word from Net :: IMAP :: Simple POD:

 use strict; use warnings; # required modules use Net::IMAP::Simple; use Email::Simple; use IO::Socket::SSL; # fill in your details here my $username = ' user@example.com '; my $password = 'secret'; my $mailhost = 'pop.gmail.com'; # Connect my $imap = Net::IMAP::Simple->new( $mailhost, port => 993, use_ssl => 1, ) || die "Unable to connect to IMAP: $Net::IMAP::Simple::errstr\n"; # Log in if ( !$imap->login( $username, $password ) ) { print STDERR "Login failed: " . $imap->errstr . "\n"; exit(64); } # Look in the the INBOX my $nm = $imap->select('INBOX'); # How many messages are there? my ($unseen, $recent, $num_messages) = $imap->status(); print "unseen: $unseen, recent: $recent, total: $num_messages\n\n"; ## Iterate through unseen messages for ( my $i = 1 ; $i <= $nm ; $i++ ) { if ( $imap->seen($i) ) { next; } else { my $es = Email::Simple->new( join '', @{ $imap->top($i) } ); printf( "[%03d] %s\n\t%s\n", $i, $es->header('From'), $es->header('Subject') ); } } # Disconnect $imap->quit; exit; 
+5
source

You tried to do some error checking after you tried to perform the operation

 if ($gmail->error()) { print $gmail->error_msg(); } 

I found that when I do this, this leads to:

Error: Failed to log in with these credentials - Could not find the final URL Also, HTTP error: 200 OK Error: Failed to log in.

I believe that this may be because the last module was updated in 2006, and gmail may have changed the way logins work so that it can no longer access it.

What could you do if you don't want to download new messages using pop3, you can use Net :: IMAP :: Simple to access your gmail account via IMAP

+1
source

Source: https://habr.com/ru/post/1305295/


All Articles