PDA

View Full Version : How to Send Attachment by PERL Script


desert
01-13-2001, 02:01 PM
Iím developing CGI script whish will allow the webmaster to send a message to any body and include Attachment with the message .

But I donít know how to attach documents with the message Iím using the following PERL code to send mail :

print MAIL "To: $receptionistmail\n";
print MAIL "From: $webname<$youremail>\n";
print MAIL "Subject: $msgtitel\n";
print MAIL "Hi $receptionistname:\n";
print MAIL "$msgbody\n";
close (MAIL);

Please Help.

desert
01-14-2001, 12:33 AM
No body knows ??!!! some thing strange.

whkoh
01-14-2001, 03:17 AM
The code is:


<FORM METHOD="post" ACTION="mailto:jburns@htmlgoodies.com" ENCTYPE="multipart/form-data">
Attachment: <INPUT TYPE="file" NAME="attachedfile" MAXLENGTH=50 ALLOW="text/*">
</FORM>


Change the ENCTYPE to the correct MIME type. A list can be found here (http://www.utoronto.ca/webdocs/HTMLdocs/Book/Book-3ed/appb/mimetype.html).




------------------
Koh Wei Han
Network Engineer
Contact: whkoh@apexmail.com , whkoh@mailandnews.com , whkoh@020.co.uk

[Edited by Jacob on 01-20-2001 at 10:53 AM]

desert
01-14-2001, 05:12 AM
Thank you : Whkoh

But in this case the message will be sent using the local mail agent, an it's not the way which I want
I want exactly as follows if possible :

print MAIL "To: $receptionistmail\n";
print MAIL "From: $webname&lt;$youremail&gt;\n";
print MAIL "Subject: $msgtitel\n";
print MAIL "Hi $receptionistname:\n";
print MAIL "$msgbody\n";
close (MAIL);

because in this way the message will be sent using the host server send mail program.

thank you again

whkoh
01-15-2001, 02:47 AM
If it doesn't work, please visit (http://htmlgoodies.earthweb.com) the site or origin.

------------------
Koh Wei Han
Network Engineer
Contact: whkoh@apexmail.com , whkoh@mailandnews.com , whkoh@020.co.uk

desert
01-15-2001, 07:18 AM
Sorry Whkoh

But I couldnít find the one, which I want.
Iím still waiting, maybe somebody will help.

I hope ĎJacobí will help.

desert
01-15-2001, 12:55 PM
I found this code in one of the free scripts
But it doesnít work with me do you know why?

####################################
print MAIL &lt;&lt;"END";
From: $myemail
To: $receptionistmail
Subject: $supject

Requested File: $file

END
open(FILE, "uuencode $file $file|") or die;
while( &lt;FILE&gt; ) { print MAIL; };

close(FILE);
close(MAIL);
#############################

Jacob
01-15-2001, 03:07 PM
Hi desert,

Give me a little bit, as I'm trying to do some research on attaching files (I personally haven't had to do this yet).

So far it looks like you may need to an extra module/libary to do this, but I want to make sure there isn't another alternative.

Regards,

