isJustMe isJustMe - 6 months ago 48x
Perl Question

is there a way to attach a PDF FILE with Net::SMTP in Perl

I'm trying to attach a pdf file on a multipart content email, yes I know i could use mime lite or a billion perl modules, but I'm limited to use perl 5.8.8 as it comes out of the box, so far I have

use Net::SMTP;
use MIME::Base64 qw( encode_base64 );
use MIME::Base64 qw( decode_base64 );
use strict;
use warnings;

my $from = '';
my $to = '';
my $to2 = '';
my $boundary = 'frontier';

open my $Initial_File, '<', "summary.pdf";
binmode $Initial_File;
open my $Initial_OutFile, '>', "temp.pdf";
my $buf;
while ( read( $Initial_File, $buf, 60 * 57 ) ) {
print $Initial_OutFile encode_base64( $buf );

close $Initial_OutFile;
close $Initial_File;

open INFILE, '<', "temp.pdf";
open my $final_output, '>',"summary2.pdf";
binmode $final_output;
my $buffer;
while ( $buffer = <INFILE> ) {
print $final_output decode_base64( $buffer );
my @pdf = $final_output;
close $final_output;
close INFILE;

my $smtp = Net::SMTP->new('');
$smtp->recipient($to,$to2, { SkipBad => 1 });
$smtp->datasend("Subject: Test Email \n");
$smtp->datasend("MIME-Version: 1.0\n");
$smtp->datasend("Content-type: multipart/mixed;\n\tboundary=".$boundary."\n");
$smtp->datasend("Content-type: text/plain\n");
$smtp->datasend("Content-Disposition: quoted-printable\n");
$smtp->datasend("\nTest From You \n");
$smtp->datasend("Content-Disposition: attachment; filename=summary2.pdf \n");
$smtp->datasend("Content-Type: application/pdf; name=summary2.pdf ");
# $smtp->quit;


The email sends out correctly, but (obviously) when trying to open the pdf file it says it has an incorrect encoding, is there a way to buffer the PDF file to the attachment in a way that it send it out as it is ?


ended up adding MIME:Lite manually to my user directory

use lib '/xx/sas/xx/perl';
use MIME::Lite;
open(SMTP,'/xx/sas/xx/perl/MIME/srv.txt') || die("Could not open the file");
my $mail_host = <SMTP>;
open(DATA, $ARGV[3] ) || die("Could not open the file");
my @csv = <DATA>;

foreach (@csv){
    $textStr.= $_;

$msg = MIME::Lite->new (
  From => $ARGV[0],
  To => $ARGV[1],
  Subject => $ARGV[2],
  Type =>'multipart/mixed'
) or die "Error creating multipart container: $!\n";

$msg->attach (
  Type => 'TEXT',
  Data => $textStr
) or die "Error adding the text message part: $!\n";

foreach my $file (split(',', $ARGV[4])) { 
        if ( $file =~ /\.gif$/i ){ $content_type ='image/gif'}
        if ( $file =~ /\.jpg$/i ){ $content_type ='image/jpeg'}
        if ( $file =~ /\.zip$/i ){ $content_type ='application/zip'}
        if ( $file =~ /\.html$/i ){ $content_type ='text/html'}
        if ( $file =~ /\.pdf$/i ){ $content_type ='application/pdf'}
        if ( $file =~ /\.xls$/i ){ $content_type ='application/'}
        if ( $file =~ /\.log$/i ){ $content_type ='application/octet-stream'}
        $msg->attach (
           Type => $content_type,
           Path => $file,
           Filename => $file,
           Disposition => 'attachment'
        ) or die "Error adding $file: $!\n";    

MIME::Lite->send('smtp', $mail_host, Timeout=>60);