zlinkexchange has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm at least getting an email, however the attachment isn't being attached. Originally this email was working fine with sendmail. I'm on a windows server that doesn't have sendmail apparantely so I'm using SMTP to send the email. This is what arrives in my email:
1001du.txt Content-Transfer-Encoding: binary Content-Type: multipart/mixed; boundary="boundary-144416012012" MIME-Version: 1.0 Date: 144416012012 From: sking@mortgage-pros.com To: admin@mortgagerefinancingpros.com Subject: New loan request in database (extra fields) This is a multi-part message in MIME format. --boundary-144416012012 Content-Disposition: inline Content-Length: 271 Content-Transfer-Encoding: binary Content-Type: text/plain Dear Member, Please find attached to this email message a loan application file fro +m a recent loan applicant. Be sure to save this DU 3.2 file to your +hard drive and import the data into your loan processing program. Regards, Site Administration Mortgage-pros.com --boundary-144416012012 Content-Disposition: inline; filename="1001du.txt" Content-Transfer-Encoding: base64 Content-Type: application/zip; name="1001du.txt" MTAwMWR1LnR4dA== --boundary-144416012012--
Here's the code:
sub sendfile { my $email = $_[0]; my $from = $_[1]; my $subject = $_[2]; my $message = $_[3]; my $file = $_[4]; ($date,$boundary) = &boundary; #print "Content-type: text/html\n\n"; #print $_[4]; #exit; my $file_cont=encode_base64($file); #my $file_cont=encode_base64($line); $length=length($message); $send = qq~Content-Transfer-Encoding: binary Content-Type: multipart/mixed; boundary="$boundary" MIME-Version: 1.0 Date: $date From: $from To: $email Subject: $subject This is a multi-part message in MIME format. --$boundary Content-Disposition: inline Content-Length: $length Content-Transfer-Encoding: binary Content-Type: text/plain $message --$boundary Content-Disposition: inline; filename="$file" Content-Transfer-Encoding: base64 Content-Type: application/zip; name="$file" $file_cont --$boundary-- ~; $smtp = Net::SMTP->new($mailhost); $smtp->mail($_[1]); $smtp->to($_[0]); $smtp->data(); $smtp->datasend("To: $_[0]\n"); $smtp->datasend("From: $_[1]\n"); $smtp->datasend("Subject: $_[2]\n\n"); $smtp->datasend("\n"); #$smtp->datasend("$_[3]\n"); $smtp->datasend("$_[4]\n"); $smtp->datasend("$send\n"); $smtp->dataend(); $smtp->quit; # open(MAIL,"|$mailprog -t"); # print MAIL $send; # close(MAIL); }

Replies are listed 'Best First'.
Re: mail script with attachment
by atcroft (Abbot) on Jan 16, 2012 at 23:52 UTC

    The problem that appears to be occurring is that you are Base64 encoding the file name, rather than the content of the file. This is illustrated by decoding the value that appeared in the email:

    $ perl -MMIME::Base64 -le 'print decode_base64(q{MTAwMWR1LnR4dA==});' 1001du.txt

    MIME::Base64's documentation does not indicate that it can open a file on its own, but instead encodes the stream of bytes it is given as its first argument.

    Hope that helps.

      This is what I get when using the $line variable and not the $file variable. I merely switched the two:
      Content-Transfer-Encoding: binary Content-Type: multipart/mixed; boundary="boundary-111916012012" MIME-Version: 1.0 Date: 111916012012 From: sking@mortgage-pros.com To: admin@mortgagerefinancingpros.com Subject: New loan request in database (extra fields) This is a multi-part message in MIME format. --boundary-111916012012 Content-Disposition: inline Content-Length: 271 Content-Transfer-Encoding: binary Content-Type: text/plain Dear Member, Please find attached to this email message a loan application file from a recent loan applicant. Be sure to save this DU 3.2 file to you +r hard drive and import the data into your loan processing program. Regards, Site Administration Mortgage-pros.com --boundary-111916012012 Content-Disposition: inline; filename="1033du.txt" Content-Transfer-Encoding: base64 Content-Type: application/zip; name="1033du.txt" MTAzM3x8fFNjb3R8c2tpbmdAbW9ydGdhZ2UtcHJvcy5jb218WWVzfFl8fHx8fDF8fDM2MH +x8fHx8 fHx8fHx8fHx8fHx8fHx8fHx8fHxTY290fEN8S2luZ3x8fHx8fGFzZGZ8NTY1NDY1NDY1NH +x8fDE2 NTQxMTIzfDV8fHx8fHx8fHx8fHx8MTIzIEFueSBTdHJlZXR8fEFueSBDaXR5fENBfDEyMz +Q1fHx8 fFl8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8Z3x8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fH +x8fHx8 fHx8fHNhZGZ8fHx8fHx8fHx8fDU2NDU2NTU1fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fH +x8fHx8 fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fH +x8fHx8 fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fH +x8fHx8 fHx8fHx8fHx8fHx8fHxZfFl8fHx8fHx8fHx8fHx8fHx8fHx8fHx8YXBwfHx8U3VibWl0IE +FwcGxp Y2F0aW9ufHw= --boundary-111916012012--

        But, where is $line defined?

        See $/ to slurp the file in a variable