Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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

( #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)? 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 studying the Monastery: (5)
As of 2016-06-25 00:26 GMT
Find Nodes?
    Voting Booth?
    My preferred method of making French fries (chips) is in a ...

    Results (322 votes). Check out past polls.