CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
Unable to send Email using Office365.

 



patro_ramesh
New User

Nov 11, 2015, 1:38 AM

Post #1 of 6 (8831 views)
Unable to send Email using Office365. Can't Post

#--------------------------- #!C:\perl\bin\perl.exe -w

Code
#!/usr/bin/perl  -w 

$| = 1;

#use lib '/apps/sybmon/gem/lib' ;

use strict;
use DBI;
use warnings;
use diagnostics ;
use Cwd ;
use File::Copy ;
use File::Basename;
use Net::SMTP;
use MIME::Lite;
#use Net::FTP;
#use Net::SMTP::SSL;

#------------ Connection related and data variables

use vars qw( $opt_S $opt_U $opt_P $dvndb ) ;
use vars qw( $data @data1 @param $row_data $outfile $test $para1 @newparm );

#------------ MAIL/SMS variables

use vars qw( $from_address $to $cc $cc1 $mail_host $subject $message_body $msg $senderr @attach $sendOK $message1 $user $pass) ;


$from_address = 'gtlcomp@gulftainer.com';


$to = 'pramesh@gulftainer.com,ramesh.daini@gmail.com';
#$cc = 'gjeeni@gulftainer.com,pramesh@gulftainer.com,ageorge@gulftainer.com,dangus@gulftainer.com';

$mail_host = 'smtp.office365.com';

$user = 'gtlcomp@gulftainer.com' ;
$pass = '0f1GHDTL';

my $smtpport = 587;

#------------ Processing and Error capture Variables

use vars qw($curdir $file_header $logfile $errorfile @procERR ) ;

#------------------------------------------- Initialise Global variables---------------------------------------------------

$opt_S = 'GTLDEVSERVER' ;
$opt_U = 'sa' ;
$opt_P = 'sybase';
$dvndb = 'sctdb' ;

$curdir = cwd ;

$row_data = 'inspdet.txt';

opendir(MYDIR, $curdir) || die "Cannot open directory: $curdir \n $!";
my @copyfiles = (readdir MYDIR);
closedir(MYDIR) ;

foreach (@copyfiles) {
next if (m/^\.+/) ;
unlink($_) if (m/\.txt$/) ;
}


my($dbh)= DBI->connect("dbi:Sybase:server=$opt_S",$opt_U,$opt_P);

my($firstdt,$wday, $row, $todaydt ) ;

# finding today's date
$firstdt = $dbh -> prepare("select convert(char(10),getdate(),103)") ;
$firstdt -> execute();
$todaydt = $firstdt -> fetchrow();
print "$todaydt\n" ;

#----------------------------Program will run every sunday -----------------------------------------------------

$wday = $dbh -> prepare("select datepart(dw,getdate())") ;
$wday -> execute();
$row = $wday -> fetchrow();

push(@param,'ID');

#if($row == 2 ) {
# push(@param,'ID');
#}

#---------------------------------- Expire file creation ---------------------------------------------
foreach $para1 (@param) {

if ($para1 eq "ID"){
$outfile = 'inspout.txt';
$subject = "Inspection Details Generated on: $todaydt ";
&filecreate();

}

}

#------------------------------- subroutine for file creation ---------------------------------------------------
sub filecreate {
open (DAT, ">$row_data") ;

if( defined $dbh ) {

my $sth = $dbh->prepare("select isnull(cust_id,'-'),isnull(substring(cust_name,1,30),'-'),isnull(substring(vessel_name,1,30),'-'),isnull(substring(voyno,1,30),'-'),convert(char(10),log_date,103) from sctdb..sct_insp_details where gt_status = 1");
$sth->execute ;
do {
while(@data1 = $sth->fetchrow_array) {
my($datarow) = join('ZZZ',@data1) ;
print DAT join('ZZZ',$datarow),"\n";

}
} while($sth->{syb_more_results});
}

close DAT ;
&format_file () ;
&message_bodynew();
&sendmsgbymail() ;
}
#--------------------------------------- subroutine for message body -----------------------------------------------
sub message_bodynew {
$message_body = '';
open (DAT, "<$outfile") or die "unable to open file $row_data\n";
while(<DAT>)
{
# Good practice to store $_ value because subsequent operations may change it.
my($line) = $_;
# Good practice to always strip the trailing newline from the line.
chomp($line);
# Convert the line to upper case.
$line =~ tr/[a-z]/[A-Z]/;
# Print the line to the screen and add a newline
# print "$line\n";
$message_body = "$message_body" . "$line\n" ;

}
close DAT ;
}

