We help IT Professionals succeed at work.

emailing attachments

ubya308
ubya308 asked
on
Hi,

I have created a perl script which takes values from a form and adds them into a CSV file. and then emails those values to an adminstrator. What I would like it to do then is to add the CSV file as an attachment to that email.

I am using  sendmail for the email, but have very few modules with which to work.

Is there any way I can add an attachment to the email without using one of the dedicated modules.

Thanks

Mike
Comment
Watch Question

Commented:
Place the following into a file called mail.pl
~~~~~~~


use MIME::Base64;


sub sendEMail {
$fromAddress = $_[0];
$toAddress = $_[1];
$subj = $_[2];
$bodyText = $_[3];
$attName = $_[4];
$attCont = $_[5];

$|++;

$boundary="Message-Boundary-19990614";

 ##  Set up the Email Delivery Agent.
 $mailprog ="/usr/lib/sendmail";
 open(MAIL, "| $mailprog -t ") || die $!;
 print MAIL "To: $toAddress\n";
 print MAIL "From: $fromAddress\n";
 print MAIL "Subject: $subj\n";

     $filename ="$attName";
       if ($filename ne "")
     {
     print MAIL "MIME-Version: 1.0\n";
     print MAIL "Content-type: Multipart/mixed;\n";
     print MAIL "\tboundary=$boundary\n";
     print MAIL "\n";
     print MAIL "\n";
     print MAIL "--$boundary\n";
     

     ##     Print out any message sent with the email
     print MAIL "Content-type: text/plain; charset=US-ASCII\n";
     print MAIL "Content-description: Mail message body\n";
     print MAIL "Content-transfer-encoding: 7BIT\n";
     print MAIL "\n";
     print MAIL $bodyText."\n";
     print MAIL "--$boundary\n";

     ##     Print the header for that attachment.
     print MAIL "Content-type: application/octet-stream; name=\"$filename\"; type=Unknown\n";
     print MAIL "Content-transfer-encoding: BASE64\n";
     print MAIL "Content-disposition: attachment\n";
     print MAIL "\n";
     
     ##     Send out the contents!!
     $body = encode_base64("$attCont");     ##     Use base64 for encoding the contents
     print MAIL $body;
     print MAIL "\n";
     #print MAIL "--$boundary\n";

         
     ##     Indicate end of all attachments.
     print MAIL "--$boundary--\n";
     close(MAIL);
     ## End of send email
    }
    else
    {
     print MAIL "Content-type: text/plain;\n";
     print MAIL "\n";
     print MAIL "\n";
     
     print MAIL $bodyText."\n";
     close(MAIL);
     ## End of send email

    }
}


return 1;









~~~~~~~~~~

The following calls it, after reading the contents of attachment from a file

~~~~~~~~~~~

#!/usr/bin/perl

     require("mail.pl");

     print "Content-Type: text/html\n\n";


     $emailsFromAddress =  "testing\@somedomain.co.uk";
     $emailAddr = "mbailey\@somedomain.co.uk";
     $subject = "test Attachment";

#     Either read a file in or create a string
#     to send in an attachment.
#     $fileCont = "The content of the file - this could be file contents at the byte level?...";
     open(FH, "spell.zip") or die("No test file");
     $fileCont = "";
     while (<FH>)
     {
          $fileCont .= $_;    
     }
     $bodyContent = "Please find attatched a file.\n\nThis mail is automated - please DO NOT reply to it.";
     $filename = "spell.zip";

     $sent = &sendEMail("$emailsFromAddress", "$emailAddr", "$subject", "$bodyContent", "$filename", "$fileCont");

     print "Done! $sent";

~~~~~~~~


The first file uses the base64 module which is pretty standard on any PERL distribution.

The second file reads the contents of a file in - you could equally just use your CSV string as the filecontent and comment out the file reading bit...

I'm in a hurry at the moment but if you have any problems I'll have more time later,

Cheers,

Mal

Author

Commented:
You da man!

Commented:
thanks

Explore More ContentExplore courses, solutions, and other research materials related to this topic.