LPM: Email attachments

Steve Lane sml at zfx.com
Wed Mar 22 13:44:03 CST 2000


Rich Bowen wrote:
> 
> Any of you guys aware of a module that handles email attachments simply?
> I don't really want to have to use something like MIME::Lite to build
> the message myself, but I suppose I could. I was hoping that there was a
> Mail::Something module that would just let me do something like:
> 
> $mail = new Mail::Something;
> $mail->attach("/path/to/file");
> $mail->recipient('rbowen at rcbowen.com');
> $mail->send;
> 
> Or something along those lines.
> 
> Any suggestions?

i've had great luck with MIME::Entity.  here's a complete example
of a message with some text and an optional attachment:

  use MIME::Entity;
  my $mail = build MIME::Entity (
    From         => $from,
    To           => $to,
    Bcc          => $bcc,
    Subject      => param('subject'),
    Data         => $data,
  );

  # add the attachment, if present
  if (my $file = param('file')) {
    my $filename = (split m|[/\\]|, $file)[-1];
    my($data, $buffer);
    while (read($file, $buffer, 1024)) { $data .= $buffer }
    $mail->attach(Data     => [$data],
                  Type     => upload_mimetype('file'),
                  Filename => $filename,
                  Encoding => '-SUGGEST');
  }

  # mail the message
  open MAIL, "|/usr/sbin/sendmail -oi -t" or die "can't open sendmail:
$!";
  $mail->print(\*MAIL);
  close MAIL or die "can't close sendmail: $!";

this was built for a webpage form; i'm sure the 'attach' method
accepts filenames as well as core data.  one other note: i had
to twiddle the 'Encoding' param several times before it worked;
the details are in the MIME::Entity manpage, but they're less-than-clear
(last time i looked).

good luck, Steve
--
Steve Lane <sml at zfx.com>



More information about the Lexington-pm mailing list