#------------------------------- subroutine for file creation ---------------------------------------------------
sub format_file {

open (DAT, "<$row_data") or die "unable to open file $row_data\n";
open (OUT, ">$outfile");
print (OUT "CustID CustomerName Vessel_Name Voyageno Log Date \n") ;
print (OUT "===============================================================================================================\n") ;

while( <DAT> ) {

chomp;
chomp;
s/\s+$//;
next if /^\s*$/; # SKIP# Remove trailing spaces/tabs blank lines
next if /^#/; # Skip Remarks
next if $_ eq '0' ; # Skip line containing return value.
#print "******* $_\n" ;

my ($custid, $custname, $vesselname, $voyageno, $logdate ) =split('ZZZ',$_) if defined $_;
#$days = ('00'.$days);
#$days = substr($days,length($days) - 2,length($days));

#print "$fileno $comp_no $refno $fmdt $todt $days $adddt $emp_name\n" ;


format OUT =
#Custid Cust_Name Vessel_name Voyno log_date
@<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<
$custid, $custname, $vesselname, $voyageno, $logdate
.
write OUT;

}

print (OUT "===============================================================================================================\n") ;
print (OUT "THIS IS AN AUTO-GENERATED MESSAGE - PLEASE DO NOT REPLY TO THIS MESSAGE\n");
close DAT ;
close OUT ;
}

#----------------------------- E-mail subroutine start here -------------------------------------------
sub sendmsgbymail {

# $msg $senderr @attach $sendOK

undef $senderr ;

if ( defined $cc ) {

$msg = MIME::Lite->new (
From => $from_address,
To => $to,
Cc => $cc,
Subject => $subject,
Type =>'multipart/mixed'
) or die "Error creating multipart container: $!\n";

} else {

$msg = MIME::Lite->new (
From => $from_address,
To => $to,
Subject => $subject,
Type =>'multipart/mixed'
) or die "Error creating multipart container: $!\n";

}

$msg->attach (
Type => 'TEXT',
Data => $message_body
) or die "Error adding the text message part: $!\n";


#---------------------- Send the email -------------------------------


MIME::Lite->send('smtp',$mail_host,AuthUser=>$user, AuthPass=>$pass, SSL=>0, Port=>$smtpport, Timeout=>30,Debug => 1 ) ;

undef $@;
eval { $msg->send } ;

if ($@) {
$senderr=1 ;
undef $sendOK ;

} else { $sendOK = 1 ; }

}

#---------------------------------------------end of file ------------------------------------------------

1;



(This post was edited by FishMonger on Nov 11, 2015, 6:07 AM)


Laurent_R
Veteran / Moderator

Nov 11, 2015, 3:58 AM

Post #2 of 6 (8829 views)
Re: [patro_ramesh] Unable to send Email using Office365. [In reply to] Can't Post

Hm, it is nice to provide the code, but perhaps you could also say in which respect it does not work according to your expectations. Does it compile? Does it do anything? Do you get any warning or error message?


FishMonger
Veteran / Moderator

Nov 11, 2015, 6:30 AM

Post #3 of 6 (8821 views)
Re: [patro_ramesh] Unable to send Email using Office365. [In reply to] Can't Post

What does Office365 have to do with your code?

Are you asking us to critique your code?

What is your question?

EDIT:
Why did you post this in the Advanced section? Based on your code, this should be in the Beginner section.


(This post was edited by FishMonger on Nov 11, 2015, 6:57 AM)


patro_ramesh
New User

Nov 11, 2015, 8:08 PM

