Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

use sendmail to send file as attachment 1

Status
Not open for further replies.

random260

Programmer
Mar 11, 2002
116
US
Searched the forum but got no hits on how to do this. I have a script that easily sends mail with information to a user, but how do I attach a small existing file to the mail? I collect information via HTML form the print it to mail like this

my $name = param("name");
print MAIL "Contact name: $name\n";

then send it like this

send_mail('myname@mydomain.com',
$return,
$subject);

somehow there must be a simple way to attach a small file with this (as an attachment, not simply include the contents of the file in the message). For example the file data.txt stored at

/
I would appreciate a complete example of how to do this.
thanks
Steve
 
Code:
#!usr/bin/perl
use Socket;
use MIME::Entity;
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime();$pid=$$;
local %form = &get_form_data;
$File = "P$hour$min$sec$year$yday-$pid.htm"; #change this to your data file
    &writefile;
    &sendemail;  
    print "Content-Type: text/html\n\n";
    print &quot;<html><head><title>Thanks for your interest</title>\n</head><body><font face=\&quot;Verdana\&quot; size=\&quot;2\&quot; color=\&quot;#008000\&quot;>
    We thank you for your query & it has been queued<br>
    We will be in touch at the earliest convenience<br>
    Please allow up to 3 working days for a reply<br><br>
    Thank you for your query<hr>
    Click on the menu to the left to navigate your way around the site,</body>&quot;;
   

&sendemail;

sub sendemail {

local($print_config,$key,$sort_order,$sorted_field,$env_report); 
    $mailprog = '/usr/sbin/sendmail';
    $auto_type = 'text/plain';
    $auto_encoding = 'base64';
    $auto_body = 'body.txt';
    $message_type = 'multipart/mixed'; 
    $log_or_email = 'B';
    $email_log = 'automail_lite.log';
    $Body=&quot;Guess what&quot;;
    $top = build MIME::Entity Type => $message_type,
                          From => &quot;webserver\@yourdomain.com&quot;,
                          To => &quot;user\@mailbox.com&quot;,
                          Subject => &quot;New query from webserver&quot;; 
    attach $top Data=> $Body;
    attach $top Path => &quot;$File&quot;,Type => $auto_type,Encoding => $auto_encoding; 
    open MAIL, &quot;|$mailprog -t -i&quot; or die &quot;open: $!&quot;;
        $top->print(\*MAIL);
    close MAIL; 
    my @file =&quot;$File&quot;; #comment these out if you go ahead with your file
    unlink @file; # and this one
}
sub writefile {
    open TXT, &quot;>$File&quot;;
    print TXT &quot;<html>\n&quot;;
    print TXT &quot;<body>\n&quot;;
    print TXT &quot;<table width=\&quot;60%\&quot;>\n&quot;;
    print TXT &quot;   <tr>\n&quot;;
    print TXT &quot;     <td>Name:</td><td>$form{'name'}</td>\n&quot;;
    print TXT &quot;   </tr>\n&quot;;
    print TXT &quot;   <tr>\n&quot;;
    print TXT &quot;     <td>Telephone</td><td>$form{'tel'}</td>\n&quot;;
    print TXT &quot;   </tr>&quot;;
    print TXT &quot;   <tr>&quot;;
    print TXT &quot;     <td>email</td><td>$form{'fax'}</td>\n&quot;;
    print TXT &quot;   </tr>\n&quot;;
    print TXT &quot;   <tr>&quot;;
    print TXT &quot;     <td>email</td><td>$form{'email'}</td>\n&quot;;
    print TXT &quot;   </tr>\n&quot;;
    print TXT &quot;   <tr>&quot;;
    print TXT &quot;     <td>email</td><td>$form{'query'}</td>\n&quot;;
    print TXT &quot;   </tr>\n&quot;;
    print TXT &quot;   <tr>\n&quot;;
    print TXT &quot;     <td>interest</td><td>$form{'interest'}</td>\n&quot;;
    print TXT &quot;   </tr>\n&quot;;
    print TXT &quot;</table>\n&quot;;
    print TXT &quot;</body>\n&quot;;
    print TXT &quot;</html>\n&quot;;
 }
 sub get_form_data {
         my $temp;
         my $buffer;
         my @data;
         my $count = 0;
         read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
         foreach $temp (split(/&|=/,$buffer)) {
                 $temp =~ tr/+/ /;
                 $temp =~ s/%([0-9a-fA-F]{2})/pack(&quot;c&quot;,hex($1))/ge;
         $temp =~ s/[\r\n]/ /g;
                 push @data, $temp;
         }
         foreach $temp (split(/&|=/,$ENV{'QUERY_STRING'})) {
                 $temp =~ tr/+/ /;
                 $temp =~ s/%([0-9a-fA-F]{2})/pack(&quot;c&quot;,hex($1))/ge;
         $temp =~ s/[\r\n]/ /g;
                 push @data, $temp;
         }
         return @data;
 }

--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
If that's the SIMPLE response, i'm glad i didn't ask for the COMPLICATED one lol
 
Theres' 4 components to this

Main program
calls &quot;writefile&quot; which you can just point to your static file & forget about
Calls sendemail #->Guess what this does??
Calls get_form_data - this is to parse the data from your form

You did say COMPLETE, and if you look at it, it is simple

Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Hello Paul,
I am trying to implement your suggestion above on my website. However, my company website is running on a Windwos 2000 server. I do have Active Perl installed, but when I tried to implement your suggestion above I recieved the following error:

CGI Error
The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are:


Can't locate MIME/Entity.pm in @INC (@INC contains: C:/Perl/lib C:/Perl/site/lib .) at D:\ line 4.
BEGIN failed--compilation aborted at D:\ line 4.


Is there a work around for this problems in a Windows/Active Perl enviroment.

Any help would be greatly appreciated
Sincerely,
Dave Hartman
 
Get off a Windows host is one way...

No but seriously, get off a Windows Host, .... just kidding (rmi)

On an M$ environment, the sendmail program is not included, just another way for a compnay like those mentioned here, to p********ss people off, and still say they're open 4 business.

You're running W2k, you'll more than likely need something like blat, unless you have access to another server with a sendmail program installed.

Someone who digs, will always get answers...

HTH
--Paul

PS Bill G, if that's you I hope it doesn't affect our reverse flotation agreement.

PPS Bill, My lawyers are pissed, not annoyed, just inebriated, what did you do to them (allegedly, advisedly)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top