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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form script to send Attachments

Status
Not open for further replies.

paulbenn

Programmer
Jan 24, 2002
108
GB
Hi,

I am trying to write a script that attach's a file from the senders local drive and emails it to and address.

It kind of works but the file is either garbled or empty! and some files dont even attach or send the email.

The idea is that a visitor could send a log file from there system via email to our support centre via the web.

Here is the part of the script that should deal with the mail and the attachment.

you can try the code at this will email the same address that you put into the form.

Code:
sub send_message
{

## Send File to poster Start
my @boundaryv = (0..9, 'A'..'F');
srand(time ^ $$);
for (my $i = 0; $i++ < 24;)
{
$boundary .= $boundaryv[rand(@boundaryv)];
}

open MAIL, &quot;|$mailprog -t&quot;;
print MAIL &quot;To: $email_address ($yourname)\n&quot;;
print MAIL &quot;From: $email_address ($your_name)\n&quot;;
print MAIL &quot;MIME-Version: 1.0\n&quot;;
print MAIL &quot;Subject: $fileinfo\n&quot;;
print MAIL &quot;Content-Type: multipart/mixed; boundary=\&quot;------------$boundary\&quot;\n&quot;;
print MAIL &quot;\n&quot;;
print MAIL &quot;This is a multi-part message in MIME format.\n&quot;;
print MAIL &quot;--------------$boundary\n&quot;;
print MAIL &quot;Content-Type: text/html; charset=us-ascii\n&quot;;
print MAIL &quot;Content-Transfer-Encoding: 7bit\n\n&quot;;
print MAIL &quot;<b>Dear $toname,\n\n</b><br><br>\n&quot;;
print MAIL &quot;<br>Here is my file<br><br>File Attached: <b>$filename</b><br><br>\n&quot;;
print MAIL &quot;<br><br>\n&quot;;
print MAIL &quot;--------------$boundary\n&quot;;

## Attach the file
&attach_files;

print MAIL &quot;\n--------------$boundary--\n&quot;;
print MAIL &quot;\n&quot;;
close (MAIL);

$status = &quot;Sent&quot;;
&okay_html;

}


sub attach_files {

$file=$attachment;
($ext) = $file =~ m,\.([^\.]*)$,;
$ext =~ tr,a-z,A-Z,;
$fext=&mimetype($ext);

print MAIL &quot;--------------$boundary\n&quot;;
print MAIL &quot;Content-Type: $fext; name=\&quot;$atachment\&quot;\n&quot;;
print MAIL &quot;Content-Transfer-Encoding: base64\n&quot;;
print MAIL &quot;Content-Disposition: attachment; filename=\&quot;$filename\&quot;\n\n&quot;;
&buf_type;
print MAIL &quot;Content-Type: text/html; charset=us-ascii\n&quot;;
print MAIL &quot;Content-Transfer-Encoding: 7bit\n\n&quot;;
print MAIL &quot;File: $filename Attached&quot;;
print MAIL &quot;--------------$boundary--\n&quot;;
}



sub buf_type {


my $buf2;
$/=0;
open INPUT, &quot;$file&quot;;
binmode INPUT if ($^O eq 'NT' or $^O eq 'MSWin32');
while(read(INPUT, $buf2, 60*57))
{
print MAIL &encode_base64($buf2);
}
}

sub encode_base64 #($)
{
my ($res, $eol, $padding) = (&quot;&quot;, &quot;\n&quot;, undef);

while (($_[0] =~ /(.{1,45})/gs))
{
$res .= substr(pack('u', $1), 1);
chop $res;
}

$res =~ tr#` -_#AA-Za-z0-9+/#;                               # ` help emacs
$padding = (3 - length($_[0]) % 3) % 3;                   # fix padding at the end

$res =~ s#.{$padding}$#'=' x $padding#e if $padding;    # pad eoedv data with ='s
$res =~ s#(.{1,76})#$1$eol#g if (length $eol);          # lines of at least 76 characters

return $res;
}

Thanks for your help in advance.

Kind Regards, Paul Benn

**** Never Giveup, keep trying, the answer is out there!!! ****
 
Can Anybody help? I am in urgent need to resolve this in.
Kind Regards, Paul Benn

**** Never Giveup, keep trying, the answer is out there!!! ****
 
Ok, I see lots of potential problems, but let me start from the top, and you can correct one thing at a time and come back for help if you need it.

Your $boundary line is printed in both the send and attach subroutines. Further, $boundary is not declared in your attach subroutine. You should &quot;use strict&quot; and declare all of your variables with &quot;my&quot;.

BTW, I see no need for the random boundary string generation. Just come up with something not likely to ever appear in the body and use that always unless you expect this same perl program to re-send attachments of its own previously sent email.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Hi Tom,

Thanks for the response, I have taken the boundary's off the attachment and now I get the encode format printed in the email and no attachment?

Could you give an example of what it should look like, my email is paul@pbnet.co.uk

Many thanks for your time.
Kind Regards, Paul Benn

**** Never Giveup, keep trying, the answer is out there!!! ****
 
Check that mimetype() doesn't return newlines. Also, $atachment isn't defined (like I said before, you want to be using strict). The best way to tell what is going wrong is to print out the full source of the email. What exactly shows up in the email between your boundary lines?
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top