Perl IMAP connection to Exchange using Mail::IMAPClient? - perl

I keep getting a NO LOGIN failed message when trying to connect to the Exchange server using Mail::IMAPClient. From what I've read everything should work. I've gotten it to connect to Gmail but trying to connect to MS Exchange is more difficult it seems like. What could be causing it to fail on authenticating?
use strict;
use warnings;
use Authen::NTLM;
use Mail::IMAPClient;
## Option variables
my $debug
my $authmech = "NTLM";
my $username = "useraccount";
my $password = "set by prompt;
## Settings for connecting to IMAP server
my $imap = Mail::IMAPClient->new(
Server => 'mail.server.domain',
User => $username,
Password => $password,
Port => 993,
Ssl => 1,
Authmechanism => $authmech,
Debug => 1
) or die "Cannot connect through IMAPClient: $#\n";
The out put from running the script.
~]./status_page_msg.pl -d
Logging in as : user_account
Started at Sat Nov 12 19:20:11 2011
Using Mail::IMAPClient version 3.29 on perl 5.008008
Connecting via IO::Socket::SSL to mail.server.domain:993 Timeout 600
Connected to mail.server.domain
Read: * OK The Microsoft Exchange IMAP4 service is ready - 'serverName'
Sending: 1 AUTHENTICATE NTLM
Sent 21 bytes
Read: +
Sending: TlRMTVNTUAABAAAAB6IAAAoACgAgAAAAAAAAAAoAAABlYW0tc3RhdHVz
Sent 58 bytes
Read: 1 NO AUTHENTICATE failed.
ERROR: 1 NO AUTHENTICATE failed. at /usr/lib/perl5/site_perl/5.8.8/Mail/IMAPClient.pm line 3047
Mail::IMAPClient::authenticate('Mail::IMAPClient=HASH(0x1ac95440)', 'NTLM', 'undef') called at /usr/lib/perl5/site_perl/5.8.8/Mail/IMAPClient.pm line 443
Mail::IMAPClient::login('Mail::IMAPClient=HASH(0x1ac95440)') called at /usr/lib/perl5/site_perl/5.8.8/Mail/IMAPClient.pm line 395
Mail::IMAPClient::Socket('Mail::IMAPClient=HASH(0x1ac95440)', 'IO::Socket::SSL=GLOB(0x1b43e110)') called at /usr/lib/perl5/site_perl/5.8.8/Mail/IMAPClient.pm line 351
Mail::IMAPClient::connect('Mail::IMAPClient=HASH(0x1ac95440)') called at /usr/lib/perl5/site_perl/5.8.8/Mail/IMAPClient.pm line 307
Mail::IMAPClient::new('Mail::IMAPClient', 'Server', 'mail.server.domain', 'User', 'user_account', 'Password', 'Correct', 'Port', 993, ...) called at ./status_page_msg.pl line 63
Cannot connect through IMAPClient: 1 NO AUTHENTICATE failed.

You should have said that it used to work, but stopped working when the server was upgraded to Exchange 2010.
A quick Google for "Exchange 2010 NTLM IMAP" turned up Discontinued Features from Exchange 2007 to Exchange 2010:
NTLM isn't supported for POP3 or IMAP4 client connectivity in Exchange 2010 RTM. Connections from POP3 or IMAP4 client programs using NTLM will fail. If you are running Exchange 2010 RTM, the recommended POP3 and IMAP4 setting alternatives to NTLM are:
Kerberos (GSSAPI)
Plain Text Authentication with SSL
If you are using Exchange 2010 RTM, to use NTLM, you must retain an Exchange 2003 or Exchange 2007 server in your Exchange 2010 organization.
Support for NTLM authentication for POP3 and IMAP4 connectivity has been brought back in Exchange 2010 SP1.
Since you're using SSL, you should be able to switch to plain text authentication (just delete Authmechanism). Or, get your sysadmins to install SP1.

Related

Not able to configure Bugzilla for sending emails from intranet SMTP server

