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

Hi fellow monks,

A friend of mine recently asked if I could write a perl program that would send an email from a file. It seems he would like to periodically email a list of his students from a file he has written ( lecture notes, homework assignments, etc. ). So, this is the 'project' so far:

#!\usr\bin\perl -wT use strict; use Net::SMTP; use CGI qw ( :standard ); my ( $from, $to, $subject, $message, $address, $server, $myAddress ); $from = param( "from" ); $to = param( "to" ); $subject = param( "subject" ); $message = param( "message" ); $address = param( "address" ); $server = param( "server" ); $myAddress = param( "myAddress" ); my $smtp = new Net::SMTP( "$server", Hello => "$server" ) or die "Ughh! Unable to send email : $!\n"; $smtp->to( "$to ); $smtp->mail( "$myAddress" ); $smtp->data(); $smtp->datasend( "From: $from\n" ); $smtp->datasend( "To: $to\n" ); $smtp->datasend( "Subject: $subject\n\n\n" ); $smtp->datasend( "$message\n" ); $smtp->dataend(); $smtp->quit();


If the $message that he wanted to send was stored in a file called "syllabus.txt", how would I include that file name in place of the message text?

Thanks in advance,

-Katie.

Replies are listed 'Best First'.
Re: Sending email from a file.
by ibanix (Hermit) on Dec 05, 2002 at 08:38 UTC
    Mail::Sender will probally do what you want.
    #!/usr/bin/perl -w use strict; use Mail::Sender; # How these are defined is left as an exercise to the reader my ($from, $to, $subject, $message, $file, $server); # Create our message handler ref (my $mail = new Mail::Sender { smtp => $server } ) or die "Mail er +ror: $Mail::Sender::Error\n"; # Message header $mail->Open({ from => $from, to => $to, subject => $subject }) or die +"Mail error: $Mail::Sender::Error\n"; $mail->Body; # Add the "intro" message $mail->SendLineEnc("$message\n"); # Open $file, adding contents to message open(FILE, "$file"); while(<FILE>) { $mail->SendLineEnc("$_\n"); } # Close & send message $mail->Close;
    Alternately, if you wanted to send the file as and attachment, you could do:
    $mail->Attach({ description => 'Programming Perl 101 Syllabus', ctype => 'text/plain', encoding => '7bit', disposition => 'attachment; filename="syllabus.txt"; t +ype="Text file"', file => 'syllabus.txt' } );
    instead of opening and reading the file.

    $ echo '$0 & $0 &' > foo; chmod a+x foo; foo;
Re: Sending email from a file.
by robartes (Priest) on Dec 05, 2002 at 08:55 UTC
    Or, in the spirit of the camel, Another Way To Do It is to just slurp the file into $message:
    open HOMEWORK, "<syllabus.txt" or die "Blerch: $!\n"; local $/=undef; $message=<HOMEWORK>; # Send it on as you did above.

    CU
    Robartes-

    Update: fixed age-old gotcha of forgetting comma in open.

(z) Re: Sending email from a file.
by zigdon (Deacon) on Dec 05, 2002 at 13:42 UTC

    I think you're asking how to upload a file in CGI. If that's not your question, please just ignore me :)

    Just create a multipart form with a file field:

    <FORM METHOD=POST ACTION=/cgi-bin/upload.pl> <INPUT TYPE=FILE NAME=UploadFile> <INPUT TYPE=SUBMIT> </FORM>

    And then read the file in your CGI:

    use CGI; my $q = new CGI; my $filename = $q->param('UploadFile'); while (<$filename>) { # do something with each line of the file }

    See the CGI manpage - search for "CREATING A FILE UPLOAD FIELD"

    -- Dan