CoffeeDuke has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks, Sorry for a n00b question but I am quite new to Perl. I am actively learning and have a little starter project. I am writing a small script that would send an email with some details about an event happening on diff. dates. All works ok so far except the dates I calculated $nextday and $mon, those I wanted to add in the email Body and that does not work. I only get the "text" printed like this: "Next expected eMails from MCP (local Time): Server Start: $NEXTDAY, around 10:00" Here is my script: It returns exact what is written in the Body, but I want to see the 2 Variables return the actual Value (date). Thanks for your help.
#!/usr/bin/perl -w use CGI; use strict; use Date::Calc qw(Today Day_of_Week Add_Delta_Days); # Variables my ($start,$nextday,$mon,$counter,$day1,$mail); my ($year, $month, $day) = Today(); my $sendmail = "/usr/sbin/sendmail -oi -t -odq"; # E-Mail Config my $to_mail = "coffeeDuke\@gmail.com"; my $send_to = "To: $to_mail\n"; my $reply_to = "Reply-to: $to_mail\n"; my $subject = "Subject: Test Email Send From PerlScript\n"; # Main # NextDay = Mon-Fri | if Fri then NextDay = Mon if (Day_of_Week($year, $month, $day) || "1,2,3,4") { $counter = 1; $day1 = Today(); $day1 = Today() + $counter; $counter++; $nextday = $day1.$month.$year; $nextday = (length($day1) < 2) ? "0".$day1."." :$day1; $nextday .= (length($month) < 2) ? "0".$month : $month; $nextday .= "."; $nextday .= $year; } # Calc. Nextday if (Day_of_Week($year, $month, $day) == 5) { $counter = 3; $day1 = Today(); $day1 = Today() + $counter; $counter++; $nextday = $day1.$month.$year; $nextday = (length($day1) < 2) ? "0".$day1."." :$day1; $nextday .= (length($month) < 2) ? "0".$month : $month; $nextday .= "."; $nextday .= $year; } # Calc. Monday while (Day_of_Week($year, $month, $day) != 1) { ($year, $month, $day) = Add_Delta_Days($year, $month, $day, 1); } $mon = (length($day) < 2) ? "0".$day."." :$day; $mon .= (length($month) < 2) ? "0".$month : $month; $mon .= "."; $mon .= $year; # Send Email open(SM, "|$sendmail") or die "Cannot open $sendmail: $!"; print SM $reply_to; print SM $subject; print SM $send_to; print (SM "Content-type: text/html\n\n"); # Mail Body print (SM "<HTML><BODY>"); print (SM "Mail message from CoffeeDuke <BR><BR>"); print (SM "Write some usefull Text here..<BR><BR>"); print (SM "Next expected Backups of Coffee Server (local Time): <BR><B +R>"); print (SM "Master Server:" .$nextday, "around 10:00am <BR><BR> +"); print (SM "Backup Server:" .$mon, "around 10:30am <BR><BR>"); print (SM "Regards, <BR><BR>"); print (SM "Backup Service DukeCoffee<BR><BR>"); print (SM "</HTML></BODY>"); close(SM); __END__
  • Comment on Using Sendmail - how can I active use defined variables in the Message Body
  • Download Code

