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

I am having an issue with attaching 2 attachments one xip and other excel file with email and sending using perl sendmail. I tried to look on various forums and everywhere i get advice using MIME :: Lite. Unfortunately i cannot use it . I have tried to write a program on my own to send 2 attachments and sending using perl sendmail but program is not working. It is reading only first file for attachment purposes and ignores second one.I would appreciate if anyone can pinpoint error with my program.
use strict; use warnings; use Mail::Sendmail; use MIME::QuotedPrint; use MIME::Base64; my $file1 = 'c:\test1.xls'; my $file2 = 'c:\sample.zip'; my $file3 = $file2 || $file1; # file to attach my %mail = ( To => "abc@yahoo.com", From => "def@yahoo.com", Cc => "hij@yahoo.com", Subject => "Two Attachments", smtp => 'mac.abc.com' ); my $content; { local $/ = undef; # slurp file open IN, $file3 or die "Error opening $file3: $!"; binmode IN; $content = <IN>; $mail{body} += encode_base64(<IN>); close IN; } my $boundary = "====" . time() . "===="; $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\""; my $message = encode_qp( $email_body ); my $len = length $$mail{body}; $mail{body} = <<EOD; $boundary Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable $message $boundary Content-Type: application/octet-stream; name="$file3" Content-Disposition: attachment; filename="$file3" Content-Transfer-Encoding: base64 Content-Length: $len $mail{body} --$boundary Content-Type: application/vnd.ms-excel; name="$file3" Content-Disposition: attachment; filename="$file3" Content-Transfer-Encoding: base64 Content-Length: $len $mail{body} $boundary-- END_OF_BODY sendmail(%mail) or die $Mail::Sendmail::error; open(OUT, ">>$sendmail_log") or die "Cannot open LOG file $file3: $!"; print OUT $Mail::Sendmail::log; print OUT "\n========================================================= +=====================\n"; close(OUT);
I cannot use mime :: lite....please help me in debugging above code.

Replies are listed 'Best First'.
Re: Issue with 2 attachments in email using perl sendmail
by Corion (Patriarch) on Jul 20, 2008 at 21:06 UTC

    As you were told in the CB already, you gave no reason why you can't use MIME::Lite. Until you give us more explanation of why you can't use it, please see the following links which all provide avenues for satisfactory solutions:

    1. A guide to installing modules for Win32
    2. Sending attachments in emails using SMTP - this is somebody who wants to solve the same problem you have

    Basically, you can always cut'n'paste all of the code of MIME::Lite into your program and it will be just the same as if you had MIME::Lite installed. So you'll have to work more to convince us that MIME::Lite is not the solution for you.

Re: Issue with 2 attachments in email using perl sendmail
by almut (Canon) on Jul 20, 2008 at 21:04 UTC
    my $file1 = 'c:\test1.xls'; my $file2 = 'c:\sample.zip'; my $file3 = $file2 || $file1; # file to attach

    As you're using logical 'or', and $file2 is true (non-empty), $file3 will be $file2. And $file3 is the only file you're ever opening/reading...

Re: Issue with 2 attachments in email using perl sendmail
by shmem (Chancellor) on Jul 20, 2008 at 21:41 UTC
    I tried to look on various forums and everywhere i get advice using MIME :: Lite. Unfortunately i cannot use it.

    Care to tell why? Since you are able to use MIME::Base64 and MIME::QuotedPrint it should not be too difficult to include a use MIME::Lite in there.

    If you get the advice to use MIME::Lite everywhere, why should that be different here?

    please help me in debugging above code.

    Please go over your post again and enclose the above code within <code> tags. If you want help, help us to help by making your posting readable. See Writeup Formatting Tips for details, and use the preview button.

    That said, there are several errors (uncomplete list):

    • you use the hash %mail, but later you try to access the value stored under the key 'body' as a reference - $$mail{body}
    • you try to add a string to an empty hash value.
      Wrong:
      $mail{body} += encode_base64(<IN>); # addition won't work
      That should be:
      $mail{body} = encode_base64(<IN>); # just assign it
      or if you want to append:
      $mail{body} .= encode_base64(<IN>); # just assign it
      See perlop.
    • mismatch between here-document delimiter introduction and actual delimiter:
      $mail{body} = <<EOD; <--- delimiter ... $mail{body} --$boundary Content-Type: application/vnd.ms-excel; name="$file3" Content-Disposition: attachment; filename="$file3" Content-Transfer-Encoding: base64 Content-Length: $len $mail{body} $boundary-- END_OF_BODY <-- wrong delimiter
    • in the above snippet, you include the same chunk of data twice: $mail{body}. Do you expect different attachments? Then use different data.
    • what content does $message have?

    Fix that, and add use strict; use warnings; at the top of your script! That will make perl tell you about blatant errors, so we don't have to.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      Thanks for all of oyur advices. i will try with Mime lite and get back in case of difficulty.

        If you're still having trouble getting it to work, I wrote a script sometime ago that should help ya out :) It works with multiple attached files just fine.

        Jason L. Froebe

        Blog, Tech Blog

Re: Issue with 2 attachments in email using perl sendmail
by apl (Monsignor) on Jul 20, 2008 at 21:00 UTC
    Please bracket your example with <code> and </code>. Thanks.