I am setting up a Bugzilla server. I am getting following error when I submit email delivery method description on Bugzilla page during configuration.
The new value for smtpserver is invalid: Cannot connect to 10.71.2.16.
Email delivery method description
mail_delivery_method: SMTP
mailfrom: bugzilla#mydomain.com
use_mailer_queue: off
smtpserver: 10.71.2.16
(I have also tried putting domain name, all ports such as 25, 465, 587)
smtp_username: bugzilla#mydomain.com
smtp_password: <password for user bugzilla#mydomain.com>
smtp_ssl: I have tried On and Off both but no sucsess
smtp_debug: On (but not sure where it generates logs.
Checked /var/log/apache2/. But nothing over there.)
My environment
Ubuntu: 16.04 Server (Internal Server IP: 10.71.6.45)
Bugzilla: 5.0.3 (Internal Server IP: 10.71.6.45)
SMTP: Zimbra Release 8.0.6_GA_5922.RHEL6_64 RHEL6_64 FOSS edition.
(Internal Server IP: 10.71.2.16)
This Zimbra is hosted on a remote server (10.71.2.16) in Intranet.
It is sending and receiving emails successfully from long time without any issues.
/var/log/zimbra.log on (server 10.71.2.16 where SMTP is hosted)
Dec 1 00:25:22 email postfix/smtps/smtpd[18064]: connect from unknown[10.71.6.45]
Dec 1 00:25:22 email postfix/smtps/smtpd[18064]: SSL_accept error from unknown[10.71.6.45]: lost connection
Dec 1 00:25:22 email postfix/smtps/smtpd[18064]: lost connection after CONNECT from unknown[10.71.6.45]
Dec 1 00:25:22 email postfix/smtps/smtpd[18064]: disconnect from unknown[10.71.6.45]
/var/log/maillog (on server 10.71.2.16 where SMTP is hosted)
Dec 1 00:24:36 email postfix/smtps/smtpd[18064]: connect from unknown[10.71.6.45]
Dec 1 00:24:36 email postfix/smtps/smtpd[18064]: SSL_accept error from unknown[10.71.6.45]: lost connection
Dec 1 00:24:36 email postfix/smtps/smtpd[18064]: lost connection after CONNECT from unknown[10.71.6.45]
Dec 1 00:24:36 email postfix/smtps/smtpd[18064]: disconnect from unknown[10.71.6.45]
However when I am sending email from command prompt with below command emails are getting delivered
echo "This is Email Body. This email is sent from Ubuntu Server 10.71.6.45 using SMTP server 10.71.2.16" | mailx -v -r "bugzilla#mydomain.com" -s "Bugzilla test mail - This is the subject" -S smtp="10.71.2.16" -S smtp-use-starttls -S smtp-auth=login -S smtp-auth-user="bugzilla#mydomain.com" -S smtp-auth-password="bugzilla#mydomain.com-emailid-password" -S ssl-verify=ignore test#mydomain.com
/var/www/html/bugzilla/Bugzilla/Mailer.pm snippet
if ($method eq "SMTP") {
my ($host, $port) = split(/:/, Bugzilla->params->{'smtpserver'}, 2);
$transport = Bugzilla->request_cache->{smtp} //=
Email::Sender::Transport::SMTP::Persistent->new({
host => $host,
defined($port) ? (port => $port) : (),
sasl_username => Bugzilla->params->{'smtp_username'},
sasl_password => Bugzilla->params->{'smtp_password'},
helo => $hostname,
ssl => Bugzilla->params->{'smtp_ssl'},
debug => Bugzilla->params->{'smtp_debug'} });
}
I have tried all possible combinations. But it always give following error in Bugzilla
The new value for smtpserver is invalid: Cannot connect to 10.71.2.16.
What is that which I am doing wrong or am I missing?

Connecting to Gmail SMTP via socket returns different responses per server

I am trying to understand why opening a socket to Gmail's server via SSL provides different responses for different servers. This is stopping me from connecting to Gmail for sending out emails.
Below is a very simple script:
<?php
$smtp_connect = fsockopen('ssl://smtp.googlemail.com', 465,
$errno,
$errstr,
300);
var_dump(fgets($smtp_connect, 512));
?>
On server 1 [no ssl cert installed], the output of the above code is: string(57) "220 smtp.googlemail.com ESMTP s89sm726209qkl.44 - gsmtp
"
On server 2 [ssl cert installed], the output of the above code is: string(71) "220-mycompany.pro.com ESMTP Exim 4.87 #1 Wed, 26 Oct 2016 07:42:49 -0400
"
Can anyone explain why this happens, and how can I make server 2 have the same behavior as server 1?
Update: I have also tried smtp.gmail.com, the output is same as using smtp.googlemail.com.
The line you get the the SMTP welcome message. It is perfectly normal that different servers give a different welcome message. It is even normal that the response to commands you send is different. This means that when communicating with an SMTP server you should not assume fixed strings as response but implement proper parsing of SMTP. See the SMTP standard (RFC 2821) for details about this protocol.

Connecting to Microsoft Exchange IMAP4 service using perl Mail::IMAPClient

