[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: sending email w/perl



On Wed, 23 Oct 2002 21:26:32 -0500
Lance Hoffmeyer <lance@augustmail.com> wrote:

> I have a script where I am searching ebay
> to get results and email these to myself.
> The filehandle is FILE but everytime I 
> try to insert FILE into the $smtp it
> prints "FILE" and not the contents of file.
> What is the fix for this?
> 
> 
> use WWW::Search;
> open(FILE,">eltonjohn.txt");
> print FILE "Results of a search for Elton John MFSL on ebay\n\n\n\n";
>   my $oSearch = new WWW::Search('Ebay');
>   my $sQuery = WWW::Search::escape_query("Elton John MFSL");
>   $oSearch->native_query($sQuery);
>   while (my $oResult = $oSearch->next_result())
>     { print FILE $oResult->url, "\n"; }
> #     { print FILE $oResult, "\n"; }
> 
> 
> 
> 
> ######## SEND EMAIL INFORMING MYSELF OF EBAY RESULTS #################
> #e-mail variables
>  $mailsender = 'lance@augustmail.com';
>  $mailserver = 'mail.augustmail.com';
> # Then you have a subroutine: (You have to have libnet module
> # installed, ask again if you need directions on how to install this
> # module)
> # ####################################################################
> # ##sends an e-mail
>  #input: subject, mailto, mailbody
>  #output: e-mail
>  #################################################################### 
> sub Email{
>    #passing the parameters
> ( $subject, $mailto, $mailbody) = @_;
>    use Net::SMTP;
>    $smtp = Net::SMTP->new($mailserver);
>    $smtp->mail($mailsender);
>    $smtp->to($mailto);
>    $smtp->data();
>    $smtp->datasend("Subject: $subject\n");
>    $smtp->datasend("To: $mailto\n");
>    $smtp->datasend("From: $mailsender\n\n");
> #   $smtp->datasend($mailbody);
>    $smtp->datasend(FILE);
>    $smtp->datasend();
>    $smtp->quit;
>  } #Email
> 
>  &Email('Elton John Ebay Results','lance@augustmail.com');
> close(FILE);

First, I'm not a programmer -- of any type. Some of my jobs do cause
analytical/logical thinking though. Looking at your script something
didn't look right. 'datasend' seemed to want actual data rather than a
filename and (remember, I don't *know* perl) where is "FILE" ever closed
before attempting to use it for input?

I scanned some perl docs and found this:

**********************************************************************
Net::SMTP - Simple Mail Transfer Protocol Client

data ( [ DATA ] )
    Initiate the sending of the data from the current message.

    DATA may be a reference to a list or a list. If specified the
contents of DATA and a termination string ".\r\n" is sent to the server.
And the result will be true if the data was accepted.

    If DATA is not specified then the result will indicate that the
server wishes the data to be sent. The data must then be sent using the
datasend and dataend methods described in *Cmd*.
***********************************************************************
Net::Cmd - Network Command class (as used by FTP, SMTP etc)

datasend ( DATA )
    Send data to the remote server, converting LF to CRLF. Any line
starting with a '.' will be prefixed with another '.'. DATA may be an
array or a reference to an array.
***********************************************************************

 So, it appears that 'datasend' *IS* looking for specific, delimited,
input or a pre-defined array. So, you would most likely have to close
"FILE" then read it back in to an array or read it one line at a time
into the 'datasend' construct.

However, the first 'data ()' instance, right before 'datasend' appears
to be the place to read the file in its entirety (though I still don't
know about the "not closing" the file first thing).

You may just have to read the file into $mailbody using whatever perl
function is used to read a file into a string (saw that done in a
web-email form that used CGI to read the body into $body)

How about this?:

$file = 'FILE';		# Name the file
open(INFO, $file);		# Open the file
@lines = <INFO>;		# Read it into an array
close(INFO);			# Close the file

then your final bit would look like this:

use Net::SMTP;
>    $smtp = Net::SMTP->new($mailserver);
>    $smtp->mail($mailsender);
>    $smtp->to($mailto);
>    $smtp->data(@lines);
>    $smtp->quit;
>  } #Email

  Someone beat me with a wet noodle if I'm way off base. Just in a
reading/studying mood tonight.

G

-- 
gvl2 (Gerald)
AirBall the Rolling Basket Case (1969 Standard Beetle)
LifeSaver (1974 Bay Window Bus)
http://www.phorce1.com



Reply to: