#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    3
    Rep Power
    0

    Need Help with Perl MAIL::IMAPClient


    Hi Perl Gurus,

    I'm new to Perl. I need help with a Perl script (already written by someone) to make it work with IMAP server login without password for migration purpose.

    My IMAP server is already been set to talk on port 143 with NULL password if Authmechanism is EXTERNAL. I tried to put in a code form other working script (imapsync) but gives all sortd of error. Can you please help to fix the code. I'm not too familier with perl. Any quick help is greatly appreciated.

    Here is the error -

    "my" variable $imap masks earlier declaration in same scope at attachment_parser.pl line 213.
    "my" variable $port masks earlier declaration in same scope at attachment_parser.pl line 214.
    "my" variable $user masks earlier declaration in same scope at attachment_parser.pl line 214.
    "my" variable $authmech masks earlier declaration in same attachment_parser.pl line 216.
    Undefined subroutine &main::check_capability called at attachment_parser.pl line 219.
    at attachment_parser.pl line 219
    Undefined subroutine &main::check_capability called at attachment_parser.pl line 219.
    print() on closed filehandle LOGFILE at ./start_migration.pl line 38.

    The original code no longer working as the IMAP serer and Auth Mechanism changed

    The New IMAP Server needs :

    Authmechanism EXTERNAL to make it work.
    Any help is greatly appreciated. I'm in a time bound project and need to be done quicker.

    Thank you for reading this.

    Original Code
    ---------------

    use warnings;

    ## Show stack traces
    use Carp ();
    local $SIG{ __WARN__ } = \&Carp::cluck;
    local $SIG{ __DIE__ } = \&Carp::cluck;

    use Mail::IMAPClient;
    use Mail::IMAPClient::BodyStructure;
    use Data:umper;
    use Net::IMAP::Simple;
    use File::Path;
    use Net::IMAP::Client;
    #use MIME::Parser;


    ## Default vars
    ## Use special (twisted) proxy for access
    #my $port = '143';
    unless (defined($ARGV[0])) { die "No user specified!\n"; }
    my $user = "$ARGV[0]";
    unless (defined($ARGV[1])) { die "No user specified!\n"; }
    my $server = "$ARGV[1]";
    my $port = '2112';
    #my $port = '143';
    my $pass = 'none';
    my $msgSizeLimit = 31457280; # Remove attachments from messages > 30 MB
    my $debug = 1; # 0=false, 1=true, 2=verbose

    ## Connect to the server
    my $start_time = time_stamp();
    if ($debug == 1) { print time_stamp() . " Start\n";}
    print time_stamp() . " Connecting to $server... ";
    #my $imap = Mail::IMAPClient->new(
    # #Fast_io => 'True',
    # #Buffer => '51200',
    # #Debug => 'True',

    Server => $server,
    Port => $port,
    User => $user,
    Password => $pass,
    #Debug => ( $debug ? 'true' : 'false' ),
    # ShowCredentials => 'true',
    # ) or die "Cannot connect to $server as $user: $@";
    #$imap->login or die "Could not login: $@\n";
    #print "Connected.\n";


    Modified version of code from other script (Connect to the server section modified)
    --------------------------------------------
    ## Added Variables ##
    my $port = '143';
    my $pass = 'NULL';
    my $authmech = 'EXTERNAL';
    ## Added Variables Completed##

    ### Modified Connect to the server

    my $imap = Mail::IMAPClient->new();
    my($imap,
    $host, $port, $user, $domain, $password,
    $mydebugimap, $mytimeout, $fastio,
    $ssl, $tls, $authmech, $authuser, $reconnectretry,
    $proxyauth, $uid, $split, $Side ) = @_ ;

    check_capability( $imap, $authmech, $Side ) ;

    if ( $proxyauth ) {
    $imap->Authmechanism("") ;
    $imap->User($authuser) ;
    } else {
    $imap->Authmechanism( $authmech ) unless ( $authmech eq 'LOGIN' or $authmech eq 'PREAUTH' ) ;
    $imap->User($user) ;
    }

    $imap->Authcallback(\&xoauth) if $authmech eq "XOAUTH" ;
    $imap->Authcallback(\&plainauth) if $authmech eq "PLAIN" || ($authmech eq "EXTERNAL") ;

    if ($proxyauth) {
    $imap->User($authuser);
    $imap->Domain($domain) if (defined($domain));
    $imap->Authuser($authuser);
    if ($authmech eq "EXTERNAL") {$password = "NULL"};
    $imap->Password($password);
    } else {
    $imap->User($user);
    $imap->Domain($domain) if (defined($domain));
    $imap->Authuser($authuser);
    if ($authmech eq "EXTERNAL") {$password = "NULL"};
    $imap->Password($password);
    }

    ### Modification Completed
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    776
    Rep Power
    495
    Hi,

    Perl Code:
    ## Added Variables ##
    my $port = '143';
    my $pass = 'NULL';
    my $authmech = 'EXTERNAL';
    ## Added Variables Completed##
     
    ### Modified Connect to the server
     
    my $imap = Mail::IMAPClient->new();
    my($imap,
    $host, $port, $user, $domain, $password,
    $mydebugimap, $mytimeout, $fastio,
    $ssl, $tls, $authmech, $authuser, $reconnectretry,
    $proxyauth, $uid, $split, $Side ) = @_ ;


    $port is declared on line 2 above (with the my operator), $authmech on line 4 and $imap on line 9. Then, on lines 10 to 14, you are declaring a whole bunch of variables, including some already declared such as $port, $authmech and $imap. The warning simply tells you that you are redeclaring the same variables in the same scope.

    And by the way, this seems to be not only a syntax error, but also a logical error: the values you've just assigned to $port, $authmech and $imap will be scrapped by the assignment of lines 10 to 14 to @_. So, although I am not sure what yuou are trying to do, the logic of the program seems to be flawed

    The next error concerned this:
    Perl Code:
    check_capability( $imap, $authmech, $Side ) ;

    The check_capability subroutine is not defined anywhere.

    The last error (print on a closed filehandler) does not seem to be in the code you posted, but I guess its meaning is quite clear.

    Hope this helps.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    3
    Rep Power
    0
    Thank you for advice. Is it possible to help me professionally fix the code ? The main purpose is to connect to IMAP server with authmech external and NULL password.

    I'm willing to pay for the service. I need to fix this so I can continue with my migration. I'm not a Perl coder. This was written by someone else who is no longer with us.

    Regards

    Originally Posted by Laurent_R
    Hi,

    Perl Code:
    ## Added Variables ##
    my $port = '143';
    my $pass = 'NULL';
    my $authmech = 'EXTERNAL';
    ## Added Variables Completed##
     
    ### Modified Connect to the server
     
    my $imap = Mail::IMAPClient->new();
    my($imap,
    $host, $port, $user, $domain, $password,
    $mydebugimap, $mytimeout, $fastio,
    $ssl, $tls, $authmech, $authuser, $reconnectretry,
    $proxyauth, $uid, $split, $Side ) = @_ ;


    $port is declared on line 2 above (with the my operator), $authmech on line 4 and $imap on line 9. Then, on lines 10 to 14, you are declaring a whole bunch of variables, including some already declared such as $port, $authmech and $imap. The warning simply tells you that you are redeclaring the same variables in the same scope.

    And by the way, this seems to be not only a syntax error, but also a logical error: the values you've just assigned to $port, $authmech and $imap will be scrapped by the assignment of lines 10 to 14 to @_. So, although I am not sure what yuou are trying to do, the logic of the program seems to be flawed

    The next error concerned this:
    Perl Code:
    check_capability( $imap, $authmech, $Side ) ;

    The check_capability subroutine is not defined anywhere.

    The last error (print on a closed filehandler) does not seem to be in the code you posted, but I guess its meaning is quite clear.

    Hope this helps.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    776
    Rep Power
    495
    Sorry, I do not know anything about IMAP. I am willing to help with the Perl syntax if needed, but can't help on IMAP.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by Laurent_R
    Sorry, I do not know anything about IMAP. I am willing to help with the Perl syntax if needed, but can't help on IMAP.
    Thanks. I only need help with Perl. In the original code , I need to modify /add authmech (EXTERNAL) , authcallback and pass as NULL. If that piece of Perl code works , I'm good. Please let me know if you can modify to make that code work. I'll pay you for this.

    Thanks again.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    776
    Rep Power
    495
    Your so-called original code cannot really be the original code, as it can't work.

    Perl Code:
    #my $imap = Mail::IMAPClient->new(
    # #Fast_io => 'True',
    # #Buffer => '51200',
    # #Debug => 'True',
     
    Server => $server,
    Port => $port,
    User => $user,
    Password => $pass,
    #Debug => ( $debug ? 'true' : 'false' ),
    # ShowCredentials => 'true',
    # ) or die "Cannot connect to $server as $user: $@";


    The above lines are supposed to do the connection.

    At the very least, the very first line above should be decommented out (i.e. remove the # at the beginning.

    Then, my guess is that you should probably add authmech to the list of parameters, something like:

    Perl Code:
    my $imap = Mail::IMAPClient->new(
    Server => $server,
    Port => $port,
    User => $user,
    Authmech => 'EXTERNAL',
    Authcallback => NULL, 
    Password => $pass,
    # ) or die "Cannot connect to $server as $user: $@";


    but you check the documentation of Net::IMAP::Simple or Net::IMAP::Client, where you will probably find some examples (I must go now, and I have no time to check right now).

IMN logo majestic logo threadwatch logo seochat tools logo