Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Not hard-code text with NET::SMTP ?

by Bush Dr (Initiate)
on Aug 11, 2003 at 15:27 UTC ( [id://282903]=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to call an email script from another program (Tivoli). If I hard-code the text to send, it sends normally...However, if I try to pass a variable as the text it never sends the page. Here's a snippet of Sample code below:
open (PAGE, "<staging.log"); @lines = <PAGE>; foreach $line (@lines) { $newline = $line; } close (PAGE); ##### Sending Email my $recipient1 = 'johnny@abc.net'; my $smtp = Net::SMTP->new('mailrelay.acme.com', Hello => 'email.acme.com' ); $smtp->mail ($ENV{USER}); $smtp->recipient($recipient1); $smtp->data(); $smtp->datasend("$newline"); $smtp->datasend(); $smtp->quit;
$newline is the culprit. If I pass it as listed above, it doesn't work. If I replace the variable with static text it runs.........help!!

Title edit by tye

Replies are listed 'Best First'.
Re: NET::SMTP
by gnangia (Scribe) on Aug 11, 2003 at 15:45 UTC
    Try it this way
    $smtp->data(); $smtp->datasend("To: $mailheaders{To}\n"); $smtp->datasend("From: $mailheaders{From}\n"); $smtp->datasend("Subject: $mailheaders{Subject}\n"); $smtp->datasend("Date: $smtpdate\n"); $smtp->datasend($message); $smtp->dataend();
    where $message contains text of the message
Re: NET::SMTP
by Abigail-II (Bishop) on Aug 11, 2003 at 15:33 UTC
    $newline will be the last line of staging.log, which you are getting in a very inefficient way. Is that what you want?

    Abigail

      that's the way I've done it now. It will be the only line in that file. I'm using:
      open (PAGE, "<staging.log");
      any ideas?

        I suspect that staging.log is playing tricks on you, but having a blank line at the end of the file. You can always check if the contents of the current instance of $line is empty before you assign it to $newline.

        Something like this:

        if( $line =~ /^\s*$/ ) { $newline = "Blank line."; } else { $newline = $line; }

        Or, better yet, you could play around with debug mode and step through the program as it runs.

        elbieelbieelbie

        If you're sure that's the only line in the file, a simpler and perhaps less error-prone method is to use:

        $newline = join("",<PAGE>);
        which will read in all of the lines from the file and concatenate them into one string.

        In any case, printing out $newline as a debugging aid will probably help you track this problem down without sending a zillion messages.

      No one took the bait, so I will :)

      What would be a more efficient way of doing it?

        One way of doing it more efficiently:
        while ($newline = <HANDLE>) {}

        It's very inefficient to first read in all the lines into an array, and then setting $newline to each entry of the array, ending at the last.

        If the file is big, you might want to read backwards from the end.

        Abigail

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://282903]
Approved by elbie
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-25 23:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found