Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

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 examining the Monastery: (9)
As of 2019-05-24 17:00 GMT
Find Nodes?
    Voting Booth?
    Do you enjoy 3D movies?

    Results (150 votes). Check out past polls.

    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!