[Webinar] Streamline your web hosting managementRegister Today

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

Attaching file to Mail

Let me surmise the case first.
1. i am sending a file using <input type=file>
    method = post enc=multipart/form-data
   to a cgi prog which uses sendmail .

2. in the cgi prog i am using MIME::Entity
    developed by Zeegee software  
The problem is that I can only attach a file which
is on the server. When working from a remote
machine I get an error saying
read-open "filename": No such file or directory at /usr/lib/perl5/site_perl/MIME/Body.pm line 407

obviously ,  the program cannot find the file.
Is the remote file not getting uploaded or am i
missing something ?
I have a skeleton code .
My code : -

use CGI qw/:standard :html3/;
use CGI::Carp qw/fatalsToBrowser set_message/;
use MIME::Entity;
print header();
print start_html (
                   -title => 'raj test mail',
print end_html();
    sub handler {
     $err = shift;
     print "<H1>Hmm.. Found an Error</h1>";
     print "<b>$err</b>";
MAIN  : {
  $query = new CGI;
  @var = $query->param();
  for $var (@var){
    ${$var} = $query->param($var);
  $top =build MIME::Entity From => $from,
                           To => $to,
                           Subject => $subj,
                           Data  => $message,
         or croak " Cannot build $top" ;
  if ( $file ){
     $top->attach( Path => $file,
                   Type => "text/html",
                   Encoding => "-SUGGEST"
 open( MAIL ,"| /usr/sbin/sendmail -t -oi") or croak "Cannot open MAIL program";
 close MAIL ;
   $subj = "unspecified";
<Body Bgcolor=white Text=black>
<Font Face='Verdana,Arial,Helvetica' Size=2>
<h1>The Mail to $to subject $subj has been sent</h1>
<b>Thank you for using raj mail</b>

<b>Thank you for using raj mail</b>

print end_html();  

  • 2
1 Solution
When you upload a file to the web server, you need to have your CGI program read the data and save it on the system where it is running. I don't see any code in your script to do this.

Also, where is the $file variable being set? If you are assuming that the file being uploaded is automatically saved by the web server, your assumption is incorrect.
The perldoc for CGI indicates that $file is the
full path to the file on the user's machine, which
is true.  It indicates that this is also a filehandle that
you can use to directly access the file, however
that is not the case as $file is not on the local
machine.  There is a function in CGI called tmpFileName and you can use that function to get the path to the temporary file.

$tmp_file = tmpFileName($file);
open FILE, $tmp_file;
while (<FILE>) {
close FILE;

CGI.pm does not automatically initialize the variable $file with the name of the uploaded file. The documentation says:

       When the form is processed, you can retrieve the entered
       filename by calling param().

              $filename = $query->param('uploaded_file');

The above assumes that 'uploaded_file' is the value of the NAME attribute of the INPUT parameter used in the form. Something like:

<INPUT TYPE="file" NAME="uploaded_file" ....>

The part about the $filename also being a handle is correct. Therefore, you can do:

while (<$filename>) {
    # process data or write it somewhere else
raj_chandranAuthor Commented:
thanks prakash,
i corrected that assumption  after posting the question
and a  walk .
Now iam opening the file  uploaded and writing the
contents to a   public dir.


Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

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