Results 1 to 13 of 13

Thread: loop problem

  1. #1
    Join Date
    Feb 2004
    Posts
    25

    Unanswered: loop problem

    What I am doing wrong am I having problem with loop what I need to change or modify to print $uc_cust1 in subject line of mail and in the body of the mail.

    For given customer name in calendar file I am opening the $annualfile file then send mail to all managers with there data

    The problem That I am having are
    1-
    subject => "for client $uc_cust1 $uc_os1", always print the first customer name and does not print the second customer name for example print Cust_ABCD LINX but does not print Cust_EFGH HP
    2-
    The same thing happen in body of the mail
    datasend("<b>Hi</b><br>\n\n $uc_cust1 $uc_os1<br>\n $body<br>\n");

    For example the calendar file contain
    Cust_ABCD LINX
    Cust_EFGH HP-UX

    and each $annualfile contains

    Manager : jon slate
    Title : Manager, tec
    Serial : abcd89
    Email : jon@abcd.com
    Data 1 data2 jhkhh
    Xxxxxxxxxxxxxxxxx
    Xxxxxxxxxxxxxxxxx
    ******************************************
    Manager : mark anton
    Title : Manager, intel
    Serial : noo7788
    Email : marky@xefr.com
    Data 2 3 Xxxxxxxxxxxxx
    Xxxxxxxxxxxxx

    my $calendarfile = "c:\\uu\\calendar.txt";
    open(CALENDARFILE,"<$calendarfile") or die "open failed:
    $calendarfile\n";
    my @fields = ();
    while($_ = <CALENDARFILE> ) {
    @fields = split ' ';
    my ($cust1, $cust2) = split(/\W/,lc($fields[0]));
    my ($os1, $os2) = split(/\W/,lc($fields[1]));
    my $uc_cust1 = uc($cust1);
    my $uc_os1 = uc($os1);
    my $cust_mail = lc($fields[2]);

    my $annualfile = "c:\\mydata\\${uc_cust1}_${uc_os1}.log";

    open(ANNUALFILE,"<$annualfile") or print $!,"\n";
    {
    local $/='The people below are managed by:';
    while( <ANNUALFILE> ){
    chomp;
    next unless my ($manager) = /Manager\s*:\s*(.*)/;
    my @manager = (split(/\W/, $manager));
    my $first_name = $manager[0];
    my $second_name = $manager[2];
    next unless my ($to) = /Email\s*:\s*(.*)/;
    push(@mails,[$to,$_]);
    }}
    close ANNUALFILE;

    foreach (@mails){
    send_mail($_->[0],$_->[1]);
    }

    sub send_mail{
    use lib '/perl/lib';
    use Net:MTP;
    use Mail:ender;
    $sender = new Mail:ender
    my ($to,$body) = @_;
    my $smtp = Net:MTP->new('d276xxx');
    $smtp->mail('joebla\@bla.com');
    $smtp->to($to);
    $smtp->data();
    $smtp->datasend("To: $to\n");
    $smtp->datasend("Subject:for client $uc_cust1 $uc_os1\n");
    $smtp->datasend("From: Joe Blla\n");
    $smtp->datasend("Content-Type: text/html\n");
    $smtp->datasend("\n");
    $body =~ s/$/<br>/mg;
    $smtp->datasend("<b>Hi</b><br>\n\n $uc_cust1 $uc_os1<br>\n
    $body<br>\n");
    $smtp->dataend();
    $smtp->quit;
    }
    }
    close CALENDARFILE;

  2. #2
    Join Date
    Jan 2004
    Location
    Germany
    Posts
    167
    This piece of code works on my machine:
    Code:
    #! /usr/bin/perl
    
    use strict;
    use warnings;
    
    my $calendarfile = "c:\\uu\\calendar.txt";
    
    open(CALENDARFILE,"<$calendarfile") or die "open failed: $calendarfile\n";
    while(my $line = <CALENDARFILE> ) {
      my @fields = split(/\s+/,$line);
      my ($cust1, $cust2) = split(/\W/,lc($fields[0]));
      my ($os1, $os2) = split(/\W/,lc($fields[1]));
      my $uc_cust1 = uc($cust1);
      my $uc_os1 = uSc($os1);
      my $cust_mail = lc($fields[2]);
      my $annualfile = "c:\\mydata\\${uc_cust1}_${uc_os1}.txt";
    
    
      open(ANNUALFILE,"<$annualfile") or print $annualfile,": ",$!,"\n";
      {
        local $/='The people below are managed by:';
        while( <ANNUALFILE> ){
          chomp;
          next unless my ($manager) = /Manager\s*:\s*(.*)/;
          my @manager = (split(/\W/, $manager));
          my $first_name = $manager[0];
          my $second_name = $manager[2];
          next unless my ($to) = /Email\s*:\s*(.*)/;
          send_mail($to,$_,$uc_cust1,$uc_os1);
        }
      }
      close ANNUALFILE;
    }
    close CALENDARFILE;
    
    sub send_mail{
      use lib '/perl/lib';
      use Net::SMTP;
      #use Mail::Sender;
      #$sender = new Mail::Sender
      my ($to,$body,$cust,$os) = @_;
      my $smtp = Net::SMTP->new('d276xxx');
      $smtp->mail('joebla\@bla.com');
      $smtp->to($to);
      $smtp->data();
      $smtp->datasend("To: $to\n");
      $smtp->datasend("Subject:for client $cust $os\n");
      $smtp->datasend("From: Joe Blla\n");
      $smtp->datasend("Content-Type: text/html\n");
      $smtp->datasend("\n");
      $body =~ s/\$/<br>/mg;
      $smtp->datasend("<b>Hi</b><br>\n\n $uc_cust1 $uc_os1<br>\n$body<br>\n");
      $smtp->dataend();
      $smtp->quit;
    }
    Please use the [code]-tags if you post some perl-code. It makes the code more readable.
    board.perl-community.de - The German Perl-Community

  3. #3
    Join Date
    Feb 2004
    Posts
    25
    Yes Renee that worked fine thank you.
    Also I need to know how can I include HTML form with in $body of the mail this is what I have done using your codes

    sub send_mail{
    use lib '/perl/lib';
    use Net:MTP;
    use CGI;
    use CGI::Carp qw(fatalsToBrowser);
    use lib qw(./perllib);

    my ($to,$body,$cust,$os) = @_;
    my $smtp = Net:MTP->new('d276xxx');
    $smtp->mail('joebla\@bla.com');
    $smtp->to($to);
    $smtp->data();
    $smtp->datasend("To: $to\n");
    $smtp->datasend("Subject:for client $cust $os\n");
    $smtp->datasend("From: Joe Blla\n");
    $smtp->datasend("Content-Type: text/html\n");
    $smtp->datasend("\n");
    $body =~ s/\$/<br>/mg;

    my $form = qq~

    <HTML>
    <BODY BGCOLOR="#FFFFCC"
    TEXT="000000"
    >
    <H3 align=center>Please Complete the form</H3>
    <form method ="post"
    ENCTYPE = "text/plain"
    ACTION ="mailto:joebla\@bla.com?subject=Reply the form $cust1"
    >
    <input type="hidden" name="to" value="$to">
    <input type="hidden" name="to" value="$cust1">
    <input type="hidden" name="to" value="$manager[2]">
    <input type="submit" value="Send your Request">

    $smtp->datasend("<b>Hi</b><br>\n\n $uc_cust1 $uc_os1<br>\n$body<br>\n");

    </form></BODY></HTML>~;
    my $cgi = CGI->new();

    $smtp->dataend();
    $smtp->quit;
    }

  4. #4
    Join Date
    Jan 2004
    Location
    Germany
    Posts
    167
    I would recommend to replace the send_mail-subroutine by the routine I've posted some weeks ago (http://www.dbforums.com/t1004695.html )...

    There I've given you some code sending a form via mail!
    board.perl-community.de - The German Perl-Community

  5. #5
    Join Date
    Feb 2004
    Posts
    25
    I have used your recommendation, but unable to make it work
    I have the following problems

    1- Error message
    [Thu Aug 26 15:11:01 2004] test.pl: Use of uninitialized value in substitution (s///) at test.pl line 46, <ANNUALFILE> chunk 4.

    [Thu Aug 26 15:11:01 2004] test.pl: Use of uninitialized value in concatenation (.) or string at test.pl line 74, <ANNUALFILE> chunk 4.

    Line 46 contain
    $body =~ s/$/<br>/mg;

    Line 47 contain
    $sender->Open({ from => 'your@address.com', to => "$to",

    Requirement
    1- $to or recipient
    3- The line Hi $second_name


    #! /perl/bin/perl
    my $calendarfile = "c:\\uu\\calendar.txt";
    open(CALENDARFILE,"<$calendarfile") or die "open failed:
    $calendarfile\n";
    while(my $line = <CALENDARFILE> ) {
    my @fields = split(/\s+/,$line);
    my ($cust1, $cust2) = split(/\W/,lc($fields[0]));
    my ($os1, $os2) = split(/\W/,lc($fields[1]));
    my $uc_cust1 = uc($cust1);
    my $uc_os1 = uc($os1);
    my $cust_mail = lc($fields[2]);
    my
    $annualfile="c:\\myprog\\perl\\test\\${uc_cust1}_$ {uc_os1}-annual.log"
    ;
    open ANNUALFILE, "<$annualfile";
    {
    local $/='The people below are managed by:';
    while( <ANNUALFILE> ){
    chomp;
    next unless my ($manager) = /Manager\s*:\s*(.*)/;
    my @manager = (split(/\W/, $manager));
    my $first_name = $manager[0];
    my $second_name = $manager[2];
    next unless my ($to) = /Email\s*:\s*(.*)/;
    #send_mail($to,$_,$uc_cust1,$uc_os1);
    send_mail($to,$_,$cust,$os);
    }
    }
    close ANNUALFILE;
    }
    close CALENDARFILE;

    sub send_mail{
    use strict;
    use warnings;
    use CGI;
    use CGI::Carp qw(fatalsToBrowser);
    use lib qw(./perllib);
    use Mail::Sender;
    my ($to,$body,$cust,$os) = @_;
    $body =~ s/$/<br>/mg;
    my $form = qq~

    <form method ="post"
    ENCTYPE = "text/plain"
    ACTION ="mailto:joebla\@efg.com?subject=Reply the from $second_name for $uc_cust1"
    >
    <input type="text" name="myField_1"><br />
    <input type="hidden" name="to" value="$to">
    <input type="submit" value="test">

    <BR>
    <b>Hi $second_name</b><br>\n\n test test lines,
    <br>\n<br>\n
    $cust $os Thank you for your assistance.<br>\n
    <br>\n
    $body<br>
    </form>~;

    my $cgi = CGI->new();
    #print $cgi->header(-type => 'text/html');

    my $sender = new Mail::Sender {
    smtp => 'smtp.com',
    on_errors => undef,
    } or die "Can't create the Mail::Sender object:

    $Mail::Sender::Error\n";

    $sender->Open({ from => 'your@address.com', to => "$to",

    subject => "HTML test for $uc_cust1 $uc_os1",
    ctype => "text/html",
    encoding => "7bit"}) or die $Mail::Sender::Error,"\n";
    $sender->SendEx($form);
    $sender->Close();

    print $cgi->h1('OK');
    }
    Last edited by lonaj; 08-26-04 at 02:18.

  6. #6
    Join Date
    Jan 2004
    Location
    Germany
    Posts
    167
    It would be very helpful to me, if you use the [code]-Tags to make your code more readable. And an error message instead of "it doesn't work" would help to find the error faster...
    board.perl-community.de - The German Perl-Community

  7. #7
    Join Date
    Feb 2004
    Posts
    25
    Renee, thank you for follow up
    I am not quit sure how to do [code]-tags so I have attached the script
    If you are refering to selecting "Disable smilies in text" I have done it please correct me if I am wrong

    I am getting the following errors

    1- Error message
    [Thu Aug 26 15:11:01 2004] test.pl: Use of uninitialized value in substitution (s///) at test.pl line 46, <ANNUALFILE> chunk 4.

    [Thu Aug 26 15:11:01 2004] test.pl: Use of uninitialized value in concatenation (.) or string at test.pl line 74, <ANNUALFILE> chunk 4.

    Line 46 contain
    $body =~ s/$/<br>/mg;

    Line 47 contain
    $sender->Open({ from => 'your@address.com', to => "$to",
    Attached Files Attached Files

  8. #8
    Join Date
    Jan 2004
    Location
    Germany
    Posts
    167
    please test this code on your machine!
    Code:
    #! /usr/bin/perl
    
    use strict;
    use warnings;
    use CGI;
    use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
    use lib qw(./perllib);
    use Mail::Sender;
    use Data::Dumper;
    
    $| = 1;
    
    my $cgi = CGI->new();
    print $cgi->header(-type => 'text/html');
    warningsToBrowser(1);
    
    my $calendarfile = "./calendar.txt";
    open(CALENDARFILE,"<$calendarfile") or print "open failed: $calendarfile\n";
    while(my $line = <CALENDARFILE> ) {
      my @fields = split(/\s+/,$line);
      my ($cust1, $cust2) = split(/\W/,lc($fields[0]));
      my ($os1, $os2) = split(/\W/,lc($fields[1]));
      my $uc_cust1 = uc($cust1);
      my $uc_os1 = uc($os1);
      my $cust_mail = lc($fields[2]);
      my $annualfile="./${uc_cust1}_${uc_os1}.log";
      open ANNUALFILE, "<$annualfile" or print $annualfile," -> ",$!;
      {
        local $/='The people below are managed by:';
        while( <ANNUALFILE> ){
          chomp;
          next unless my ($manager) = /Manager\s*:\s*(.*)/;
          my @manager = split(/\W/, $manager);
          my $first_name = $manager[0] || 'unknown Manager';
          my $second_name = $manager[2] || 'Testperson';
          next unless my ($to) = /Email\s*:\s*(.*)/;
          print $cgi->h2('sending mail to '.$to);
          send_mail($to,$_,$uc_cust1,$uc_os1,$second_name);
        }
      }
      close ANNUALFILE;
    }
    close CALENDARFILE;
    print $cgi->h1('OK');
    
    sub send_mail{
      my ($to,$body,$cust,$os,$second) = @_;
      my $form = qq~
        <form method ="post"  ACTION ="mailto:dbforums\@renee-baecker.de?subject=Reply the from $second for $cust" >
          <input type="text" name="myField_1"><br />
          <input type="hidden" name="to" value="$to">
          <input type="submit" value="test"><BR>
          <b>Hi $second</b><br>\n\n test test lines, 
          <br>\n<br>\n $cust $os Thank you for your assistance.<br>\n<br>\n
          $body<br>
        </form>~;
    
      my $sender = new Mail::Sender {smtp => 'your.smtp.tld'} or print "Can't create the Mail::Sender object: $Mail::Sender::Error\n";
    
      $sender->Open({ from => 'person@domain.tld', 
                      to => 'recipient@domain.tld',
                      subject => "HTML test for $cust $os",
                      ctype => "text/html",
                      encoding => "7bit"}) or print "It doesn't work! $Mail::Sender::Error\n";
      $sender->SendEx($form) or print $Mail::Sender::Error;
      $sender->Close();  
    }
    Using the [code]-Tags: [ code ] ... your code [ / code ] but without whitespaces in the tags!
    board.perl-community.de - The German Perl-Community

  9. #9
    Join Date
    Feb 2004
    Posts
    25
    Thank you for reply
    I have tested your code this time no error message
    But the MAIL subroutine for send_mail did not send any mail

  10. #10
    Join Date
    Jan 2004
    Location
    Germany
    Posts
    167
    Does your SMTP-server require an authentication?? In that case you should read the documentation of Mail:ender on CPAN ( http://search.cpan.org )!


    If that doesn't work you should use an other module (look at CPAN for Mail::* modules or Net:MTP )
    board.perl-community.de - The German Perl-Community

  11. #11
    Join Date
    Feb 2004
    Posts
    25
    There is no problem with authentication or mail module i am able to send mail with the code provided but I am having problem with $cust $os values in Subroutine send_mail
    Attached Files Attached Files

  12. #12
    Join Date
    Jan 2004
    Location
    Germany
    Posts
    167
    This code works fine. I've tested it on my webserver (Debian 3.0 and perl 5.008).
    Code:
    #! /usr/bin/perl 
    
    use strict; 
    use warnings; 
    use CGI; 
    use CGI::Carp qw(fatalsToBrowser); 
    use lib qw(./perllib); # include the directory where Mail/Sender.pm is
    use Mail::Sender; 
    
    my $cgi = CGI->new();
    print $cgi->header(-type => 'text/html'); 
    
    my $calendarfile = "./calendar.txt"; 
    
    my @mails;
    
    open(CALENDARFILE,"<$calendarfile") or print "open failed: $calendarfile\n"; 
    while(my $line = <CALENDARFILE> ) { 
      chomp $line;
      my @fields = split(/\s+/,$line); 
      my ($cust1, $cust2) = split(/\W/,lc($fields[0])); 
      my ($os1, $os2) = split(/\W/,lc($fields[1])); 
      my $uc_cust1 = uc($cust1); 
      my $uc_os1 = uc($os1); 
      my $cust_mail = lc($fields[2]); 
    
      my $annualfile="./${uc_cust1}_${uc_os1}.log"; 
    
      open ANNUALFILE, "<$annualfile"; 
      { 
        local $/='The people below are managed by:'; 
        while( <ANNUALFILE> ){ 
          chomp; 
          next unless my ($manager) = /Manager\s*:\s*(.*)/; 
          my @manager = (split(/\W/, $manager)); 
          my $first_name = $manager[0]; 
          my $second_name = $manager[2]; 
          next unless my ($to) = /Email\s*:\s*(.*)/; 
          push (@mails,[$to,$_,$uc_cust1,$uc_os1,$second_name]); 
        } 
      } 
      close (ANNUALFILE); 
    } 
    close(CALENDARFILE); 
    
    foreach my $mail(@mails){ 
      send_mail(@$mail); 
    } 
    
    sub send_mail{ 
      my ($to,$body,$cust,$os,$second) = @_; 
      $body =~ s/$/<br>/mg; 
      my $form = qq~ 
        <HTML><BODY BGCOLOR="#FFFFCC" TEXT="000000"> 
        <H3 align=center>Please Complete the form below</H3> 
        <form method ="post" ENCTYPE = "text/plain" ACTION ="mailto:dbforums\@renee-baecker.de?subject=Reply the from $second for $cust" > 
        <input type="text" name="myField_1"><br /> 
        <input type="hidden" name="to" value="$to"> 
        <input type="submit" value="test"><BR> 
        <b>Hi $second</b><br>\n\n test test lines, 
        <br>\n<br>\n $cust $os Thank you for your assistance.<br>\n<br>\n 
        $body<br> 
        </form></BODY></HTML>~; 
    
      my $sender = new Mail::Sender { smtp => 'your.smtp.com', on_errors => undef, } 
                   or die "Can't create the Mail::Sender object: $Mail::Sender::Error\n"; 
    
      $sender->Open({ from     => 'person@domain.tld',
                      to       => 'recipient@domain.tld', 
                      subject  => "HTML test for $cust $os", 
                      ctype    => "text/html",  
                      encoding => "7bit"}) or die $Mail::Sender::Error,"\n"; 
      $sender->SendEx($form); 
      $sender->Close(); 
      print $cgi->h1('ok'); 
    }
    board.perl-community.de - The German Perl-Community

  13. #13
    Join Date
    Feb 2004
    Posts
    25
    Thanks that works fine

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •