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; }

In reply to Re: Email With Attachments by tachyon
in thread Email With Attachments by geekuskhan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.