Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Send an attachment to the receipient

Posted on 1999-10-06
29
Medium Priority
?
319 Views
Last Modified: 2012-06-27
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
Comment
Question by:Dooj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 17
  • 11
29 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 2105619
#Yes you can.
use MIME::Lite;
#can even help you with headers and encoding.
0
 

Author Comment

by:Dooj
ID: 2105627
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
 

Author Comment

by:Dooj
ID: 2105635
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Dooj
ID: 2105644
ozo,

As I suspected, this module is not installed? Anyother way out???
0
 
LVL 84

Expert Comment

by:ozo
ID: 2105664
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
 

Author Comment

by:Dooj
ID: 2105676
ozo,

I dont think I have the proper rights to install the module. How do I stream to sendmail??
0
 
LVL 84

Expert Comment

by:ozo
ID: 2105722
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
 

Author Comment

by:Dooj
ID: 2105845
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
 
LVL 5

Expert Comment

by:prakashk021799
ID: 2107090
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
 

Author Comment

by:Dooj
ID: 2108329
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
 
LVL 84

Expert Comment

by:ozo
ID: 2108360
if there are non-ASCII characters in the file, you may have to use
Content-Transfer-Encoding: base64
0
 
LVL 84

Expert Comment

by:ozo
ID: 2108367
#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
 

Author Comment

by:Dooj
ID: 2108393
ozo,

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

Cheers
0
 
LVL 84

Expert Comment

by:ozo
ID: 2108567
if your recipients mail reader understands uuencode
0
 

Author Comment

by:Dooj
ID: 2108574
how do i uuencode a file and then pipe it to sendmail??
0
 
LVL 84

Expert Comment

by:ozo
ID: 2108651
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
 

Author Comment

by:Dooj
ID: 2108730
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
 

Author Comment

by:Dooj
ID: 2114943
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
 
LVL 84

Expert Comment

by:ozo
ID: 2115027
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
 

Author Comment

by:Dooj
ID: 2115032
So, whats the solution?
0
 
LVL 84

Expert Comment

by:ozo
ID: 2115055
#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
 

Author Comment

by:Dooj
ID: 2115059
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
 

Author Comment

by:Dooj
ID: 2115078
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
 
LVL 84

Expert Comment

by:ozo
ID: 2115093
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
 

Author Comment

by:Dooj
ID: 2115123
ozo,

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

Cheers, :)
0
 

Author Comment

by:Dooj
ID: 2115125
ozo, can you please post your answer so that I can grade it.

Cheers,
0
 
LVL 84

Accepted Solution

by:
ozo earned 800 total points
ID: 2115150
$file_content = pack("u",$file_content);
$content_length = length $file_content;
0
 

Author Comment

by:Dooj
ID: 2115378
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
 

Author Comment

by:Dooj
ID: 2115419
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question