------------------
Jacob A. Wheeler
Co-Founder / Web Engineer
Big Resources Network
jacob@bigresources.com
ICQ: 390147 (http://www.icq.com/390147)

desert
01-15-2001, 03:27 PM
Thank you Jacob

I'm Waiting .......

Jacob
01-15-2001, 06:26 PM
ok, I found some code to work with, give me a sec to whip up a script and test it.



------------------
Jacob A. Wheeler
Co-Founder / Web Engineer
Big Resources Network
jacob@bigresources.com
ICQ: 390147 (http://www.icq.com/390147)

Jacob
01-15-2001, 06:59 PM
Ok, Back to the drawing board. The script that looked like it would work is apparently designed to only handle txt files (it simply opens and writes the contents to the email message)

Below is the code just in case someone else would be interested:


#!/usr/bin/perl
$recipient = "to\@email.com"; #Who do you want to send the file to?
$webmaster = "your\@email.com"; #Your email address
$mail_program = "/usr/sbin/sendmail"; #Path to sendmail

#File you wish to send!
@file = "/path/to/file";

&send_email;
sub send_email {
print "Content-type: text/html\n\n";

open(MAIL,"|$mail_program -t");
print MAIL "To: $recipient\n";
print MAIL "From: $webmaster\n";
print MAIL "Subject: File Attachement!\n";
open(CONTENTS,"@file) || print "Error opening file for sending!\n";
while (&lt;CONTENTS&gt; ) {
chop $_;
print MAIL $_,"\n";
}
close (CONTENTS);
close (MAIL);
exit(0);
print "Email Sent!\n";
}


I'm still looking...

------------------
Jacob A. Wheeler
Co-Founder / Web Engineer
Big Resources Network
jacob@bigresources.com
ICQ: 390147 (http://www.icq.com/390147)

[Edited by Jacob on 01-20-2001 at 10:54 AM]

desert
01-16-2001, 06:12 AM
Thank you Jakop

But still I want a code, which can send any file as an attachment.

;) Sorry for disturbing you. ;)

Jacob
01-16-2001, 07:35 AM
Here is the only other script I can find right now:
http://www.datatrendsoftware.com/automail_lite.html

I checked out the source code, and it uses a perl module (mime::tools).

Regards,

------------------
Jacob A. Wheeler
Co-Founder / Web Engineer
Big Resources Network
jacob@bigresources.com
ICQ: 390147 (http://www.icq.com/390147)

desert
01-16-2001, 09:28 AM
Dear Jakop

Iíve visited the site but their program is complicated and itís need specific requirements, which is not available in most ISPís.

But Iíve downloaded a free script which is attaching files from Itís same directory, of course itís deferent than the one which I want, as I want the user to chose the file from his local hard disk using the following browse script which written by ďwhkohĒ

&lt;INPUT TYPE="file" NAME="attachedfile" MAXLENGTH=50 ALLOW="text/*" &gt;

I have sent a copy to your mail, You may get a new idea

And this is the script:


#!/usr/bin/perl
#################################################################
# Email The File Version 1.0 #
# Copyright 1999 Jeff Ledger jeff@ledgerlabs.cc #
# Created 7/9/99 Last Modified 7/9/99 #
# Script Archived At: http://www.ledgerlabs.cc #
#################################################################
# Set Variables
$mailprog = "/usr/bin/sendmail"; # Sendmail Location
$youremail = "jdoe\@yoursite.com"; # Your Email Address
$yourname = "John Doe"; # Your Name
$aftersend = "http://www.ledgerlabs.cc/"; # Where to go after
$file = "efile.zip"; # File to send to user

# Set Your Options
$automessage = "1"; # Send 'efile.letter' to user with file.
# 0 = NO / 1 = YES

$autoalert = "1"; # Email yourself alert when user gets a file.
# 0 = NO / 1 = YES

# Done #
#################################################################


#####################
# Get the form data #
#####################

read(STDIN, $values, $ENV{'CONTENT_LENGTH'});

# Split up the name-value pairs
@pairs = split(/&/, $values);

foreach $set (@pairs) {
($name, $itemvalue) = split(/=/, $set);

# Remove plus signs and any encoding present
$itemvalue =~ tr/+/ /;
$itemvalue =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$itemvalue =~ s/&lt;!--(.|\n)*--&gt;//g;

$FORM{$name} = $itemvalue;
}

##########################
# Set incoming Variables #
##########################

$email = $FORM{'email'};


##############################
# Send file to user in email #
##############################

open(MAIL, "|$mailprog -oi -t") or die;

# Create headers

print MAIL &lt;&lt;"END";
From: $yourname
To: $email
Subject: File: $file (uuencoded)

Requested File: $file

END
open(FILE, "uuencode $file $file|") or die;
while( &lt;FILE&gt; ) { print MAIL; };

# Close the file and finish sending the mail

close(FILE);
close(MAIL);


##################################################################
# Send 'efile.letter' to requestor if automessage is toggled on. #
##################################################################

if ($automessage eq '1') {

# Send AutoResponse Email

open(MAIL, "|$mailprog -oi -t") or die;

# Create headers

print MAIL &lt;&lt;"END";
From: $yourname
To: $email
Subject: File Confirmation

END

open(FILE, "efile.letter") or die;
while( &lt;FILE&gt; ) { print MAIL; };

close(FILE);
close(MAIL);

}


####################################################################################
# (Peek-a-boo) Email alert to webmaster if someone gets a file if autoalert is on. #
####################################################################################

if ($autoalert eq '1') {


open(MAIL, "|$mailprog -oi -t") or die;

# Create headers

print MAIL &lt;&lt;"END";
From: $yourname
To: $youremail
Subject: *EFILE REPORT*

EFILE ENGINE WAS USED BY: $email
END


# !!! you should check close exit codes

close(MAIL);

}


##############################################################
# Alert user that file is sent and send them somewhere else. #
##############################################################


print "Content-type: text/html\n\n";
print "&lt;TITLE&gt;File Sent!&lt;/TITLE&gt;\n";
print "&lt;META HTTP-EQUIV=\"refresh\" CONTENT=\"2; URL=$aftersend\"&gt;\n";
print "&lt;CENTER&gt;&lt;FONT SIZE=3&gt;\n";
print "Your requested file has been sent to: $email &lt;BR&gt;&lt;Br&gt;\n";
print "Teleporting.. or Click &lt;a href=\"$aftersend\"&gt;here&lt;/a&gt;\n";



[This message has been edited by desert (edited 01-16-2001).]

Jacob
01-17-2001, 07:39 PM
Hi Desert,

I got your script via email. There isn't much I can do for you as I don't exactly have time to write your script for you.

I can however tell you what you need to do. Since you already have the code to send file attachements, you should look for a file uploader to allow you to upload files via form for a temporary directory. From there you will be able to send the file (as it will be local on the server) and then remove the file after the email is sent (so you don't waste space).

To Recap:

-&gt; Find uploading script, combine it with the above script to allow the user to upload (to a temporary dir), and then send the email.
-&gt; Remove the file from the server

Let me know if you have any questions on how to go about this.

Regards,


------------------
Jacob A. Wheeler
Co-Founder / Web Engineer
Big Resources Network
jacob@bigresources.com
ICQ: 390147 (http://www.icq.com/390147)

desert
01-18-2001, 12:49 AM
Thank you Jacob,
I will try and let you know soon.
I have another question which is easy for you, but I will start new thread for the new question.


thank you .

wildbass
02-21-2001, 12:09 PM
Hello,
Checkout http://www.wildonweb.com - url is http://www.wildonweb.com/wowpop/

Try the Sendmail and POP access using SendMail - if you like - will send you code - email me directly
webmaster@wildonweb.com


Originally posted by desert
Iím developing CGI script whish will allow the webmaster to send a message to any body and include Attachment with the message .

But I donít know how to attach documents with the message Iím using the following PERL code to send mail :

print MAIL "To: $receptionistmail\n";
print MAIL "From: $webname<$youremail>\n";
print MAIL "Subject: $msgtitel\n";
print MAIL "Hi $receptionistname:\n";
print MAIL "$msgbody\n";
close (MAIL);

Please Help.

Unregistered
04-27-2001, 03:58 AM
Originally posted by whkoh
The code is:


<FORM METHOD="post" ACTION="mailto:jburns@htmlgoodies.com" ENCTYPE="multipart/form-data">
Attachment: <INPUT TYPE="file" NAME="attachedfile" MAXLENGTH=50 ALLOW="text/*">
</FORM>


Change the ENCTYPE to the correct MIME type. A list can be found here (http://www.utoronto.ca/webdocs/HTMLdocs/Book/Book-3ed/appb/mimetype.html).




------------------
Koh Wei Han
Network Engineer
Contact: whkoh@apexmail.com , whkoh@mailandnews.com , whkoh@020.co.uk

[Edited by Jacob on 01-20-2001 at 10:53 AM]