How can I connect to gmail from perl?

I am trying to read messages from a gmail account, and the examples I saw do not work.

I started with this:

use Mail::IMAPClient;
use IO::Socket::SSL;

my $user = 'user\@mydomain.com';
my $pwd = 'password';

my $socket = IO::Socket::SSL->new(
    PeerAddr => 'imap.gmail.com',
    PeerPort => 993,
    SSL_verify_mode => SSL_VERIFY_PEER,
    )
    or die "socket(): $@";

my $client = Mail::IMAPClient->new(
    Socket => $socket,
    User     => $user,
    Password => $pwd,
    )
    or die "new(): $@";

if ( $client->IsAuthenticated() ) {
    print "Auth OK\n";
} else {
    print "No auth\n";
}

This seems to work, but is not authenticated. According to the documentation, Mail :: IMAPClient-> new should call a login if a username and password are specified.

I tried calling client-> login no difference.

There are several questions with similar content, but the answers claim that they use a different package (Mail :: Webmail :: Gmail is one, but it seems outdated and does not work)

An account is a Google Apps account, not a regular gmail account. I have enabled IMAP access for the account.

Net:: IMAP:: Client, Google Apps, gmail , " ".

use Net::IMAP::Client;

my $user = 'user\@gmail.com';
my $pwd = ' app password';

my $imap = Net::IMAP::Client->new(
    server => 'imap.gmail.com',
    user   => $user,
    pass   => $pwd,
    ssl    => 1,                              # (use SSL? default no)
    ssl_verify_peer => 0,                     # (use ca to verify server, default yes)
    port   => 993                             # (but defaults are sane)
    ) or die "Could not connect to IMAP server: $_";

$imap->login or
    die('Login failed: ' . $imap->last_error);

-, gmail imap?

+4
1

Perl "\@" "\@".

\.

+7

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


All Articles