http://www.perlmonks.org?node_id=597761


in reply to How do I send an email with Perl (including an attachment)?

My solution uses Mail::Send and sendmail only:

sub sendAttachment { my( $from, $to, $subject, $filename, $type, $data ) = @_; my $boundary = '==' . time . '=='; # untaint stuff for mailing $ENV{'PATH'} = '/bin:/usr/bin'; delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'}; my $msg = new Mail::Send; $msg->to($to); $msg->add('From',$from); $msg->subject($subject); $msg->add('Content-Type', qq(multipart/mixed; boundary="$boundary"; +) ); my $fh = $msg->open; print $fh "--$boundary\n"; print $fh "Content-Disposition: attachment; filename=$filename;\n"; print $fh "Content-Type: $type; name=$filename; charset=iso-8859-1; +\n"; print $fh "Content-Transfer-Encoding: 8bit;\n\n"; print $fh $data; print $fh "--$boundary\n"; $fh->close; }
Used like so:
sendAttachment( '"Me, Here" <me@here.org>', 'user@foo.com', 'Quarterly Report - Q1 2007', 'report-q12007.csv', 'text/csv', $data );