Scripting >> Perl >> How to send mail with attachment using Net : : SMTP

# Following obtained from internet discussion # Not yet tested by myself use Net::SMTP; use MIME::Base64; my $file = 'test.doc'; my $mediatype = 'msword'; open INPUT, $file or die "Can't open INPUT file $file: $!\n"; binmode INPUT; my $buffer; my $from = ''; my $to = ''; my $server = ''; my $time = sprintf ("%x", time); my $smtp = Net::SMTP->new($server) or die "Can't create SMTP object\n"; $smtp->mail($from); $smtp->to($to); my @msg = <<MSG; From: $from To: $to Subject: word document attachment MIME-Version: 1.0 Content-type: multipart/mixed; boundary="Boundary $time" --Boundary $time Content-type: text/plain This is a test email..... --Boundary $time Content-type: application/$mediatype; name="$file" Content-disposition: attachment; filename="$file" Content-transfer-encoding: base64 MSG # $smtp->data(); $smtp->datasend(@msg); $smtp->datasend(encode_base64($buffer)) while(read (INPUT, $buffer,60*57)); $smtp->datasend("\n--Boundary $time--\n"); $smtp->dataend(); $smtp->quit(); # close INPUT;