Loading ...
Sorry, an error occurred while loading the content.
 

How to send an email attachment using PERL?

Expand Messages
  • hereitis@netscape.net
    Any pointers? My requirement is just to send a zip file to some email receipient. I don t want any modules to include. If somebody let me know the sample MIME
    Message 1 of 8 , Nov 29, 2001
      Any pointers?

      My requirement is just to send a zip file to some email receipient.

      I don't want any modules to include.

      If somebody let me know the sample MIME email message for sending a
      zip file and some text and how to compose that message in perl.

      I'm not expecting any sendmail program.

      Thanks in advance
      Kiran
    • ramprasad@netcore.co.in
      You should not be afraid of including modules CPAN module Mime::Lite provides a single line interface to send mail with an attachement If you dont want still
      Message 2 of 8 , Nov 30, 2001
        You should not be afraid of including modules
        CPAN module Mime::Lite provides a single line interface to send
        mail with an attachement
        If you dont want still to use any of these try using command
        line uuencode-decode


        --- In perl-beginner@y..., hereitis@n... wrote:
        > Any pointers?
        >
        > My requirement is just to send a zip file to some email receipient.
        >
        > I don't want any modules to include.
        >
        > If somebody let me know the sample MIME email message for sending a
        > zip file and some text and how to compose that message in perl.
        >
        > I'm not expecting any sendmail program.
        >
        > Thanks in advance
        > Kiran
      • Godwin Stewart
        ... You have to MIMEify the message yourself. The basic format for this is as follows: From: Your name To: Recipient s name
        Message 3 of 8 , Nov 30, 2001
          On Fri, 30 Nov 2001 01:03:38 -0000, hereitis@... wrote:

          > If somebody let me know the sample MIME email message for sending a
          > zip file and some text and how to compose that message in perl.

          You have to MIMEify the message yourself.

          The basic format for this is as follows:

          From: "Your name" <your_e-mail@...>
          To: "Recipient's name" <their_e-mail@...>
          Date: Fri, 30 Nov 2001 10:21:00 +0100
          Subject: E-mail MIMEified in Perl
          Content-Type: multipart/mixed;
          boundary="unique_string_generated_randomly"

          This is a MIME-encoded e-mail. If you can read this text you should
          think seriously about upgrading your e-mail client!

          --unique_string_generated_randomly
          Content-Type: text/plain; charset="US-ASCII"
          Content-Transfer-Encoding: 7bit

          Here is the .zip attachment you asked for...

          --unique_string_generated_randomly
          Content-Type: application/zip; name="filename_without_path"
          Content-Transfer-Encoding: base64
          Content-Disposition: attachment; filename="filename_without_path"

          base64-encoded data goes here
          ...
          ...
          ...
          ...

          --unique_string_generated_randomly--


          Explanations (based on a Linux system):

          The date string has to comply to RFC-822, you can obtain such a string (on a
          Linux system at least) by piping the output of "date -R" into a stream:

          open( DATESTREAM, "date -R |" );
          chomp($DATEHEADER = "Date: " . <DATESTREAM>);
          close(DATESTREAM);

          The boundary string used to separate the various MIME sections in the e-mail
          should be unique to your e-mail. A good way to do that is to base it on your
          hostname and the Unix time.

          open( HOSTSTREAM, "hostname |" );
          chomp($MIMEBOUNDARY = time() . "__" . <HOSTSTREAM>);
          close(HOSTSTREAM);

          The .ZIP file can be base64-encoded using the "uuencode" utility.
          # uuencode -m file name
          ...will base64-encode the file "file" and name it "name". The first line of
          the output will be:
          begin-base64 <mode> name

          Subsequent lines will be the encoded data itself, except the last line which
          will be:
          ====

          This makes it easy enough to filter the output in order to keep just the
          encoded data:

          $FILETOENCODE="/home/user/documents/zipfiles/sendthis.zip";
          @FULLPATH = split /\//, $FILETOENCODE;
          $FILEWITHOUTPATH = $FULLPATH[scalar(@FULLPATH)-1];

          open(B64STREAM,"uuencode -m $FILETOENCODE $FILEWITHOUTPATH |");
          while ($B64LINE = <B64STREAM>) {
          next if $B64LINE =~ m/^begin-base64/;
          last if $B64LINE =~ m/={4}/;
          do_something_with($B64LINE);
          }
          close(B64STREAM);

          --
          ____________________________________________
          | G. Stewart -- gstewart@... |
          | Port de Pontille, FR-37500 CHINON, FRANCE. |
          ____________________________________________
        • hereitis@netscape.net
          Thanks Godwin & Ramprasad. The reason don t want any modules is because I m running my perl scripts on tripod.com server. They have restricted use of sending
          Message 4 of 8 , Nov 30, 2001
            Thanks Godwin & Ramprasad.

            The reason don't want any modules is because I'm running my perl
            scripts on tripod.com server. They have restricted use of sending
            mails. I can only send mails using their module which does not have
            support for MIME attachments.

            Godwin, As you have told, I'm constructing the MIME message myself
            only. but I'm having some trouble using it. Can you tell me whether
            its absolutely necessary to encode the file? Can I not send the file
            as binary itself? I was trying to send image file as a start.

            Here is some explaination about their module:
            Their module (TripodMail) uses a template which is a text file which
            I have created "testemail.txt". It contains some variable references
            which gets replaced by actual values when I call the module function
            sendMail.

            I've listed the code and the template file as below:

            Template file:
            --------------------------------------------------------------------
            To: $to
            From: $from
            Subject: Test MIME Email
            MIME-Version: 1.0
            Content-Type: multipart/mixed;boundary=end;
            --end
            Content-Type: image/gif;
            Content-Disposition: attachment;
            filename="one.gif"

            $logdata--end--
            --------------------------------------------------------------------

            Code:
            -----------------------------------------------------------------
            #!/usr/local/bin/perl

            require TripodMail;
            $MAIL = new TripodMail;

            $profile{to} = 'me@...';
            $profile{from} = 'me@...';

            $mail_template = "./testemail.txt";
            $logfile = "./index.gif";
            open(LOG, "<$logfile") || die "Can't open $logfile: $!";
            while (<LOG>)
            {
            $logdata = $logdata.$_;
            }
            close(LOG);
            %variables = (logdata =>$logdata, to => $profile{to}, from =>
            $profile{from});

            $MAIL->sendMail($mail_template, \%variables);

            exit;
            --------------------------------------------------------------------


            It did work in a sense that it sent a mail with attachment BUT it
            changed CR characters in the attachment to CRLF which corrupted the
            file :o( I suspect the error is in how I read the binary file which
            should not be.
            while (<LOG>)
            {
            $logdata = $logdata.$_;
            }
            close(LOG);
            Can you suggest alternative method.

            Thanks for going through all my mail :o)

            Kiran


            --- In perl-beginner@y..., Godwin Stewart <gstewart@s...> wrote:
            > On Fri, 30 Nov 2001 01:03:38 -0000, hereitis@n... wrote:
            >
            > > If somebody let me know the sample MIME email message for sending
            a
            > > zip file and some text and how to compose that message in perl.
            >
            > You have to MIMEify the message yourself.
            >
            > The basic format for this is as follows:
            >
            > From: "Your name" <your_e-mail@d...>
            > To: "Recipient's name" <their_e-mail@d...>
            > Date: Fri, 30 Nov 2001 10:21:00 +0100
            > Subject: E-mail MIMEified in Perl
            > Content-Type: multipart/mixed;
            > boundary="unique_string_generated_randomly"
            >
            > This is a MIME-encoded e-mail. If you can read this text you should
            > think seriously about upgrading your e-mail client!
            >
            > --unique_string_generated_randomly
            > Content-Type: text/plain; charset="US-ASCII"
            > Content-Transfer-Encoding: 7bit
            >
            > Here is the .zip attachment you asked for...
            >
            > --unique_string_generated_randomly
            > Content-Type: application/zip; name="filename_without_path"
            > Content-Transfer-Encoding: base64
            > Content-Disposition: attachment; filename="filename_without_path"
            >
            > base64-encoded data goes here
            > ...
            > ...
            > ...
            > ...
            >
          • Godwin Stewart
            ... No. It *has* to be encoded. It can be either encoded in base64 as a MIME section, or uuencoded inline. Both encoding methods convert the 8-bit binary data
            Message 5 of 8 , Dec 1, 2001
              On Sat, 01 Dec 2001 00:50:40 -0000, hereitis@... wrote:

              > Godwin, As you have told, I'm constructing the MIME message myself
              > only. but I'm having some trouble using it. Can you tell me whether
              > its absolutely necessary to encode the file? Can I not send the file
              > as binary itself? I was trying to send image file as a start.

              No. It *has* to be encoded. It can be either encoded in base64 as a MIME
              section, or uuencoded inline. Both encoding methods convert the 8-bit binary
              data to 7-bit text. This is imposed by RFC-822, "Standard for ARPA Internet
              Text Messages", the key word here being "Text".

              MIME - Multipurpose Internet Mail Extensions - are just an extension of
              RFC-822. The format is governed by another RFC document, RFC-1521 IIRC.

              BTW, well done for the "MIME-Version: 1.0" header, I forgot that!

              --
              ____________________________________________
              | G. Stewart -- gstewart@... |
              | Port de Pontille, FR-37500 CHINON, FRANCE. |
              ____________________________________________
            • hereitis@netscape.net
              Hi Godwin, Thanks again.. I know very little about all those RFC standards :o( Let me tell you my requirement. I ve a web-site and my site visitors input their
              Message 6 of 8 , Dec 2, 2001
                Hi Godwin,
                Thanks again..
                I know very little about all those RFC standards :o(
                Let me tell you my requirement.
                I've a web-site and my site visitors input their email address,
                and then I've to send a zip file to their e-mail address.

                So my mail message once encoded can be a static text file except
                the "To" part. Could you please provide me the RAW MIME code
                with a sample uuencoded or base64 data?

                Also I work on windows NT. How can I encode the zip file in
                the respective encoding format?

                Kiran

                --- In perl-beginner@y..., Godwin Stewart <gstewart@s...> wrote:
                > On Sat, 01 Dec 2001 00:50:40 -0000, hereitis@n... wrote:
                >
                > > Godwin, As you have told, I'm constructing the MIME message
                myself
                > > only. but I'm having some trouble using it. Can you tell me
                whether
                > > its absolutely necessary to encode the file? Can I not send the
                file
                > > as binary itself? I was trying to send image file as a start.
                >
                > No. It *has* to be encoded. It can be either encoded in base64 as a
                MIME
                > section, or uuencoded inline. Both encoding methods convert the 8-
                bit binary
                > data to 7-bit text. This is imposed by RFC-822, "Standard for ARPA
                Internet
                > Text Messages", the key word here being "Text".
                >
                > MIME - Multipurpose Internet Mail Extensions - are just an
                extension of
                > RFC-822. The format is governed by another RFC document, RFC-1521
                IIRC.
                >
                > BTW, well done for the "MIME-Version: 1.0" header, I forgot that!
                >
                > --
                > ____________________________________________
                > | G. Stewart -- gstewart@s... |
                > | Port de Pontille, FR-37500 CHINON, FRANCE. |
                > ____________________________________________
              • Godwin Stewart
                ... The easiest thing would be for you to write (without sending) an e-mail with the attachment you want to send. Then look at the source of that e-mail. Which
                Message 7 of 8 , Dec 2, 2001
                  On Sun, 02 Dec 2001 19:40:44 -0000, hereitis@... wrote:

                  > Could you please provide me the RAW MIME code with a sample uuencoded or
                  > base64 data?

                  The easiest thing would be for you to write (without sending) an e-mail with
                  the attachment you want to send. Then look at the source of that e-mail.
                  Which MUA are you using? The keystroke in Outlook Express (and possibly
                  Outlook too) for getting at the e-mail source is Ctrl-F3, Netscape's is
                  Ctrl-U.

                  Strip out all of the headers except "From:", "To:", "Subject:",
                  "Content-Type:" and "MIME-Version:". If you don't put a "Date:" header in,
                  the SMTP server *should* add it for you.

                  --
                  ____________________________________________
                  | G. Stewart -- gstewart@... |
                  | Port de Pontille, FR-37500 CHINON, FRANCE. |
                  ____________________________________________
                • k_0_b
                  OK. thanks godwin. my job is done!! I searched for base64 encoder and tried sending the mail and it worked :o). If anybody needs help regarding this, let me
                  Message 8 of 8 , Dec 5, 2001
                    OK. thanks godwin. my job is done!!
                    I searched for base64 encoder and tried sending the mail and it
                    worked :o).

                    If anybody needs help regarding this, let me know. I'll put
                    the details when I got time.

                    Thanks again to all of you who helped me in this.

                    -Kiran


                    --- In perl-beginner@y..., Godwin Stewart <gstewart@s...> wrote:
                    > On Sun, 02 Dec 2001 19:40:44 -0000, hereitis@n... wrote:
                    >
                    > > Could you please provide me the RAW MIME code with a sample
                    uuencoded or
                    > > base64 data?
                    >
                    > The easiest thing would be for you to write (without sending) an e-
                    mail with
                    > the attachment you want to send. Then look at the source of that e-
                    mail.
                    > Which MUA are you using? The keystroke in Outlook Express (and
                    possibly
                    > Outlook too) for getting at the e-mail source is Ctrl-F3,
                    Netscape's is
                    > Ctrl-U.
                    >
                    > Strip out all of the headers except "From:", "To:", "Subject:",
                    > "Content-Type:" and "MIME-Version:". If you don't put a "Date:"
                    header in,
                    > the SMTP server *should* add it for you.
                    >
                    > --
                    > ____________________________________________
                    > | G. Stewart -- gstewart@s... |
                    > | Port de Pontille, FR-37500 CHINON, FRANCE. |
                    > ____________________________________________
                  Your message has been successfully submitted and would be delivered to recipients shortly.