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

Hi,

I need to send email from a cgi tha will allow attachments with the following extensions (.txt, .html, .doc) plus it needs to have some data from a form in the body of the message. . I think that Mime::Lite would be the easiest way to do thi, but unfortunately it is not installed on the server and I dont have the power to get it installed. I do have mime tools though, and I'm sure it will do the job, but I seem to be having some trouble. Does any one know where I can find a really clear example of how this is done.

Here is what I have so far:

$top = MIME::Entity->build(Type =>"multipart/mixed",<br> From => "$name",<br> To => "$in{email_address}",<br> Cc => "$in{from}",<br> Subject => "$in{job_title}");<br> $top->attach(Path=>"$file_path"); <br> $top->attach(Data=>$name); <br> $top->attach(Data=>$comment); <br> $top->attach(Data=>$city_state_zip);<br> $top->attach(Data=>$in{phone});<br> $top->attach(Data=>$in{email});<br> $top->attach(Data=>$in{education});<br> $top->attach(Data=>$in{interest});<br> $top->attach(Data=>$in{experience});<br> open (MAIL, "| /usr/lib/sendmail -t -oi -oem") or die "cant ope +n: $!"; $top->print(\*MAIL);<br> close MAIL;<br>

Thanks,

Jason

Edited by footpad, 30 June 01 - 08:15 pm (PDT)

Replies are listed 'Best First'.
Re: Email With Attachments
by tachyon (Chancellor) on Jul 01, 2001 at 06:41 UTC

    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; }
      Thanks alot this is exactly what I needed , I searched for days for a concise example with no luck. You are a life saver.
Re: Email With Attachments
by tomhukins (Curate) on Jul 01, 2001 at 17:18 UTC
Re: Email With Attachments
by Anonymous Monk on Jul 01, 2001 at 06:39 UTC