How to connect to your Gmail inbox

I am currently trying to connect to a gmail inbox using Perl and Net::IMAP::Client

with the following code

use strict;
use warnings;

use Net::IMAP::Client;

my $user = '[address]@gmail.com';
my $pwd = '[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)

      

But the variable $imap

does matter undef

and I get this error:

Using uninitialized $ _ value in concatenation (.) Or string on line testIMAP.pl 9.
Failed to connect to IMAP server: on line testIMAP.pl 9.

I have successfully connected to my mailbox using Outlook, but since I am not getting the error, I am not sure where to look. Does anyone know what I am doing wrong here?

+3


source to share


1 answer


Many thanks to zdim for troubleshooting help.

First, zdim indicated that I had the wrong error. $ _ should have been $!

This showed the error message "Network is not available", however I was able to successfully pint and telnet to "imap.gmail.com".

A solution to this was found here Perl IO :: Socket :: SSL: connect: no network available .

Change the use statement in the Net :: IMAP :: Client module to the following:



use IO::Socket::SSL 'inet4';

      

After that, the connection was established, but the login failed with

Login failed: [ALERT] Please log in via your web browser: https://support.google.com/mail/accounts/answer/78754 (Failure)

      

This is due to the security features of Gmail. I received an email that allowed me to confirm that the connection was not malicious and then subsequent logins were successful.

For others, there may be several solutions to this latter. You may need to provide a password for the app. If 2-Step Verification is enabled or you may need to enable "allow less secure applications"

+3


source







All Articles