imap_open on server Aruba.it - imap-open

To receive mail I usually use:
imap_open('{mail.sito.com:143/notls}', $user, $pass);
I am having difficulty however when I have to connect to an email on Aruba server.
i tried with:
{mail.website.com:143}
{mail.website.com/pop3:110}
{pop3.website.com/pop3:110}
{imaps.aruba.it:993}INBOX
{imaps.aruba.it:993}
but it does not open the stream.
Do you know which is the right string to use?

You can use pop3 protocol with
$pop3 = "pop3.yourdomain.ext";
$port = 110;
$inbox = imap_open("{".$pop3.":".$port."/pop3/notls}INBOX", $username, $password);
......
imap_close($inbox);
but don't work if Imap is active in Aruba services.

After some tests it work :
$imap = "imaps.aruba.it";
$port = 993;
$inbox = imap_open("{".$imap.":".$port."/imap4/ssl}INBOX",$username, $password);
......
imap_close($inbox);
REMEMBER : you must activate imap extension in your php.ini

Related

How can I handle multiple outlook accounts using Perl

I have task to read mails from secondary mail account from outlook using perl.
Please check the following it may help you.But you should install respective modules.Reference from "http://www.perlmonks.org/?node_id=916759"
use Modern::Perl;
use Mail::POP3Client;
use MIME::QuotedPrint;
my $pop_user = 'XXXXXXXXXX';
my $pop_pass = 'XXXXXXXXXX';
my $pop_host = 'exchange3';
#connect to POP3 sever
my $pop = new Mail::POP3Client ( HOST => $pop_host );
$pop->User($pop_user);
$pop->Pass($pop_pass);
$pop->Connect()
or die "Unable to connect to POP3 server: ".$pop->Message()."\n";
#count number of items in POP3 mailbox
my $mailcount = $pop->Count();
for (my $i = 1; $i <= $mailcount ; $i++) {
my $header = $pop->Head($i); #gets the header
my $uni = $pop->Uidl($i); # gets the unquie id
my $body = $pop->Body($i);
$body = decode_qp($body); #decode quoted printable body
say "$uni";
say "$header\n";
say "$body";
}

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

Trying to send authenticated email using Net::SMTP::SSL

I'm attempting to send an authenticated email with the Net::SMTP::SSL module to a comcast email server. I'm using the following code.
#!/usr/bin/perl
use Net::SMTP::SSL;
use MIME::Base64;
$smtp = Net::SMTP::SSL->new
(
"smtp.comcast.net",
Hello => "host.comcast.net",
Port => 465,
Timeout => 30,
Debug => 1,
);
$smtp->datasend("AUTH LOGIN\n");
$smtp->response();
# Mailbox info
$smtp->datasend(encode_base64('username')); # username
$smtp->response();
$smtp->datasend(encode_base64('password')); # password
$smtp->response();
# Email from
$smtp->mail('user\#comcast.net');
# Email to
$smtp->to('user\#host.com');
$smtp->data();
$smtp->datasend("To: user\#host.com\n");
$smtp->datasend("From: user\#comcast.net\n");
$smtp->datasend("Subject: Test");
# Line break to separate headers from body
$smtp->datasend("\n");
$smtp->datasend("Blah\n");
$smtp->dataend();
$smtp->quit();
exit;
I'm basically following the code from here for comcast.
I've ran telnet and can connect to the smtp server on the port and I can issue the AUTH LOGIN and successfully login, but issuing the
$smtp->datasend("AUTH LOGIN\n");
always results in:
Can't call method "datasend" on an undefined value
I've also tried executing the auth method to login and that fails as well.
What am I missing here? I know it's something simple I'm overlooking.
Pretty much all of the Net::SMTP methods optionally set error codes, so I suspect that Net::SMTP::SSL does the same. Try the following for your constructor:
use Net::SMTP::SSL;
use MIME::Base64;
use strict;
use warnings;
my $smtp = Net::SMTP::SSL->new(
"smtp.comcast.net",
Hello => "host.comcast.net",
Port => 465,
Timeout => 30,
Debug => 1,
) or die "Failed to connect to mail server: $!";
Also, the fact that your smtp server is different than your Hello is a little suspect to me.
For email services that use STARTTLS, it's best to use the newer NET::SMTPS module. Try the following code:
my $msg = MIME::Lite ->new (
From => 'from#bellsouth.net',
To => 'to#bellsouth.net',
Subject => 'Test Message',
Data => 'This is a test',
Type => 'text/html'
);
my $USERNAME = 'from#bellsouth.net';
my $PASSWORD = 'abc123';
my $smtps = Net::SMTPS->new("smtp.mail.att.net", Port => 587, doSSL => 'starttls', SSL_version=>'TLSv1');
$smtps->auth ( $USERNAME, $PASSWORD ) or die("Could not authenticate with bellsouth.\n");
$smtps ->mail('from#bellsouth.net');
$smtps->to('to#bellsouth.net');
$smtps->data();
$smtps->datasend( $msg->as_string() );
$smtps->dataend();
$smtps->quit;
Originally from http://www.skipser.com/p/2/p/send-email-using-perl-via-live.com.html
Try this:
Email from
Change
$smtp->mail('user\#comcast.net'); to $smtp->mail('user#comcast.net'); without '\'
Email to
Change
$smtp->to('user\#host.com'); to $smtp->to('user#host.com'); without '\'

