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

My programmer partner, at whose feet I am learning, has asked me to find the answer to a problem. He is working off site on a contract and has limited net access (this is not a master testing a pupil). I've asked on comp.lang.perl.modules and on a couple of my regular mailing lists to no avail. I now turn to the monks for help. His question to me was: how to get a filehandle out of Mailer::Mail suitable for use with the write() function. I have tried
$mailer = new Mail::Mailer 'smtp', Server=>'smtp.server.net'; select($mailer); select($mailer->open(\%mail_header)); select($mailer->body);
I know there is a filehandle out there somewhere I just can't find it. TIA Tony

Replies are listed 'Best First'.
Re: Getting Filehandle from Mail::Mailer
by chromatic (Archbishop) on Apr 26, 2000 at 20:34 UTC
    It inherits from IO::Handle, so something like this ought to work. (I've tested it, but the delivery delays here are terrible.)
    #!/usr/bin/perl -w use strict; use Mail::Mailer; my $mailer = new Mail::Mailer 'smtp', Server => 'smtp.server.net'; $mailer->open( { from => 'chromatic', to => 'chromatic@perlmonks.org', + subject => 'Not Valid Addresses' }); print $mailer "This is my text.";
    $mailer acts like a filehandle in this case, so it ought to work with write().
Re: Getting Filehandle from Mail::Mailer
by btrott (Parson) on Apr 26, 2000 at 20:40 UTC
    Expanding from chromatic's answer--as he said, Mail::Mailer inherits from IO::Handle, so a Mail::Mailer object is a filehandle.

    You said you tried

    select $mailer;
    Did that not work for you? It worked for me:
    my $mailer = new Mail::Mailer 'smtp', 'relay.foo.com'; $mailer->open({ To => 'baz@foo.com', From => 'foo@bar.com', Subject => 'Testing' }); select $mailer; print "This is a test message.\n"; $mailer->close;
Re: Getting Filehandle from Mail::Mailer
by BBQ (Curate) on Apr 26, 2000 at 22:13 UTC
    Anyone see anything wrong with this approach? I've been using it for a while...
    sub SendEmail { my ($to,$from,$subject,$msg) = @_; my $host = '192.168.193.10'; # your mailhost if ($to) { use Net::SMTP; $smtp = Net::SMTP->new($host); $smtp->mail($from); $smtp->to($to); $smtp->data(); $smtp->datasend("From: $from\n"); $smtp->datasend("To: $to\n"); $smtp->datasend("Subject: $subject\n"); $smtp->datasend("\n"); $smtp->datasend("$msg\n"); $smtp->dataend(); $smtp->quit; return(1); } else { return(0); } }
    The error cases could use some improovement, I know... But, HTH!

    UPDATED (ala Slashdot):
    Sorry about this post, it doesn't reply the asked question...
    I guess I just got carried away.

    #!/home/bbq/bin/perl
    # Trust no1!
Re: Getting Filehandle from Mail::Mailer
by Anonymous Monk on Apr 27, 2000 at 19:17 UTC
    Many thanks to chromatic btrott and even BBQ. My partner is still having trouble and so sends along this message
    Yes I can print() to $mailer but I cannot write() to it. Here are the errors.
    Write on closed filehandle at ./format_eg.pl line 77.
    Write on closed filehandle at ./format_eg.pl line 78.
    Lines 77 and 78 are the two write statements in the code below. The code works on STDOUT and a normal file. Here is the code.
    sub Write_Data{ format STDOUT = @<<<<<<<<<<@<<<<<<<<<<<@<<<<<<<<<<<<@<<<<<<<<<<<<<<<<<<<<<< shift(@printrows), shift(@printrows), shift(@printrows), shift(@printrows) . my $To = 'dvmrgn@home.com'; my %mail_header = ('To', $To, 'From', 'webmaster@onlinerenter.com', 'Subject', 'Test of New Clients Report'); my $mailer = new Mail::Mailer 'smtp', Server=>'smtp.telusplanet.net'; $mailer->open(\%mail_header); my $ofh = select($mailer); $~ = "STDOUT"; write ; write ; $mailer->close; select($ofh); } # End of write_data
    Please thank everyone for their help and I hope someone has an insight to it.
    There endeth the q uestion. TIA
    Tony