Replies are listed 'Best First'.
Re: Using Sendmail - how can I active use defined variables in the Message Body
by davido (Cardinal) on Aug 13, 2012 at 09:25 UTC

    Can you point me to the line(s) in the script you posted that is/are producing this output?

    Next expected eMails from MCP (local Time): Server Start: $NEXTDAY, around 10:00

    I don't see "$NEXTDAY" anywhere in the code you posted, nor do I see the text, "eMails from MCP". Maybe it's some neat obfu trick, but otherwise, I don't really want to spend time looking at code that isn't capable of producing the output you're describing.


    Dave

      $nextday is calculated in line 44. if you do a print on that it should give you the date. $mon is done in line 55. sorry my bad about the print out text I changed it in the code after I wrote it. Its the part in line 71 / 72
      print (SM "Master Server:" .$nextday, "around 10:00am <BR><BR>"); print (SM "Backup Server:" .$mon, "around 10:30am <BR><BR>");
      Thanks for answering Dave, much appriciated.

        Unrelated to the output you're receiving, but this line can never be false:

        if (Day_of_Week($year, $month, $day) || "1,2,3,4") {

        Also, these two lines are a little perplexing:

        $nextday = $day1.$month.$year; $nextday = (length($day1) < 2) ? "0".$day1."." :$day1;

        The second assignment clobbers the first one... may as well just get rid of the first one.

        At any rate, can you post the exact code that's failing, and the exact copied and pasted output? I still don't think we've seen code, and output from the same version of your script.


        Dave

        $NEXTDAY is NOT the same variable as $nextday. The following example uses strings as the values in the variables, but using numeric values (with appropriate adjustment of the test at Ln 8) would provide a similar demonstration.

        #!/usr/bin/perl use 5.014; # 987079 my $NEXTDAY = "fooday"; my $nextday = "BARDAY"; if ($NEXTDAY eq $nextday) { say "Behold, a miracle!"; } else { say "\$NEXTDAY is $NEXTDAY and \$nextday is $nextday"; }

        If you used the == test at Ln 8, you would observe an apparent miracle... but only because an == test checks for numeric equality... even when applied to non-numerics (eg, strings).

Re: Using Sendmail - how can I active use defined variables in the Message Body
by Marshall (Canon) on Aug 13, 2012 at 11:13 UTC
    I don't quite understand what is failing, but since I'd never used Date::Calc before, I wrote a little skip_WeekEnd() routine for you.
    Hope it helps...Add one line to include Friday->Monday.

    Calculate the date that you need in one sub.
    Do the formatting in another sub or inline if it becomes simple enough.
    Your "length($month)" sort of stuff is confusing (at least to me) and there are problems with using a string operator like length() with something that is designed to operate on integer values.

    #!/usr/bin/perl -w use strict; use Date::Calc qw(Day_of_Week Date_to_Text_Long Add_Delta_YMD); use Data::Dumper; my @dates = ([2012, 8, 9 ], # Thursday DOW=4 [2012, 8, 10], # Friday DOW=5 [2012, 8, 11], # Saturday DOW=6 [2012, 8, 12], # Sunday DOW=7 [2012, 8, 13]);# Monday DOW=1 foreach my $dateRef (@dates) { print "Current Date: ",Date_to_Text_Long(@$dateRef),"\n"; my @NoWeekend = skip_WeekEnd(@$dateRef); print "Skip Sat/Sun: ",Date_to_Text_Long(@NoWeekend),"\n\n"; } sub skip_WeekEnd #returns next Monday if this is a Weekend { my @date = @_; my $dow = Day_of_Week(@date); @date = Add_Delta_YMD(@date,0,0,2) if($dow == 6);#Sat @date = Add_Delta_YMD(@date,0,0,1) if($dow == 7);#Sun return @date; } __END__ Current Date: Thursday, August 9th 2012 Skip Sat/Sun: Thursday, August 9th 2012 Current Date: Friday, August 10th 2012 Skip Sat/Sun: Friday, August 10th 2012 Current Date: Saturday, August 11th 2012 Skip Sat/Sun: Monday, August 13th 2012 Current Date: Sunday, August 12th 2012 Skip Sat/Sun: Monday, August 13th 2012 Current Date: Monday, August 13th 2012 Skip Sat/Sun: Monday, August 13th 2012
Re: Using Sendmail - how can I active use defined variables in the Message Body
by afoken (Chancellor) on Aug 13, 2012 at 18:11 UTC

    Have a look at the various template engines available at CPAN. Separate HTML and code. Depending on the template engine, you may get HTML escaping for free.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)