(perl Net::SMTP) authentication failure using gmail smtp server

I have written a perl script to send email using the gmail's smtp server: smtp.gmail.com -
use Net::SMTP;
my $smtp = Net::SMTP->new('smtp.gmail.com',
Port=> 587,
Timeout => 20,
Hello=>'user#gmail.com'
);
print $smtp->domain,"\n";
$sender = "user#gmail.com";
$password = "mypassword";
$smtp->auth ( $sender, $password ) or die "could not authenticate\n";
$receiver = "user#gmail.com";
$subject = "my custom subject";
$smtp->mail($sender);
$smtp->to($receiver);
$smtp->data();
$smtp->datasend("To: <$reciever> \n");
$smtp->datasend("From: <$sender> \n");
$smtp->datasend("Content-Type: text/html \n");
$smtp->datasend("Subject: $subject");
$smtp->datasend("\n");
$smtp->datasend('the body of the email');
$smtp->dataend();
$smtp->quit();
print "done\n\n";
For 'user', I have my gmail username and for 'mypassword' I have the password. But when I run this code it stops at the auth itself giving : could not authenticate.
I am able to connect to the smtp serever of google as I get : 'mx.google.com' as the result of :
print $smtp->domain,"\n";
What is it that I am doing wrong? Please help.
use Net::SMTP;
my $smtp = Net::SMTP->new ....
afaik you need to use an encrypted connection to send via gmail, use Net::SMTP::TLS or Net::SMTP::SSL (with port 465)
Hello=>'user#gmail.com'
'Hello' is not an email address, put your hostname there instead
$sender = "user#gmail.com";
...
$receiver = "user#gmail.com";
put these in single quotes
if you still get "could not authenticate." make sure you have the modules MIME::Base64 and Authen::SASL installed.
$smtp->datasend("To: <$reciever> \n");
should be $receiver, not $reciever
Gmail uses TLS/STARTTLS on port 587, SSL on port 465. Please follow Sending email through Google SMTP from Perl. Alternatively you could use Email::Send::SMTP::Gmail. I recommend this as it is way easier to use and has many more features.

"Can't call method "auth" on an undefined value at..." after many successful runs

i have a perl app that is supposed to send emails to a massive number of recipients. It seems to work ok, but after about 9K emails it fails with:
Can't call method "auth" on an undefined value at...
In the code I see:
# Open a connection to the SendGrid mail server
my $smtp = Net::SMTP->new('smtp.xyz.net', Port=> 25, Hello=>$DOMAIN);
# Authenticate
my $code = $smtp->auth($USERNAME, $PASSWORD);
The Net::SMTP constructor returns undef if there's a problem (e.g. it's unable to connect to port 25 on smtp.xyz.net). You aren't checking for that, and when you try to call a method on undef, you get the error message you mentioned.
my $smtp = Net::SMTP->new('smtp.xyz.net', Port=> 25, Hello=>$DOMAIN)
or die "Failed to open SMTP connection: $!";
may give you more information. (Although it's not necessarily a socket error, so $! may not contain anything useful.)
The Net::SMTP documentation says that when a method fails it returns undef. So I expect your method call failed.
You might be able to get more information by enabling the Debug => 1 flag in the Net::SMTP constructor.
You will want to detect that your method call failed, and possibly retry it after a short wait.
# Open a connection to the SendGrid mail server
my $smtp = Net::SMTP->new('smtp.xyz.net', Port=> 25, Hello=>$DOMAIN, Debug=>1);
die "Failed to make connection" unless ($smtp);
# Authenticate
my $code = $smtp->auth($USERNAME, $PASSWORD);
You could change it to retry in increasing intervals
something like this:
my $retry = 10; # in seconds;
my $smtp = Net::SMTP->new('smtp.xyz.net', Port=> 25, Hello=>$DOMAIN);
while (not defined $smtp) {
if ($retry > 300) {
die "could not connect to smtp server, giving up";
else {
print "could not connect to smtp, retrying in $retry seconds\n";
}
sleep ($retry);
$smtp = Net::SMTP->new('smtp.xyz.net', Port=> 25, Hello=>$DOMAIN);
$retry *= 2;
}
# Authenticate
my $code = $smtp->auth($USERNAME, $PASSWORD);
This happens mainly if you have the wrong mailbox. Check if smtp.xyz.net is the correct smtp mailbox or it could even be mail.xyz.net. That kind of error occurs if auth is not able to work with the value of 'host' given.