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

I am trying to send a .xlsx via SMTP. There is content in the email that is sent but i think there is something wrong with the encoding. Whenever i try to open the .xlsx from the email i get the message the file format or file extension is not valid and to verify that it has not bee corrupted.

I realized that something like mimelite might be easier but i would like to figure out the SMTP route as well.

Any help would be appreciated

my $to_email = "xxx\@imail.org"; our $smtp = Net::SMTP->new("smtp.xx.xx.com",Debug=>1); $smtp->mail("xxx\@imail.org"); $smtp->recipient("$to_email"); $smtp->data(); $smtp->datasend("To: $to_email\n"); $smtp->datasend("Cc: xxx\@imail.org\n"); $smtp->datasend("From: xxx\@imail.org\n"); $smtp->datasend("Subject: Annual Access Reviews\n"); $smtp->datasend("MIME-Version: 1.0\n"); $smtp->datasend("Content-Type: multipart/mixed; boundary= \"*B +CKTR*\"\n\n"); # Send the body. $smtp->datasend("--*BCKTR*\n"); $smtp->datasend("Content-Type: text/html\n\n"); $smtp->datasend("@$distinct_manager[3], \n\n"); $smtp->datasend("<div><b>As a manager your task at hand</b> is + the following:</div>"); $smtp->datasend("<p>These user access reviews must be complete +d and returned by 12/18/2015 XX.XX.2015(6).</p>"); $smtp->datasend("<p><b>Please email the completed spreadsheets + to emailaddress\@imail.org.</b></p>"); $smtp->datasend("<p><b>Please go to http://some_internal_websi +te on how to upload your completed spreadsheet(s).</b></p>"); $smtp->datasend("<div>Thank You,</div>"); $smtp->datasend("<div>Manager, ISSA Identity and Access Manage +ment</div>"); $smtp->datasend("\n"); $smtp->datasend("--*BCKTR*\n"); $smtp->datasend("Content-Type: application/vnd.openxmlformats- +officedocument.spreadsheetml.sheet; name=\"$attachfile\" \n"); $smtp->datasend("Content-Transfer-Encoding: base64"); $smtp->datasend("Content-Disposition: attachment; filename =\" +$attachfile\"\n\n"); $smtp->datasend("\n"); open(DAT, $attachfile) || die("Could not open text file!"); binmode(DAT); my $data = do { local $/; <DAT> }; close DAT; $smtp->datasend($data); $smtp->datasend("\n"); $smtp->datasend("\n"); $smtp->dataend(); $smtp->quit;

Replies are listed 'Best First'.
Re: Net:::SMTP and Attaching a xlsx
by Anonymous Monk on Nov 21, 2015 at 03:31 UTC

    I realized that something like mimelite might be easier but i would like to figure out the SMTP route as well.

    Its the same route, encode the attachment using appropriate mime encoding ... if you gotta ask how its done, just use the mime tools

Re: Net:::SMTP and Attaching a xlsx
by deg01004 (Initiate) on Nov 21, 2015 at 04:52 UTC

    Thank you for your reply. I may be heading in the wrong direction. I believe that xlsx needs base64 encoding. I used the following to encode the data

    .
    my $buf; use MIME::Base64 qw(encode_base64); open(FILE, "$attachfile") or die "$!"; binmode(FILE); while (<FILE>){ $buf .=$_; } $buf = encode_base64($buf); $smtp->datasend($buf);

    Is this the right direction. I did run it with the above change and recieve the same error as before

    .
      No; you're avoiding mime::tools while trying to get someone to explain what/how it does -- why dance around the knowledge you say you want? Use mimetools and see what it does, look at the source to see how

        I am definitely open to using MIME::TOOLS although i have never used it before. Looking at it it looks like i should use the MIME::Decoder.

        $decoder = new MIME::Decoder 'base64' or die "unsupported"; $decoder->encode(\*STDIN, \*STDOUT);

        I have looked for an example on how use this due to not fully understanding the document. Do you by chance have an example that i could relate to the above scenario