• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 321
  • Last Modified:

Send an attachment to the receipient

Hi,

I am trying to send an attachment thru my perl script. What is the best way to do this? can I stream the content of the file to Sendmail or something?

Cheers,
0
Dooj
Asked:
Dooj
  • 17
  • 11
1 Solution
 
ozoCommented:
#Yes you can.
use MIME::Lite;
#can even help you with headers and encoding.
0
 
DoojAuthor Commented:
ozo,

That would be great! What exactly does MIME::Lite do? Do I have to install anything for this. fyi, I use unix.

Thanks,

Dooj
0
 
DoojAuthor Commented:
ozo,

I also have to tell you that if MIME::Lite is not installed on the machine, it'd be fairly difficult for me to get it installed. If this is the case, could you suggest something else?

Cheers, Dooj
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
DoojAuthor Commented:
ozo,

As I suspected, this module is not installed? Anyother way out???
0
 
ozoCommented:
if you have CPAN.pm installed, you can just run
perl -MCPAN -e 'CPAN::Shell->install(MIME::Lite)'
or you could just stream to sendmail yourself with any appropriate headers and encodings.
0
 
DoojAuthor Commented:
ozo,

I dont think I have the proper rights to install the module. How do I stream to sendmail??
0
 
ozoCommented:
Assuming you have rights to write a perl script, you could just install to your own directory,
or do it yourself like:

open MAIL,"|sendmail -t" or die $!;
print MAIL <<END_HERE;
Content-Transfer-Encoding: binary
Content-Type: multipart/mixed; boundary="_----------=_93927818231600"
MIME-Version: 1.0
X-Mailer: MIME::Lite 1.123  (B2.11; Q2.03)
From: me@myhost.com
To: you@yourhost.com
Cc: some@other.com, some@more.com
Subject: A message with 2 parts...

This is a multi-part message in MIME format.

--_----------=_93927818231600
Content-Disposition: inline
Content-Length: 31
Content-Transfer-Encoding: binary
Content-Type: text/plain

Here's the text file you wanted
--_----------=_93927818231600
Content-Disposition: inline; filename="file.txt"
Content-Length: 167
Content-Transfer-Encoding: binary
Content-Type: text/plain; name="file.txt"

Hi,

I am trying to send an attachment thru my perl script. What is the best way
to do this? can I stream the content of the file to Sendmail or something?

Cheers,

--_----------=_93927818231600--

END_HERE
close MAIL or die $!||$?;
0
 
DoojAuthor Commented:
ozo,

Here is something I worked on (Mime::Lite was not very clear to me!!) :

#!/usr/bin/perl

use CGI;

my $cgi = new CGI;
my $file = $cgi->param('file');
$file=~m/^.*(\\|\/)(.*)/; # strip the remote path and keep the filename

while(<$file>) {
     $file_content.=$_;
}
print $file;
open(SENDMAIL,"|$SENDMAIL -f$RECIPIENT $RECIPIENT") or
      die("Unable to Send Stripped News to $RECIPIENT");

print SENDMAIL "To: $RECIPIENT\n";
print SENDMAIL "From: Testing<$RECIPIENT>\n";
print SENDMAIL "Subject: Attachment\n";
print SENDMAIL "$file_content\n\n";
close(SENDMAIL);

print $cgi->header();
print "$file has been successfully uploaded... thank you.\n";

I receive the file ok as the body of the email rather than an attachment. Could you throw some light on how I can send this as an attachment?

Thanks.
0
 
prakashk021799Commented:
You are not outputting any MIME headers in your script. In order for your file to be sent as an attachment, you should send it in MIME format.

Do something like this:

print SENDMAIL <<"MESSAGE";
To: $RECIPIENT
From: Testing<$RECIPIENT>
Subject: Attachment
Content-Disposition: attachment; filename="your-file-name"
Content-Length: <LENGTH OF YOUR FILE HERE>
Content-Transfer-Encoding: binary
Content-Type: text/plain; name="your-file-name"
MIME-Version: 1.0

$file_content
MESSAGE

close(SENDMAIL);

Make sure you supply the Content-Type, Content-Length and Content-Disposition headers appropriate values.

Also, if you copy and paste the above code, make sure there are no extra spaces at the beginning or end of the lines (especially the line ending the here document should have just the text "MESSAGE" only, no spaces surrounding it).
0
 
DoojAuthor Commented:
prakashk,

Excellent! it works when I send a text file but not a word document or an image file.

When I open the word doc, it opens up an emapty file.

Why is that?

Cheers, Dooj
0
 
ozoCommented:
if there are non-ASCII characters in the file, you may have to use
Content-Transfer-Encoding: base64
0
 
