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

Hi, I did read a couple of documentation on Base64 module in perl. I have a problem where i have to encode a file and then send it as an email to a notes client; and at the notes client i would want to decode the file which will be sent as an attachment. How do i go about doing this? Please help me! If u suggest some reading please do let me know the documents. Thanks, Nisha

Replies are listed 'Best First'.
Re: Base64 encoding
by Corion (Patriarch) on Dec 01, 2005 at 08:07 UTC

    What you want to use for the sending side of your problem is to use MIME::Lite, which easily encodes and sends attachments via mail.

    For the receiving side, it depends on how you can automate your Lotus Notes client. On Windows, you can access the Lotus Notes object model, which will give you access to the mail and all attachments easily. On other operating systems without proper RPC/IPC mechanisms, you might have to connect to the Domino server via an IMAP or POP3 interface and then use MIME::Parser to parse out the attachments again.

Re: Base64 encoding
by inman (Curate) on Dec 01, 2005 at 09:08 UTC
    You need to concentrate on the e-mail sending side of things. Notes (from R5 upwards) has been able to handle external e-mail (including HTML and attachments) as part of the default mail gateway.

    Use a minimal test rig to work with various Mail:: modules. Configure a Domino server to run the SMTP task and then use this as your SMTP gateway. You can deliver mail to a mailbox on the same server and examine it immediately.

    I have used Mail::Sender with a Domino environment in order to send status messages. I haven't worked with attachments but the docs say that it can.

Re: Base64 encoding
by zentara (Cardinal) on Dec 01, 2005 at 14:25 UTC
    Here is basically how you would encode it, although it may not be the best method of using sendmail. It shows how it is done manually, so you can appreciate how modules make it easier. The second script, shows how MIME::Lite does it.
    #!/usr/bin/perl use strict; use warnings; use MIME::Base64; my $SENDFILE = 'z.jpg'; my $FROMUSER = 'zentara'; my $FROMEMAIL = 'zentara@zentara.zentara.net'; my $TOUSER = 'zentara'; my $TOEMAIL = 'zentara@zentara.zentara.net'; open(F_MAIL,"|/usr/sbin/sendmail -t"); my $boundary = "----------90125"; print F_MAIL <<END_OF_MAIL; Precedence: list From: $FROMUSER <$FROMEMAIL> To: $TOUSER <$TOEMAIL> MIME-Version: 1.0 Subject: File attachment test Content-Type: multipart/mixed; boundary=\"$boundary\" This is a multi-part message in MIME format. --$boundary Content-Type: text/plain; charset=\"iso-8859-1\" Here is the body of the message. A file attachment is also provided b +elow. --$boundary Content-Type: application/octet-stream; name=\"$SENDFILE\" Content-Transfer-Encoding: Base64 Content-Disposition: attachment; filename=\"$SENDFILE\" END_OF_MAIL #open(F_SEND,$SENDFILE) || &Error("Cannot open sendfile for MIME encod +ing"); open(F_SEND,$SENDFILE) || die ("Cannot open sendfile for MIME encoding +"); while (read(F_SEND,my $buf, 60 * 57 ) ) { print F_MAIL encode_base64($buf); } close(F_SEND); print F_MAIL <<END_OF_MAIL; --$boundary-- END_OF_MAIL close(F_MAIL); __END__

    #########################################

    And here a module method.

    #########################################

    #!/usr/bin/perl use warnings; use strict; use MIME::Lite; $MIME::Lite::AUTO_CONTENT_TYPE="true"; my $msg = MIME::Lite->new( From => 'zentara' , To => 'zentara@z.net' , Subject => 'test attach', Type => 'multipart/mixed' ); $msg->attach( Path => './mailbody' , Filename => 'message.txt' ); $msg->attach( Path => "./logfile_07-24-2005.tar.gz" , Disposition => "attachment" ); $msg->send; print "Mission accomplished\n"; __END__

    I'm not really a human, but I play one on earth. flash japh