Post #4 of 6 (8807 views)
Re: [Laurent_R] Unable to send Email using Office365. [In reply to] Can't Post

Hi ,

Below error message receiving while compile

C:\Perl>perl -w c:\plalerts\inspdetails.pl
12/11/2015
MIME::Lite::SMTP>>> MIME::Lite::SMTP
MIME::Lite::SMTP>>> Net::SMTP(2.29)
MIME::Lite::SMTP>>> Net::Cmd(2.26)
MIME::Lite::SMTP>>> Exporter(5.58)
MIME::Lite::SMTP>>> IO::Socket::INET(1.29)
MIME::Lite::SMTP>>> IO::Socket(1.29)
MIME::Lite::SMTP>>> IO::Handle(1.25)
MIME::Lite::SMTP=GLOB(0x3817a48)<<< 220 HE1PR03CA0026.outlook.office365.com Micr
osoft ESMTP MAIL Service ready at Thu, 12 Nov 2015 04:00:57 +0000
MIME::Lite::SMTP=GLOB(0x3817a48)>>> EHLO localhost.localdomain
MIME::Lite::SMTP=GLOB(0x3817a48)<<< 250-HE1PR03CA0026.outlook.office365.com Hell
o [83.110.103.204]
MIME::Lite::SMTP=GLOB(0x3817a48)<<< 250-SIZE 157286400
MIME::Lite::SMTP=GLOB(0x3817a48)<<< 250-PIPELINING
MIME::Lite::SMTP=GLOB(0x3817a48)<<< 250-DSN
MIME::Lite::SMTP=GLOB(0x3817a48)<<< 250-ENHANCEDSTATUSCODES
MIME::Lite::SMTP=GLOB(0x3817a48)<<< 250-STARTTLS
MIME::Lite::SMTP=GLOB(0x3817a48)<<< 250-8BITMIME
MIME::Lite::SMTP=GLOB(0x3817a48)<<< 250-BINARYMIME
MIME::Lite::SMTP=GLOB(0x3817a48)<<< 250 CHUNKING
MIME::Lite::SMTP=GLOB(0x3817a48)>>> MAIL FROM:<gtlcomp@gulftainer.com>
MIME::Lite::SMTP=GLOB(0x3817a48)<<< 530 5.7.57 SMTP; Client was not authenticate
d to send anonymous mail during MAIL FROM

C:\Perl>


patro_ramesh
New User

Nov 11, 2015, 8:22 PM

Post #5 of 6 (8806 views)
Re: [FishMonger] Unable to send Email using Office365. [In reply to] Can't Post

Hi ,

Good day

While trying to send email using script mail host:

$mail_host = 'smtp.office365.com';

Emails are not delivered, I hope it needs SSL authentication
required.

Not sure below line is correct or not in my script.

MIME::Lite->send('smtp',$mail_host,AuthUser=>$user, AuthPass=>$pass, SSL=>0, Port=>$smtpport, Timeout=>30,Debug => 1 ) ;


Thanks
Ramesh Patro


FishMonger
Veteran / Moderator

Nov 12, 2015, 6:37 AM

Post #6 of 6 (8783 views)
Re: [patro_ramesh] Unable to send Email using Office365. [In reply to] Can't Post

Quoted from the module's documentation

Quote
WAIT!

MIME::Lite is not recommended by its current maintainer. There are a number of alternatives, like Email::MIME or MIME::Entity and Email::Sender, which you should probably use instead. MIME::Lite continues to accrue weird bug reports, and it is not receiving a large amount of refactoring due to the availability of better alternatives. Please consider using something else.




Quote
MIME::Lite::SMTP=GLOB(0x3817a48)>>> MAIL FROM:<gtlcomp@gulftainer.com>
MIME::Lite::SMTP=GLOB(0x3817a48)<<< 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM


It looks like smtp.office365.com is not allowing anonymous mail relay from the gulftainer.com domain, which is smart because it would open them up to be a spam rely.

Do you have an email account on the office365.com domain?

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives