in reply to Email With Attachments
Here is a Mime::Entity example ripped straight out of a subscriber list script of mine.
Hope this helps, it is fairly self explanatory I think. It uses an eval to check for the availability of Mime::Entity.
Cheers
tachyon
#!/usr/bin/perl -Tw # ensure all fatals go to browser during debugging and setup # *don't* uncomment these lines on production code for security # BEGIN { # $|=1; # print "Content-type: text/html\n\n"; # use CGI::Carp('fatalsToBrowser');} use strict; use Fcntl (':flock'); my $mime = (eval 'use MIME::Entity') ? '' : 1; # check if MIME::entity + installed, required to send HTML and Binaries use CGI; my $q = new CGI; # create new CGI object $CGI::DISABLE_UPLOADS = 0; # enable uploads $CGI::POST_MAX = 1048576; # limit the maximum upload size to 1MB # we declare an Unindent prototype here which we use for our herepages sub Unindent; # to enable mail queueing for sendmail set this to '-odq' (set to '' f +or no queue) # *warning* -odq option needs to be configured or messages may never b +e delivered! my $odq = ''; ..... sub Mailout { (my $date = $datetime) =~ s/\s\d\d:\d\d\:\d\d//; my $recordfile = $file; my @archive = &GetRecords('archive.txt'); $recordfile = $file." :re-send" if grep{/$file/}@archive; + my @subscriber = &GetRecords('subscriber.txt'); my $emessage = "Here is the latest $list_name."; my $count = 0; foreach my $line(@subscriber) { next if $line =~ m/^\s*$/; (my $date,$name,$email) = split /,/,$line; my $unsubmessage = &UnsubLink; if ($file =~ m/txt$/) { &SendText($emessage, "$path_to_files/$file", $unsubmessag +e); } elsif ($file =~ m/(?:htm|html)$/) { return "Sorry, unable to send HTML file, MIME::Entity not i +nstalled!<br>\n" unless $mime; &SendHTML($emessage, "$path_to_files/$file", $unsubmessag +e); } else { return "Sorry, unable to send binary file, MIME::Entity not + installed!<br>\n" unless $mime; &SendBinary($emessage, "$path_to_files/$file", $unsubmess +age); } $count++; } &AddRecords('archive.txt', "$date,$recordfile,$count"); return "The file: '$file' was sent to $count subscribers.<br>\n"; } sub SendBinary { my ($emessage, $file, $unsubmessage) = @_; my $top = build MIME::Entity Type => 'multipart/mixed', From => $our_email, To => $email, Subject => $list_name; attach $top Data => "Hello $name,\n\n$emessage\n\n$us\n\n\n$unsubmessage" +; attach $top Path => $file, Type => 'application/octet-stream', Encoding => 'base64'; open ('MAIL', "|$mail_prog -t -i $odq") or DieNice("Can't open mai +l program '$mail_prog': $!"); eval '$top->print(\*MAIL)' if $mime; close MAIL; return; } sub SendHTML { my ($emessage, $file, $unsubmessage) = @_; my $top = build MIME::Entity Type => 'multipart/mixed', From => $our_email, To => $email, Subject => $list_name; attach $top Data => "Hello $name,\n\n$emessage\n\n$us\n\n\n$unsubmessage" +; attach $top Path => $file, Type => 'text/HTML', Encoding => '7bit'; open ('MAIL', "|$mail_prog -t -i $odq") or DieNice("Can't open mai +l program '$mail_prog': $!"); eval '$top->print(\*MAIL)' if $mime; close MAIL; return; } # Send Text file to subscriber, we are using a closure { # we define some closure vars to provide a private memory my $text; # remember contents of last file sent my $last_file; # remember name of last file sent sub SendText { my ($emessage, $file, $unsubmessage) = @_; # add path unless full path specified $file = "$script_path/$file" unless $file =~ m/\//; # first check if we have just opened this file, # to avoid unecessary disk access on bulk mailout if ($last_file ne $file) { $text = join "\n", &GetRecords($file); $last_file = $file; # remember the last file sent } my $message = Unindent <<" MESSAGE"; To: $email Reply-to: <$our_email> From: $us <$our_email> Subject: $list_name Hello $name, $emessage $text $us $unsubmessage MESSAGE open ('MAIL', "|$mail_prog -t -i $odq") or DieNice("Can't open + mail program '$mail_prog': $!\n"); print MAIL $message; close MAIL; return; } } sub Unindent { my $unindent = shift; $unindent =~ s/^[ \t]+//gm; return $unindent; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Email With Attachments
by Anonymous Monk on Jul 07, 2001 at 02:28 UTC |