Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Answer: How do I send an email with Perl (including an attachment)?

by electrosphere (Beadle)
on Feb 01, 2007 at 16:03 UTC ( #597761=categorized answer: print w/replies, xml ) Need Help??

Q&A > mail and news > How do I send an email with Perl (including an attachment)? - Answer contributed by electrosphere

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" <>', '', 'Quarterly Report - Q1 2007', 'report-q12007.csv', 'text/csv', $data );

Log In?

What's my password?
Create A New User
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2016-10-28 19:28 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (386 votes). Check out past polls.