ozoCommented:
#from MIME/Lite.pm
sub encode_base64 {
    my $res = "";
    my $eol = "\n";

    pos($_[0]) = 0;        # thanks, Andreas!
    while ($_[0] =~ /(.{1,45})/gs) {
        $res .= substr(pack('u', $1), 1);
        chop($res);
    }
    $res =~ tr|` -_|AA-Za-z0-9+/|;

    # Fix padding at the end:
    my $padding = (3 - length($_[0]) % 3) % 3;
    $res =~ s/.{$padding}$/'=' x $padding/e if $padding;

    # Break encoded string into lines of no more than 76 characters each:
    $res =~ s/(.{1,76})/$1$eol/g if (length $eol);
    return $res;
}
0
 
DoojAuthor Commented:
ozo,

Can i uuencode the file and send it? base64 didnt work either.

Cheers
0
 
ozoCommented:
if your recipients mail reader understands uuencode
0
 
DoojAuthor Commented:
how do i uuencode a file and then pipe it to sendmail??
0
 
ozoCommented:
open FILE,"<$filename" or die "can't open $filename $!";
print SENDMAIL "begin 644 $filename\n";
print SENDMAIL pack'u',$buffer while( read(FILE,$buffer,45) );
print SENDMAIL "`\nend\n";

#but if you don't know how to uuencode a file and then pipe it to sendmail,
#then how did you base64 encode a file and then pipe it to sendmail?
0
 
DoojAuthor Commented:
ozo,

I did uuencode and piped it to sendmail, but it didnt work. Thats why I had to know what I am doing wrong.

I ahve tried everything and it still doesnt seem to work. I am doing something silly, somewhere. I will test again and post the code.

Cheers,
0
 
DoojAuthor Commented:
Ok,

I was trying to do something like this :

use CGI;

my $cgi = new CGI;
my $file = $cgi->param('file');
$file=~m/^.*(\\|\/)(.*)/; # strip the remote path and keep the filename
my $name = $2;

while(<$file>) {
     $file_content.=$_;
}
$content_length = $ENV{'CONTENT_LENGTH'};

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

print "$file has been successfully uploaded... thank you.\n";
print "result is $result";
print $name;
open(SENDMAIL,"/usr/bin/uuencode |$SENDMAIL -f$RECIPIENT $RECIPIENT") or
      die("Unable to Send attachment to $RECIPIENT");
print SENDMAIL <<"MESSAGE";
To: $RECIPIENT
From: Testing<$RECIPIENT>
Subject: Attachment
Content-Disposition: attachment; filename=$name
Content-Length: $content_length
Content-Transfer-Encoding: binary
Content-Type: text/plain; name=$name
MIME-Version: 1.0
$file_content;

MESSAGE
close(SENDMAIL);

Ozo, I even tried your piece of code. But it doesnt seem to work.
Whats wrong???

Cheers,

0
 
ozoCommented:
open(SENDMAIL,"/usr/bin/uuencode |$SENDMAIL -f$RECIPIENT $RECIPIENT");
#that's not going to work.
#(and even if if did pipe your print statements through uuencode into sendmail, it would also uuencode your headers, which I doubt you want to do)
0
 
DoojAuthor Commented:
So, whats the solution?
0
 
ozoCommented:
#what to you get just doing this?
open(SENDMAIL," |$SENDMAIL -t") or die("Unable to Send attachment to $RECIPIENT");
print SENDMAIL <<"MESSAGE";
To: $RECIPIENT
From: Testing<$RECIPIENT>
Subject: Attachment
Content-Disposition: attachment; filename=$name
Content-Length: $content_length
Content-Transfer-Encoding: binary
Content-Type: text/plain; name=$name
MIME-Version: 1.0

$file_content
MESSAGE
close(SENDMAIL);

#or
$file_content = pack("u",$file_content);
$content_length = length $file_content;
open(SENDMAIL," |$SENDMAIL -t") or die("Unable to Send attachment to $RECIPIENT");
print SENDMAIL <<"MESSAGE";
To: $RECIPIENT
From: Testing<$RECIPIENT>
Subject: Attachment
Content-Disposition: attachment; filename=$name
Content-Length: $content_length
Content-Transfer-Encoding: uuencode
Content-Type: text/plain; name=$name
MIME-Version: 1.0

$file_content
MESSAGE
close(SENDMAIL);
0
 
DoojAuthor Commented:
The first one sends the attachment out - but only text files can be opened. I will tryout the second piece of code and let you know,

Cheers,
0
 
DoojAuthor Commented:
ozo,

The second one WORKED!!!! Thank you so much. I was just going crazy .....

But, when I send a text file, it seems to eat up half of the first line. A DOC file is received ok.

Answer this and I will grade your answer and increase the points too!!!


Thanks again

Cheers,
:)
0
 
ozoCommented:
Sorry,

MIME-Version: 1.0
$file_content

MESSAGE

should have been

MIME-Version: 1.0

$file_content
MESSAGE


#if you want base64 encoding rather than uuencoding, try:
$res='';
pos($file_content) = 0;
while ($file_content =~ /(.{1,45})/gs) {
        $res .= substr(pack('u', $1), 1);
        chop($res);
}
$res =~ tr|` -_|AA-Za-z0-9+/|;
# Fix padding at the end:
my $padding = (3 - length($_[0]) % 3) % 3;
$res =~ s/.{$padding}$/'=' x $padding/e if $padding;
# Break encoded string into lines of no more than 76 characters each:
$res =~ s/(.{1,76})/$1\n/g;
$content_length = length $res;
open(SENDMAIL," |$SENDMAIL -t") or die("Unable to Send attachment to $RECIPIENT");
print SENDMAIL <<"MESSAGE";
To: $RECIPIENT
From: Testing<$RECIPIENT>
Subject: Attachment
Content-Disposition: attachment; filename=$name
Content-Length: $content_length
Content-Transfer-Encoding: base64
Content-Type: text/plain; name=$name
MIME-Version: 1.0

$res
MESSAGE
close(SENDMAIL);
0
 
DoojAuthor Commented:
ozo,

base 64 didnt work. I am happy with uuencoding. Thanks a lot for helping me out.

Cheers, :)
0
 
DoojAuthor Commented:
ozo, can you please post your answer so that I can grade it.

Cheers,
0
 
ozoCommented:
$file_content = pack("u",$file_content);
$content_length = length $file_content;
0
 
DoojAuthor Commented:
Just one more question :

How can I attach two files or how can send text in the body of the email as well as an attachment.

Cheers,
0
 
DoojAuthor Commented:
I think I know what I have to do. Please sont post your answer as I want to figure it out myself.

Cheers,
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 17
  • 11
Tackle projects and never again get stuck behind a technical roadblock.
Join Now