Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

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

by electrosphere (Beadle)
on Feb 01, 2007 at 16:03 UTC ( [id://597761]=note: print w/replies, xml ) Need Help??


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 );

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://597761]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2025-06-17 13:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.