i have an error in website Eprints, cannot send test email in perl - perl

Connect failed :IO::Socket::INET: connect: timeout
at /usr/share/perl5/Net/SMTP/TLS.pm line 109.
here my code at 109
sub new {
my $pkg = shift;
my $host= shift;
my %args= #_;
$args{Host} = $host;
$args{Hello}= "localhost" if not $args{Hello};
# make the non-SSL socket that will later be
# transformed
$args{sock} = new IO::Socket::INET(
PeerAddr => $host,
PeerPort => $args{Port} || 25,
Proto => 'tcp',
Timeout => $args{Timeout} || 5)
or croak "Connect failed :$#\n";

Your outgoing connections to port 25 may be blocked (firewalled) to prevent outgoing spam.
Do you get smtp greeting message when you telnet smtp port on the host from command line?
telnet hostname.domainname 25
Does the smtp server handle MSP (587) port? [port for message submission via smtp protocol]

Related

How to connect outlook IMAP server go through proxy server uisng perl

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)

Unable to connect to outlook.office365.com for processing emails with Perl Module Mail::IMAPClient

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.

Cannot sysread (): EOF Error while connecting to Tibco JMS Topic using Net:: STOMP:: Client

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.

connect to localhost failed (Connection refused) no (more) retries

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
};

Perl Mail::Sendmail cant connect to SMTP server

I'm trying to send an email from a perl script but I'm having a bad time connecting to my SMTP server. I'm using MailCatcher- https://github.com/sj26/mailcatcher/ - running with it's default settings (localhost:1025).
MailCatcher is running and I can send mail to it with other services. Any clues as to what could be wrong? (Also tried sending a mail with Net::SMTP but that also failed to connect)
print "Testing Mail::Sendmail version $Mail::Sendmail::VERSION\n";
my %mail = (
To => 'temp#test.com',
From => 'test#test.com',
Subject => 'Test message'
);
$mail{server} = 'localhost:1025';
$mail{'mESSaGE : '} = "The message key looks terrible, but works.";
print Dumper(%mail);
if (sendmail %mail) { print "Mail sent OK.\n" }
else { print "Error sending mail: $Mail::Sendmail::error \n" }
Output
Testing Mail::Sendmail version 0.79
Default server: localhost
Default sender:
$VAR1 = 'Subject';
$VAR2 = 'Test message';
$VAR3 = 'server';
$VAR4 = 'localhost:1025';
$VAR5 = 'To';
$VAR6 = 'temp#test.com';
$VAR7 = 'message : ';
$VAR8 = 'The message key looks terrible, but works.';
$VAR9 = 'From';
$VAR10 = 'test#test.com';
Error sending mail: connect to localhost failed (Connection refused)
connect to localhost failed
connect to localhost failed (Connection refused)
connect to localhost failed
connect to localhost failed (Connection refused) no (more) retries!
EDIT:
Turned out mailcatcher was running on another port and wasn't giving an error when I was trying to start it up on 25.
You don't set the mail server and port in a header within the email; rather you set it in the %mailcfg hash:
use Mail::Sendmail qw(sendmail %mailcfg)
$mailcfg{port} = 1025; #localhost is already the default server...
You can try this one .. it worked for me ..
use strict;
use warnings;
use Email::Sender::Simple qw(sendmail);
use Email::Sender::Transport::SMTP();
use Email::Simple();
use Email::Simple::Creator();
my $smtpserver = 'server';
my $smtpport = 25;
my $smtpuser = 'User_Name';
my $smtppassword = 'pass';
my $transport = Email::Sender::Transport::SMTP->new({
host => $smtpserver,
port => $smtpport,
sasl_username => $smtpuser,
sasl_password => $smtppassword,
});
my $email = Email::Simple->create(header => [
To => 'me#example.com',
From => 'sender#example.com',
Subject => 'Hello there!',
], body => "This is one actually worked !!\n",
);
sendmail($email, { transport => $transport });
?>