Skip to content

Instantly share code, notes, and snippets.

@austinginder
Created October 1, 2012 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save austinginder/3813257 to your computer and use it in GitHub Desktop.
Save austinginder/3813257 to your computer and use it in GitHub Desktop.
portion of the tp_mail.pl
# Now that we have sent the text part its time to send the attachment..
if ($MAIL{'ftype'} and $MAIL{'ahandle'})
{
print S "--$boundary1$n";
print "--$boundary1$n" if $MAIL{'debug'};
if ( $MAIL{'ftype'} =~ /^text/)
{
print S "Content-Type: $MAIL{'ftype'}; charset=us-ascii; name=\"$MAIL{'aname'}\"$n";
print "Content-Type: $MAIL{'ftype'}; charset=us-ascii; name=\"$MAIL{'aname'}\"$n" if $MAIL{'debug'};
print S "Content-Transfer-Encoding: 7bit$n";
print "Content-Transfer-Encoding: 7bit$n" if $MAIL{'debug'};
print S "Content-Disposition: attachment; filename=\"$MAIL{'aname'}\"$n$n" ;
print "Content-Disposition: attachment; filename=\"$MAIL{'aname'}\"$n$n" if $MAIL{'debug'};
while ($bytesread=read($MAIL{'ahandle'},$buffer,1024))
{
print S $buffer;
print $buffer if $MAIL{'debug'};
}
}
else
{
print S "Content-Type: $MAIL{'ftype'}; name=\"$MAIL{'aname'}\"$n";
print "Content-Type: $MAIL{'ftype'}; name=\"$MAIL{'aname'}\"$n" if $MAIL{'debug'};
print S "Content-Transfer-Encoding: base64$n";
print "Content-Transfer-Encoding: base64$n" if $MAIL{'debug'};
print S "Content-Disposition: attachment; filename=\"$MAIL{'aname'}\"$n$n";
print "Content-Disposition: attachment; filename=\"$MAIL{'aname'} $MAIL{'ahandle'}\"$n$n" if $MAIL{'debug'};
$/=0;
binmode $MAIL{'ahandle'};
#if ($^O eq 'NT' or $^O eq 'MSWin32');
while ($bytesread=read($MAIL{'ahandle'},$buffer,1024))
{
print S &encode_base64($buffer);
print &encode_base64($buffer) if $MAIL{'debug'};
}
print "Bytes readd = $bytesread" if $MAIL{'debug'};
}
close $MAIL{'ahandle'};
unlink $MAIL{'ahandle'};
print S "$n--$boundary1--$n";
print "$n--$boundary1--$n" if $MAIL{'debug'};
}
print S "$n.$n";
print "$n.$n" if $MAIL{'debug'};
print
return 0 unless (&___wait_for('250'));
print "quit$n" if $MAIL{'debug'};
print S "quit\n";
return 0 unless (&___wait_for('221'));
close(S);
} # End of mail
#
# $--------------------------------------------------------------------------------------------
#
# S U B - E N C O D E B A S E 6 4
#
# $--------------------------------------------------------------------------------------------
#
sub encode_base64 #($)
{
my ($res, $eol, $padding) = ("", "$n", undef);
while (($_[0] =~ /(.{1,45})/gs))
{
$res .= substr(pack('u', $1), 1);
chop $res;
}
$res =~ tr#` -_#AA-Za-z0-9+/#; # ` help emacs
$padding = (3 - length($_[0]) % 3) % 3; # fix padding at the end
$res =~ s#.{$padding}$#'=' x $padding#e if $padding; # pad eoedv data with ='s
$res =~ s#(.{1,76})#$1$eol#g if (length $eol); # lines of at least 76 characters
return $res;
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment