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

Re: sending email w/perl



On Wed, 23 Oct 2002, Lance Hoffmeyer wrote:

The problem is:

> open(FILE,">eltonjohn.txt");

Here, you open FILE for writing, but then...

> [snip]

>    $smtp->datasend(FILE);

Here you try to read from it.

That won't work in any case; you can't read from a writing file handle.

But you also have other problems. From perldoc Net::Cmd:

       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.

the datasend() method doesn't take a filehandle, it takes either an array
or a reference to an array.

Here's a better method:

#!/usr/local/bin/perl

use strict;
use warnings;
use WWW::Search;
use Net::SMTP;

my @lines;

push(@lines, "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())
    { push @lines, $oResult->url . "\n" ; }

my $mailsender = 'lance@augustmail.com';
my $mailserver = 'mail.augustmail.com';

sub Email{
   #passing the parameters
   my ( $subject, $mailto ) = @_;
   my $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(@lines);
   $smtp->datasend();
   $smtp->quit;
 }

Email('Elton John Ebay Results','lance@augustmail.com');


----------------------------------------------------------------------
Andrew J Perrin - http://www.unc.edu/~aperrin
Assistant Professor of Sociology, U of North Carolina, Chapel Hill
clists@perrin.socsci.unc.edu * andrew_perrin (at) unc.edu







Reply to: