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: Beginner:
Contents in email body

 



tedlaraghu
New User

Aug 23, 2011, 1:18 AM

Post #1 of 7 (1083 views)
Contents in email body Can't Post

Hi all,

In my perl script, I want to send log file contents as body of email, contents are sent to body of email, but without any new lines.

Ex :- if log file has something like

7:36:21 INFO: logfile = /home/etdbdid/sbcimp/dyn/logfiles/ETDIT/ETDCPP/autosys_DEV-23-08-2011-0736.log

07:36:21 INFO: Checking for autosys jobs which are in RU or FA status in xldn4615vdap(DEV)

07:36:21 INFO: If any job (Except DEVB_POS)is in RU for more than 2 hrs or in FA status

07:36:21 INFO: Investigate why job is in RU or FA status



In email it comes as

07:34:35 INFO: logfile = /home/etdbdid/sbcimp/dyn/logfiles/ETDIT/ETDCPP/autosys_DEV-23-08-2011-0734.log 07:34:35 INFO: Checking for autosys jobs which are in RU or FA status in xldn4615vdap(DEV) 07:34:35 INFO: If any job (Except DEVB_POS)is in RU for more than 2 hrs or in FA status 07:34:35 INFO: Investigate why job is in RU or FA status

Please find attached perl script, I am using.

Any help is appreciated.

Thanks

Raghu
Attachments: autosys_pending_jobs_dev.pl (2.17 KB)


rovf
Veteran

Aug 23, 2011, 5:17 AM

Post #2 of 7 (1074 views)
Re: [tedlaraghu] Contents in email body [In reply to] Can't Post

Are you sure that the newlines are REALLY missing in the mail, or that they are only an artefact of some buggy mail reader?

With other words, did you look at the raw (internet) format of the incoming mail?

Ronald


tedlaraghu
New User

Aug 23, 2011, 5:48 AM

Post #3 of 7 (1072 views)
Re: [tedlaraghu] Contents in email body [In reply to] Can't Post

Hi Rovf,

I have modified SendMail sub-routine, with this change email body is same as log file.

<< With other words, did you look at the raw (internet) format of the incoming mail? >>

How to find raw format ?

sub SendMail {
Log 1, "Sending E-Mail .... \n", $logfile;

my $msg = MIME::Lite->new
(
Subject => $subject,
From => $from,
To => $to,
Type => 'multipart/Mixed',
);
$msg->attach
(
Type => 'Text',
Data => $body
);

$msg->send();
Log 1, "E-Mail sent successfully to $from", $logfile;
}


Thanks

Raghu


(This post was edited by tedlaraghu on Aug 23, 2011, 5:50 AM)


tedlaraghu
New User

Aug 25, 2011, 6:18 AM

Post #4 of 7 (1053 views)
Re: [tedlaraghu] Contents in email body [In reply to] Can't Post

In my email body few of the lines from log file is not indented.

In log file, autorep command results come in different line, but in email, for few of autosys command results come in one line.

Log file appers as below email body is provided in attachment

********************** *******************

EQEtd_CPP_LN_DEVB_ACC 08/25/2011 12:30 ----- RU 111814534/1
EQEtd_CPP_LN_DEVC_GACCAUS 08/25/2011 13:03 ----- RU 111814534/4
EQEtd_CPP_LN_DEVB_RACCGLBLRV 08/25/2011 07:55 08/25/2011 08:14 FA 111796551/1 1
EQEtd_CPP_LN_DEVC_RACC_GLBLRV 08/25/2011 08:07 08/25/2011 08:07 FA 111796551/1 1
EQEtd_CPP_LN_DEVB_RACCAUSTRV 08/25/2011 12:40 ----- RU 111815205/1
EQEtd_CPP_LN_DEVB_POSC 08/25/2011 12:00 ----- RU 111812541/1
EQEtd_CPP_LN_DEVC_GPOSC 08/25/2011 13:07 ----- RU 111812541/7
EQEtd_CPP_LN_DEVB_RRVPOSAUDM 08/25/2011 13:00 ----- RU 111816439/1
EQEtd_CPP_LN_DEVB_RACCGLBL 08/25/2011 07:55 08/25/2011 08:14 FA 111796550/1 1
EQEtd_CPP_LN_DEVC_RACC_GLBL 08/25/2011 08:07 08/25/2011 08:07 FA 111796550/1 1
EQEtd_CPP_LN_DEVB_TDBAL_GLBL 08/25/2011 07:56 08/25/2011 08:14 FA 111796674/1 1
EQEtd_CPP_LN_DEVC_RGLBLTD 08/25/2011 08:07 08/25/2011 08:08 FA 111796674/1 1
EQEtd_CPP_LN_DEVB_TDBAL_AUST 08/25/2011 01:32 08/25/2011 02:00 FA 111793740/1 1
EQEtd_CPP_LN_DEVC_RAUSTTD 08/25/2011 01:35 08/25/2011 01:35 FA 111793740/1 1
EQEtd_CPP_LN_DEVB_RPOSAUSDOM 08/25/2011 01:10 08/25/2011 01:28 FA 111792030/1 1
EQEtd_CPP_LN_DEVC_RPOSAUSDOM 08/25/2011 01:20 08/25/2011 01:21 FA 111792030/1 1
EQEtd_CPP_LN_DEVB_AUSMEMOBAL 08/25/2011 13:00 ----- RU 111816438/1
EQEtd_CPP_LN_DEVC_GMDOMFILE 08/25/2011 13:11 ----- RU 111816438/2
Attachments: Checking for autosys jobs which are in RU or FA status in xldn4615vdap(DEV).txt (1.52 KB)


FishMonger
Veteran / Moderator

Aug 25, 2011, 4:58 PM

Post #5 of 7 (1037 views)
Re: [tedlaraghu] Contents in email body [In reply to] Can't Post

Please use the code and quote tags when posting code and the email text. The rendered html is "squashing" the multiple spaces which are still there if you view the source of the web page.

I see at least 10 problems with your code. Most of the problems relate to questionable coding practices. One of those problems which might be connected with the issue you're reporting is the useless use of cat. Perl has multiple ways of retrieving the contents of the file without having to resort to using cat in backticks.

For example:

Code
use File::Slurp; 
my $body = read_file( $logfile );


Connected to the useless use of cat issue is that you previously opened that file in append mode and wrote to it, but didn't close the file before using cat to retrieve its contents. Depending on the system you're using, the retrieved contents may or may not include the appended data.


tedlaraghu
New User

Aug 27, 2011, 3:23 AM

Post #6 of 7 (1016 views)
Re: [FishMonger] Contents in email body [In reply to] Can't Post

I am new to Perl programming, I did not get a chance to try
use File::Slurp;

What are the other problems you see in the code ?
It will help me in coding in a better way.


FishMonger
Veteran / Moderator

Aug 29, 2011, 8:19 AM

Post #7 of 7 (913 views)
Re: [tedlaraghu] Contents in email body [In reply to] Can't Post

Problems:

1) Your scriupt is missing 2 very important pragmas, which should be in every Perl script you write.

Code
use strict; 
use warnings;


2) You should not use lowercase names for your modules. By convention, Perl uses lowercase names for pragmas.

3) What does your getEnv sub do? Given its name, I would expect a direct var assignment, but it's being used in void context. This adds obfuscation, which you should avoid.

4) You're needlessly using lots of concatenation where a simple string would be cleaner and easier. This is especially true on your time2str(..) statement.

Code
my $run_time = time2str("%d-%m-%Y-%H%M", time);


5) No need/reason to chomp $run_time.

6) Instead of checking $hostname in that if/elsif/elsif chain, it would be better to use a hash.

7) You're using constants which were defined outside of the script with no indication where they came from. That, again, adds obfuscation.

8) Instead of the constants, it would be better to use the Readonly module.
http://search.cpan.org/~roode/Readonly-1.03/Readonly.pm

9a) Use a lexical var for the filehandle instead of the bareword.

9b) Use the 3 arg form of open

9c) The die statement should include the reason it failed i.e., it should include $!.

9d) Instead of saying 'WARNING', say 'ERROR' in the die statement.

9e) The die statement says that it can't open the file for reading even though you're opening it in append mode.

Code
my $logfile = "$ENV{'LOG_DIR_RAGHU'}/autosys_$environment-$run_time.log"; 
open my $logfh, '>>', $logfile
or die("\nERROR : Can't open $logfile for appending <$!>\n\n");


10) Use parens around the args to your subs to make it clear that you're calling a sub and make clear what are its arguments.

11) I previously talked about the Useless Use of Cat.
http://en.wikipedia.org/wiki/Cat_%28Unix%29#Useless_use_of_cat
http://partmaps.org/era/unix/award.html

I could point out more issues, but that should be enough for now.


(This post was edited by FishMonger on Aug 29, 2011, 8:21 AM)

 
 


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

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