I want to connect IMAP server but i am not able to connect directly imap server that's why i used proxy but still i am not able to connect and read emails.
Following is my code,
#!/usr/intel/bin/perl
use strict;
use warnings;
# fill in your details here
my $username = 'username#companyname.com';
my $password = 'password';
my $mailhost = 'outlook.office365.com';#imap-mail.outlook.com
my $mailport = 993;
my $proxyhost = '121.244.253.5';
my $proxyport = 8080;
print "Proxy...\n";
use IO::Socket::Socks::Wrapper(
{
ProxyAddr => $proxyhost,
ProxyPort => $proxyport,
SocksDebug => 0,
Timeout => 100000000
}
);
# required modules
use Net::IMAP::Simple;
use Email::Simple;
use IO::Socket::SSL;
print "Connecting...\n";
$IO::Socket::SSL::DEBUG=2;
# Connect
my $imap = Net::IMAP::Simple->new(
$mailhost,
port => $mailport,
use_ssl => 1
) || die "Unable to connect to IMAP: $Net::IMAP::Simple::errstr \n";
print "Logging In...\n";
# Log in
if ( !$imap->login( $username, $password ) ) {
print STDERR "Login failed: " . $imap->errstr . "\n";
exit(64);
}
print "Selecting Folder...\n";
# Look in the the INBOX
my $nm = $imap->select('Archive');
print "How Many Messages Are There...\n";
# How many messages are there?
my ($unseen, $recent, $num_messages) = $imap->status();
print "unseen: $unseen, recent: $recent, total: $num_messages\n\n";
print "Quickly Look for unseen messages...\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') );
}
}
print "Disconnect...\n";
# Disconnect
$imap->quit;
print "Exit...\n";
exit;
Following is my Response :-
Proxy...
Connecting...
DEBUG: .../IO/Socket/SSL.pm:332: socket not yet connected
DEBUG: .../IO/Socket/SSL.pm:334: socket connected
DEBUG: .../IO/Socket/SSL.pm:347: ssl handshake not started
DEBUG: .../IO/Socket/SSL.pm:377: set socket to non-blocking to enforce timeout=100000000
I don't know why its not going further. Please share your opinion and correct me.
Please help me here...
Correct syntax is :
my $proxyhost = '65.130.4.202';
my $proxyport = 24451;
use Net::IMAP::Simple;
print "Proxy...\n";
use IO::Socket::Socks::Wrapper(
Net::IMAP::Simple => {
ProxyAddr => $proxyhost,
ProxyPort => $proxyport,
SocksDebug => 3,
Timeout => 100000000
}
);
use IO::Socket::SSL;
print "Connecting...\n";
$IO::Socket::SSL::DEBUG=2;
# Connect
my $imap = Net::IMAP::Simple->new(
'imap.gmail.com',
port => 993,
use_ssl => 1
) || die "Unable to connect to IMAP: $Net::IMAP::Simple::errstr \n";
print "Logging In...\n";
# Log in
if ( !$imap->login( 'pappucant#gmail.com', 'pappu123' ) ) {
print STDERR "Login failed: " . $imap->errstr . "\n";
exit(64);
}
Working fine for me, didn't work with the proxy server mentioned in the code.
Output:
Proxy...
Connecting...
DEBUG: .../IO/Socket/SSL.pm:625: socket not yet connected
DEBUG: .../IO/Socket/SSL.pm:627: socket connected
DEBUG: .../IO/Socket/SSL.pm:649: ssl handshake not started
DEBUG: .../IO/Socket/SSL.pm:682: using SNI with hostname imap.gmail.com
DEBUG: .../IO/Socket/SSL.pm:738: set socket to non-blocking to enforce timeout=90
DEBUG: .../IO/Socket/SSL.pm:764: ssl handshake in progress
DEBUG: .../IO/Socket/SSL.pm:774: waiting for fd to become ready: SSL wants a read first
DEBUG: .../IO/Socket/SSL.pm:794: socket ready, retrying connect
DEBUG: .../IO/Socket/SSL.pm:764: ssl handshake in progress
DEBUG: .../IO/Socket/SSL.pm:774: waiting for fd to become ready: SSL wants a read first
DEBUG: .../IO/Socket/SSL.pm:794: socket ready, retrying connect
DEBUG: .../IO/Socket/SSL.pm:764: ssl handshake in progress
DEBUG: .../IO/Socket/SSL.pm:774: waiting for fd to become ready: SSL wants a read first
DEBUG: .../IO/Socket/SSL.pm:794: socket ready, retrying connect
DEBUG: .../IO/Socket/SSL.pm:809: ssl handshake done
Logging In...
Login failed: [AUTHENTICATIONFAILED] Invalid credentials (Failure)
Related
Sample server:
#!/usr/bin/env perl6
my $listen = IO::Socket::INET.new(:listen, :localhost<localhost>, :localport(3333));
loop {
my $conn = $listen.accept;
while my $buf = $conn.recv(:bin) {
$conn.write: $buf;
}
$conn.close;
}
Client:
#!/use/bin/env perl6
my $c = IO::Socket::INET.new(:host<localhost>, :port(3333));
$c.print: "{time}\n";
#say $c.recv; #commented out on purpose
sleep 1 ;
$c.close ;
server error:
Could not receive data from socket: Connection reset by peer
in block <unit> at server4.p6 line 5
In the server on each of the blocks I tried CATCH and QUIT. How should I catch this error?
Server needs to catch the error in the loop block:
#!/usr/bin/env perl6
my $listen = IO::Socket::INET.new(:listen, :localhost<localhost>, :localport(3333));
loop {
my $conn = $listen.accept;
while my $buf = $conn.get {
$conn.print: $buf;
}
$conn.close;
CATCH { default { say .^name, ': ', .Str , " handled in $?LINE";}}
}
Output of server reports the error and stays running to accept new connections:
perl6 --ll-exception server.p6
X::AdHoc: Could not receive data from socket: Connection reset by peer handled in 9
I have searched for days but finally run out of options, one of my clients move its emails accounts to "outlook.office365.com", now all my perl scripts fails because they have a different set of configurations on their platform, so i had to rewrite all of my perl scripts to make everything work again, i was aible to send emails with the module "Email::Sender::Transport::SMTP::TLS" but i didnt had the same luck using my old modules to read and proccess emails such as "Mail::POP3Client".
This is the code a try:
use Mail::IMAPClient;
use Email::MIME;
my $username = 'user';
my $password = 'pass';
my $server = 'outlook.office365.com';
print "Anon connect to IMAP\n";
my $imap = Mail::IMAPClient->new(
Server => $server,
Username => $username,
Password => $password,
Port => 993,
Ssl => 1,
Authmechanism => "PLAIN",
Debug => 1,
)or die "Cannot connect to $mailhost as $username: $#";
print "upgrading connection to TLS \n";
$imap->starttls
(
SSL_verify_mode => 0,
) or die "starttls failed: $#\n";
$imap->User($username);
$imap->Password($password);
print "Logging In\n";
$imap->login() or die "imap login failed: $#\n";
This is a solution that i have found on stackoverflow but it doesnt work for me, i checked my firewall configuration and everything is ok.
This is the output:
Anon connect to IMAP
Started at Fri Dec 16 13:54:25 2016
Using Mail::IMAPClient version 3.38 on perl 5.022001
Connecting with IO::Socket::SSL PeerAddr outlook.office365.com PeerPort 993 Proto tcp Timeout 600 Debug 1
ERROR: Unable to connect to outlook.office365.com: at C:/Perl64/site/lib/Mail/IMAPClient.pm line 370.
Mail::IMAPClient::connect(Mail::IMAPClient=HASH(0xcac170)) called at C:/Perl64/site/lib/Mail/IMAPClient.pm line 314
Mail::IMAPClient::new("Mail::IMAPClient", "Server", "outlook.office365.com", "Username", "user", "Password", "pass", "Port", 993, ...) called at dont.pl line 10
Cannot connect to as user: Unable to connect to outlook.office365.com: at dont.pl line 10.
To check my SSL connection:
openssl s_client -connect outlook.office365.com:993
Results:
* OK The Microsoft Exchange IMAP4 service is ready.
So any ideas?, what is wrong with this code?
EDIT: thanks to Max, finally a solution: removing startls allows to reach the login step.
use Mail::IMAPClient;
use Email::MIME;
my $username = 'user';
my $password = 'pass';
my $server = 'outlook.office365.com';
print "Anon connect to IMAP\n";
my $imap = Mail::IMAPClient->new(
Server => $server,
Port => 993,
Ssl => 1,
Authmechanism => "PLAIN",
Debug => 1,
)or die "Cannot connect to $mailhost as $username: $#";
$imap->User($username);
$imap->Password($password);
print "Logging In\n";
$imap->login() or die "imap login failed: $#\n";
On my machine, it outputs this:
Anon connect to IMAP
Started at Fri Dec 16 16:59:31 2016
Using Mail::IMAPClient version 3.38 on perl 5.022001
Read: * OK The Microsoft Exchange IMAP4 service is ready. [RABCADYAUABSADAAMgAwADEAQwBBADAAMAAyADcALgBlAHUAcgBwAHIAZAAwADIALgBwAHIAbwBkAC4AbwB1AHQAbABvAG8AawAuAGMAbwBtAA==]
upgrading connection to TLS
Sending: 1 CAPABILITY
Sent 14 bytes
Read: * CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=XOAUTH2 SASL-IR UIDPLUS MOVE ID UNSELECT CHILDREN IDLE NAMESPACE LITERAL+
1 OK CAPABILITY completed.
Logging In
Sending: 2 AUTHENTICATE PLAIN
Sent 22 bytes
Read: +
Sending: [Redact: Count=2 Showcredentials=OFF]
Sent 18 bytes
Read: 2 NO AUTHENTICATE failed.
ERROR: 2 NO AUTHENTICATE failed. at /usr/local/share/perl/5.22.1/Mail/IMAPClient.pm line 3261.
Mail::IMAPClient::authenticate(Mail::IMAPClient=HASH(0x14f6a30), "PLAIN", undef) called at /usr/local/share/perl/5.22.1/Mail/IMAPClient.pm line 562
Mail::IMAPClient::login(Mail::IMAPClient=HASH(0x14f6a30)) called at imapt.pl line 30
imap login failed: 2 NO AUTHENTICATE failed.
Which is normal, as I don't have a valid account.
I am using the Net::STOMP::Client module to connect to a Tibco JMS Topic Server which is not hosted by me.
I am trying the SSL approach for which, I have a certificate.p12 file from which I generated cert.pem & key.pem and passed it to Net::Stomp::Client. I am using the Authen::Credential::x509 module for cert based authentication.
Now I am getting the following Error when I am trying to run the perl script:
DEBUG: .../IO/Socket/SSL.pm:605: socket not yet connected
DEBUG: .../IO/Socket/SSL.pm:607: socket connected
DEBUG: .../IO/Socket/SSL.pm:629: ssl handshake not started
DEBUG: .../IO/Socket/SSL.pm:662: using SNI with hostname mmx-nprd3-07.cisco.com
DEBUG: .../IO/Socket/SSL.pm:697: request OCSP stapling
DEBUG: .../IO/Socket/SSL.pm:787: ssl handshake done
# 2016/02/10-07:18:33 JmsClient.pl[6618]: connect to stomp+ssl://mmx-ntard-07.sammy.com:7222 ok: 160.**.1**.**2
Stomp Client Object Created
Connected to broker mmx-ntard-07.sammy.com (IP 1**.*8.**.**2), port 7222
# 2016/02/10-07:18:33 JmsClient.pl[6618]: Net::STOMP::Client=HASH(0x194df00)->connect()
# 2016/02/10-07:18:33 JmsClient.pl[6618]: encoding CONNECT frame
# 2016/02/10-07:18:33 JmsClient.pl[6618]: H accept-version:1.0,1.1,1.2
# 2016/02/10-07:18:33 JmsClient.pl[6618]: H host:mmx-ntard-07.sammy.com
# 2016/02/10-07:18:33 JmsClient.pl[6618]: sent 65 bytes
# 2016/02/10-07:18:33 JmsClient.pl[6618]: received 12 bytes
# 2016/02/10-07:18:34 JmsClient.pl[6618]: received 0 bytes
cannot sysread(): EOF
Below is the perl script :-
use Net::STOMP::Client;
use No::Worries::Log qw(*);
use IO::Socket::SSL;
use FindBin qw($Bin);
use Authen::Credential::x509;
$IO::Socket::SSL::DEBUG = 2;
log_filter("debug");
my $auth = Authen::Credential::x509->new(
# client certificate to present
cert => "$Bin/JmsCertificate/aix_jms_public_Cert.pem",
# # client private key
key => "$Bin/JmsCertificate/aix_jms_private_Key.pem",
pass => 'password'
);
my $stomp = Net::STOMP::Client->new(
uri => "stomp+ssl://mmx-ntard-07.sammy.com:7222",
auth => $auth,
debug => 'all',
);
print "\nStomp Client Object Created";
my $peer = $stomp->peer();
printf("\nConnected to broker %s (IP %s), port %d\n", $peer->host(), $peer->addr(), $peer->port());
$stomp->connect();
print "\nConnected\n";
my $sid = $stomp->uuid();
$stomp->subscribe(
destination => "/queue/test",
# we use the generated subscription id
id => $sid,
# we want a receipt on our SUBSCRIBE frame
receipt => $stomp->uuid(),
);
my $count = 0;
my $frame;
while ($count < 10) {
$frame = $stomp->wait_for_frames(timeout => 1);
if ($frame) {
if ($frame->command() eq "MESSAGE") {
$count++;
printf("received message %d with id %s\n",
$count, $frame->header("message-id"));
} else {
# this will catch the RECEIPT frame
printf("%s frame received\n", $frame->command());
}
} else {
print("waiting for messages...\n");
}
}
$stomp->unsubscribe(id => $sid);
$stomp->disconnect();
I am not able to figure out what is going on here. I saw couple of other posts too but they were for ActiveMQ. I'm not sure if that would make a difference.
I want to send an email using perl ,but when i execute the command as follows:
#./sendmail.sh "par1" "par2" "par3"
i got the error msg "connect to localhost failed (Connection refused) no (more) retries"
sendmail.sh:
/usr/bin/perl /code/sendmail.pl "$1" "$2" "$3";
sendmail.pl:
#!/usr/bin/perl -w
use Mail::Sendmail;
my $event1 = shift(#ARGV);
my $event2 = shift(#ARGV);
my $time = shift(#ARGV);
#my $info = shift(#ARGV);
my $datetime = `/bin/date "+20%y-%m-%d %H:%M:%S"`;
chomp $datetime;
$msg = "This is Monitor System speak:\n
The system discovers the events at $datetime.
Something may be abnormal, please check it. The detail is below:\n";
$msg = $msg."$event1 and $event2 at $time\n";
$msg = $msg."\n";
$msg = $msg."Any problem, check it from http://map_test.php\n\n\n";
$mail_subject = "Abnormal";
sendmail(
From => 'localhost',
To => 'test#mail.com',
Subject => $mail_subject,
Message => $msg,
);
Any help appreciated.
smtp stands for simple mail transfer protocol.
When you need to send an email your mail client needs to talk to an smtp server which will accept the message. Normally your internet service provider will provide an smtp host. If you look at your mail client it will need to have an smtp server configured to be able to send mail.
Ok so when you install the Mail::Sendmail module, it doesn't know what your smtp server will be. It is up to you to tell it. It provides a default of localhost which would often be true if your server is running a sendmail daemon.
The configuration of Mail::Sendmail is stored in a variable called
%Mail::Sendmail::mailcfg
You can change the value of the sendmail server using this snippet of code:
unshift #{$Mail::Sendmail::mailcfg{'smtp'}} , 'my.smtp.server';
You need to add this line of code to your script to set the smtp server.
It adds this server to an array which also includes localhost.
So if neither of the hosts work it will still print an error message about localhost which is slightly confusing.
If you use Data::Dumper to print the contents of the mailcfg variable it will look something like this:
#!/usr/bin/perl
use Mail::Sendmail;
use Data::Dumper;
unshift #{$Mail::Sendmail::mailcfg{'smtp'}} , 'my.smtp.server';
print Dumper(\%Mail::Sendmail::mailcfg);
Should return:
$VAR1 = {
'retries' => 1,
'smtp' => [
'my.smtp.server',
'localhost'
],
'delay' => 1,
'port' => 25,
'from' => '',
'debug' => 0,
'tz' => '',
'mime' => 1
};
I am using HTTP::Tiny + IO::Socket::Socks::Wrapper to send a HTTP Request over a SOCKS Proxy. Everything is working fine except the timeout option.
When using only IO::Socket::Socks without HTTP::Tiny the timeout is working.
Example without HTTP::Tiny and a non existent proxy to trigger a timeout:
my $t = time;
my $sock = IO::Socket::Socks->new(
ProxyAddr => '4.5.6.7',
ProxyPort => 1080,
ConnectAddr => 'www.google.com',
ConnectPort => 80,
Timeout => 3
) or print "connection failed or timed out\n";
print "time: " . (time - $t) . "\n";
Output:
connection failed or timed out
time: 3.00517201423645
Example with HTTP::Tiny:
my $t = time;
my $http = wrap_connection(
HTTP::Tiny->new(timeout => 3), {
ProxyAddr => '4.5.6.7',
ProxyPort => 1080,
Timeout => 3
}
);
my $r = $http->get("http://www.google.com");
print "connection failed or timed out\n" unless $r->{success};
print "time: " . (time - $t) . "\n";
Output:
connection failed or timed out
time: 21.0282030105591
Why does the second example not timeout after 3 seconds?
It was bug, which now seems to be fixed. New version will be uploaded to CPAN soon.
And right now you can get fixed version from github repo.