I am trying to use Mail::IMAPClient to connect to a MS Exchange mailbox.
Read messages in the INBOX and depending on the contents formats and sends email to other mailboxes.
This snippet program is a test to only connect using IMAP and then disconnect. The snippet fails to connect, but I can connect using openssl.
!/usr/bin/perl
use strict;
use warnings;
use Mail::IMAPClient;
use lib "/usr/local/share/perl5";
my $IMAPserver = "imapx.servers.net";
my $IMAPuser = "emailUser";
my $IMAPpass = "resUliame";
my $IMAPport = 993;
print "Start\n";
# Attach a client using an SSL socket.
my $client = Mail::IMAPClient->new(
Server => $IMAPserver,
Port => $IMAPport,
User => $IMAPuser,
Password => $IMAPpass,
Ssl => 1,
DEBUG => 1
)
or die "new(): $#";
$client->State(Mail::IMAPClient::Connected);
# Close IMAP connections
$client->logout();
print "Done\n";
This is the output I get...
Start
Started at Mon Sep 26 10:28:16 2016
Using Mail::IMAPClient version 3.38 on perl 5.010001
Connecting with IO::Socket::SSL PeerAddr imapx.servers.net PeerPort 993 Proto tcp Timeout 600 Debug 1
ERROR: Unable to connect to imapx.servers.net: at /usr/local/share/perl5/Mail/IMAPClient.pm line 370.
Mail::IMAPClient::connect(Mail::IMAPClient=HASH(0x2064b98)) called at /usr/local/share/perl5/Mail/IMAPClient.pm line 313
Mail::IMAPClient::new("Mail::IMAPClient", "Server", "imapx.servers.net", "Port", 993, "User", "emailUser", "Password", "resUliame", ...) called at ./testEmail.pl line 17
new(): Unable to connect to imapx.servers.net: at ./testEmail.pl line 17
I can connect with openssl so I know the server, port, user & passwd is correct.
# openssl s_client -connect imapx.servers.net:993 -crlf
CONNECTED(00000003)
* OK The Microsoft Exchange IMAP4 service is ready.
* LOGIN emailUser resUliame
* OK LOGIN completed.
* SELECT Inbox
* 28027 EXISTS
* 1 RECENT
* FLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)
* OK [PERMANENTFLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)] Permanent flags
* OK [UNSEEN 27544] Is the first unseen message
* OK [UIDVALIDITY 64268] UIDVALIDITY value
* OK [UIDNEXT 46567390] The next unique identifier value
* OK [READ-WRITE] SELECT completed.

SwiftMailer connection established error

I have problem with sending mails. I using SwiftMailer 5.1.0 and account on gmail, smtp port 465 and openssl is enable, but I have this error:
Serwer: smtp.gmail.com:465 ssl
From: BizIn - system mailowy <isystemnew.pcet#gmail.com>
To: test7771#test.pl
Mail debug: Connection could not be established with host smtp.gmail.com [ #0]
At my localhost everything is okey and mails are send. But on serwer I have error.
Localhost using PHP in version 5.4.31, but at server is 5.6.0.
The fix here solved it for me: https://github.com/swiftmailer/swiftmailer/issues/544
#if-joerch
if-joerch commented on Nov 3, 2014
If you are using PHP 5.6, the error does occur because of the "SSL
context options" used for the stream context in swiftmailer. IN PHP
5.6 verify_peer and verify_peer_name the default was set to TRUE, so PHP checks the SSL certificate. It is currently not possible to
disable it in swiftmailer using some options.
You could disable the SSL check by modifying the function
"_establishSocketConnection" in StreamBuffer.php. Add these lines
before stream_socket_client command:
$options['ssl']['verify_peer'] = FALSE;
$options['ssl']['verify_peer_name'] = FALSE;
It would be great if these options could be set without hacking the
code.

Postfix & Rails 3.0 ActionMailer: lost connection after STARTTLS

I'm using Ruby 1.9.2 and Rails 3.0.4 in development mode and I'm trying to configure it to send emails from the Postfix server installed on the same box (Running Ubuntu 10.04 with the dovecot-postfix package installed) Whenever I attempt to send an email from Rails, it goes through cleanly in Rails but displays an error in the Postfix logs (I've removed domains and IPs from the excerpt below):
Feb 21 04:49:16 alpha postfix/smtpd[9060]: connect from alpha.mydomain.com[xxx.xxx.xxx.xxx]
Feb 21 04:49:16 alpha postfix/smtpd[9060]: lost connection after STARTTLS from alpha.mydomain.com[xxx.xxx.xxx.xxx]
Feb 21 04:49:16 alpha postfix/smtpd[9060]: disconnect from alpha.mydomain.com[xxx.xxx.xxx.xxx]
The strange part is that when I connect to the SMTP server from an email client like Thunderbird, it works with no problems.
I know development mode sometimes doesn't allow for sending emails so I added the following to the environments/development.rb file:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
#load mail server settings
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "mail.mydomain.com",
:port => 587,
:domain => 'mydomain.com',
:user_name => 'username',
:password => 'password',
:authentication => 'plain',
:tls => true,
:enable_starttls_auto => true }
If you're just sending email from localhost, then you don't need SMTP and all the authentication issues that come with it. You can simply deliver with sendmail directly.
config.action_mailer.delivery_method = :sendmail
Is it possible that your certificate is not valid, and that Thunderbird falls back to plain SMTP when sending emails? Try disabling TLS in your config.