# Solved: Basic Formmail with attachment Help



## HOBOcs (Jan 5, 2004)

I'm looking for assistance on creating a form that allows sending attachments.

I'm using "FORMMAIL" or a version of it.

The following allows me to create a file box but I'm not sure as to the statement for adding the file to the formmail.
FYI, I use formmail program supplied by my hosting company - I haven't been able to get an answer from them as yet.. so thought I'd throw this out there to see what kind of response I get.

Any help direction appreciated.

Sample code here:

New Page 1








*Please enter the following information and then press the send button at the bottom of this form.*  ** = Required information

*

*
* Name: *

*
* E-Mail: * 

 *Upload
your 
Resume/CV: * 





 



[TD]

Press the send button and your information request will be forward to us.


----------



## brendandonhu (Jul 8, 2002)

We can't do much without knowing which formmail script that is.


----------



## HOBOcs (Jan 5, 2004)

This is my starting point..


----------



## brendandonhu (Jul 8, 2002)

Still need to know which formmail.cgi it is.


----------



## HOBOcs (Jan 5, 2004)

I've just heard back from the Dreamhost site... they don't support attachments in their formmail.

Now I'm looking for simple alternatives.


----------



## brendandonhu (Jul 8, 2002)

There's a bunch out there in PHP or Perl if your host lets you run your own scripts. You can find 'em on Google.


----------



## HOBOcs (Jan 5, 2004)

Thanks B. took your advice and found a couple to check out.
I'll post my results as a follow-up.


----------



## HOBOcs (Jan 5, 2004)

Ok, I found something that appears to work for me. (Sends the email with an attachment) - pretty basic.

The only problem is that the document attachment (word or pdf or even a .txt file) appears to be corrupted or jumbled characters. 
Note sure if it has anything to do with encoding data or what?

Googling for answers. 
The basic code follows: It was designed for two attachments but I've commented out the original code for one attachment.

<?php

$att1 = $_FILES['Att1'];
$att_path1 = $_FILES['Att1']['tmp_name'];
$att_name1 = $_FILES['Att1']['name'];
$att_size1 = $_FILES['Att1']['size'];
$att_type1 = $_FILES['Att1']['type'];

#open, read then close the files
$fp1 = fopen( $att_path1, "rb");
$file1 = fread( $fp1, $att_size1 );
fclose( $fp1 );

// $att2 = $_FILES['att2'];
// $att_path2 = $_FILES['att2']['tmp_name'];
// $att_name2 = $_FILES['att2']['name'];
// $att_size2 = $_FILES['att2']['size'];
// $att_type2 = $_FILES['att2']['type'];

// $fp2 = fopen( $att_path2, "rb");
// $file2 = fread( $fp2, $att_size2 );
// fclose( $fp2 );

$first_name = $_POST['First_Name'];
$last_name = $_POST['Last_Name'];
$email = $_POST['Email'];
$home_phone_area = $_POST['Home_Phone_Area'];
$home_phone_number = $_POST['Home_Phone_Number'];
$work_phone_area = $_POST['Work_Phone_Area'];
$work_phone_number = $_POST['Work_Phone_Number'];
$work_phone_extention = $_POST['Work_Phone_Extentionr'];
$cell_phone_area = $_POST['Cell_Phone_Area'];
$cell_phone_number = $_POST['Cell_Phone_Number'];
$street = $_POST['Street'];
$city = $_POST['City'];
$provstate = $_POST['ProvState'];
$postalcode = $_POST['Postal_Code'];
$country = $_POST['Country'];
$notes = $_POST['Notes'];
$contact_me_byphone = $_POST['Contact_me_byPhone'];
$contact_me_byemail = $_POST['Contact_me_byEmail'];

#Email Subject
$title = $_POST['title'];
$comments = $_POST['comments'];

#create a boundary string
$num = md5(time());
$str = "==Multipart_Boundary_x{$num}x";

#File 1
set_magic_quotes_runtime(0);
#encode the data for safe transit 
#and intersperse 76-character chunks with \n
//$file1 = chunk_split(base64_encode($file1));

#File 2
//set_magic_quotes_runtime(0);
//#encode the data for safe transit 
//#and intersperse 76-character chunks with \n
//$file2 = chunk_split(base64_encode($file2));

#recipient (to whom it may concern...)
#sender (commentator)
//$to .= "[email protected]";
$to = $email . ", ";

#define header
$hdr = '';
$hdr .= "From:$email\r\n";

#cc: another recipient
//$hdr .= "cc: [email protected]\r\n";

#subject
$subject = "". $title . " From: " . $first_name . " " . $last_name;

$hdr .= "MIME-Version: 1.0\r\n";
$hdr .= "Content-Type: multipart/mixed;";
$hdr .= "boundary=$num;\r\n";
$hdr .= "--$num\r\n";

$hdr .= "Content-Type: text/plain\r\n";
$hdr .= "Content-Transfer-Encoding: 8bit\r\n\n";

#define message
$msg = "On-Line Registration from Website...\n\n";
$msg .= "Name: " . $first_name . " " . $last_name . "\n\n";
$msg .= "Email: " . $email . "\n\n";
$msg .= "Home Phone: (" . $home_phone_area . ") " . $home_phone_number . "\n";
$msg .= "Work Phone: (" . $work_phone_area . ") " . $work_phone_number . " Ext: " . $work_phone_ext . "\n";
$msg .= "Cell Phone: (" . $cell_phone_area . ") " . $cell_phone_number . "\n\n";
$msg .= "Address: \n";
$msg .= " " . $street . "\n";
$msg .= " " . $city . " " . $provstate . " " . $postalcode . "\n";
$msg .= " " . $country . "\n\n";

$msg .= "Contact by Phone: " . $contact_me_byphone . "\n";
$msg .= "Contact by Email: " . $contact_me_byemail . "\n\n";

$msg .= "Notes: " . $notes . "\n\n";

$hdr .= "$msg\r\n";
$hdr .= "--$num\r\n";

#define the first non-text attachment
$hdr .= "Content-Type: {$att_type1}; ";
$hdr .= "name=\"{$att_name1}\"\r\n";
$hdr .= "Content-Transfer-Encoding: base64\r\n";
$hdr .= "Content-Disposition: attachment;\r\n\n";
$hdr .= "filename=\"{$att_name1}\"\r\n";
$hdr .= "$file1\r\n";
$hdr .= "--$num\r\n";

#define the second non-text attachment
//$hdr .= "Content-Type: {$att_type2}; ";
//$hdr .= "name=\"{$att_name2}\"\r\n";
//$hdr .= "Content-Transfer-Encoding: base64\r\n";
//$hdr .= "Content-Disposition: attachment;\r\n\n";
//$hdr .= "filename=\"{$att_name2}\"\r\n";
//$hdr .= "$file2\r\n";
//$hdr .= "--$num--\r\n";

#send the email now...
$ok = mail( $to, $subject, $msg, $hdr);

if($ok) {
echo "
*You will receive a copy of this email *
";
echo "
*Thank you.*
";
}

?>


----------



## HOBOcs (Jan 5, 2004)

Been struggling with the corrupted file issue for the last two days.
Appears to be related to encoding base64 issue.
My email works.. send the attachment and it shows as a word attachment but as when you open the document it appears all "garbled". It may be that the document is not getting decoded properly... not sure where to look next.

Found this example at this site. http://forums.devshed.com/archive/t-192423
only one that appears to make sense.

Looking for assistance/direction.


----------



## HOBOcs (Jan 5, 2004)

I've cleaned up the code to make it more readable...
But I still have the problem geting attachments all garbled up.
I suspect Binary encode base 64 or somthing in here..

Here is cleaned up code: Still looking for clues..... direction

*******************************

<?php

# Set the File Attributes
$att1 = $_FILES['User_File'];
$att_path1 = $_FILES['User_File']['tmp_name'];
$att_name1 = $_FILES['User_File']['name'];
$att_size1 = $_FILES['User_File']['size'];
$att_type1 = $_FILES['User_File']['type'];

# Open, read then close the files
$fp1 = fopen( $att_path1, "rb");
$file1 = fread( $fp1, $att_size1);
fclose( $fp1 );

# Get the Field Names from the Form
$first_name = $_POST['First_Name'];
$last_name = $_POST['Last_Name'];
$from_email = $_POST['Email'];
$home_phone_area = $_POST['Home_Phone_Area'];
$home_phone_number = $_POST['Home_Phone_Number'];
$work_phone_area = $_POST['Work_Phone_Area'];
$work_phone_number = $_POST['Work_Phone_Number'];
$work_phone_extention = $_POST['Work_Phone_Extentionr'];
$cell_phone_area = $_POST['Cell_Phone_Area'];
$cell_phone_number = $_POST['Cell_Phone_Number'];
$street = $_POST['Street'];
$city = $_POST['City'];
$provstate = $_POST['ProvState'];
$postalcode = $_POST['Postal_Code'];
$country = $_POST['Country'];
$notes = $_POST['Notes'];
$contact_me_byphone = $_POST['Contact_me_byPhone'];
$contact_me_byemail = $_POST['Contact_me_byEmail'];
$email_subject = $_POST['email_subject'];

#create a boundary string
$num = md5(time());
$str = "==Multipart_Boundary_x{$num}x";

#encode the data for safe transit 
#and intersperse 76-character chunks with \n
set_magic_quotes_runtime(0);
$file1 = chunk_split(base64_encode($file1));

# Set up Message
$to = $from_email . ", ";
//$to .= "[email protected]";
$subject = "". $email_subject . " From: " . $first_name . " " . $last_name;

# Define Message
# Define Email Message Header
$msg1_header = "Content-Type: text/plain\n";
$msg1_header .= "Content-Transfer-Encoding: 8bit\r\n";

# Define Email Message Text
$msg1_text = "On-Line Registration from Website...\n\n";
$msg1_text .= "Name: " . $first_name . " " . $last_name . "\n\n";
$msg1_text .= "Email: " . $from_email . "\n\n";
$msg1_text .= "Home Phone: (" . $home_phone_area . ") " . $home_phone_number . "\n";
$msg1_text .= "Work Phone: (" . $work_phone_area . ") " . $work_phone_number . " Ext: " . $work_phone_ext . "\n";
$msg1_text .= "Cell Phone: (" . $cell_phone_area . ") " . $cell_phone_number . "\n\n";
$msg1_text .= "Address: \n";
$msg1_text .= " " . $street . "\n";
$msg1_text .= " " . $city . " " . $provstate . " " . $postalcode . "\n";
$msg1_text .= " " . $country . "\n\n";
$msg1_text .= "Contact by Phone: " . $contact_me_byphone . "\n";
$msg1_text .= "Contact by Email: " . $contact_me_byemail . "\n\n";
$msg1_text .= "Notes: " . $notes . "\n\n";
$message1 = "$msg1_text";

# Define the first attachment
$attachment_hdr .= "Content-Type: {$att_type1}; ";
$attachment_hdr .= "name=\"{$att_name1}\"\r\n";
$attachment_hdr .= "Content-Transfer-Encoding: base64\r\n";
$attachment_hdr .= "Content-Disposition: attachment;\r\n\n";
$attachment_file .= "filename=\"{$att_name1}\"\r\n";
$attachment_file .= "$file1\r\n";
$attachment = "$attachment_hdr\n$attachment_file\n";

# Define the Email Structure

# Define Email Header
$header = "From: $from_email\r\n";
//$header .= "cc: [email protected]\r\n"; 
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: multipart/mixed;";
$header .= "boundary=$num;\r\n";
$header .= "--$num\r\n";
$header .= "$msg1_header";

# Define Email Body
$body = 
$message1 .
"--$num\r\n" .
$attachment .
"--$num--\r\n";

#send the email now...
$ok = mail( $to, $subject, $body, $header);

if($ok) {
echo "
*You will receive a copy of this email *
";
echo "
*Thank you.*
";
}

?>


----------



## Sequal7 (Apr 15, 2001)

Have found this to be very helpful in creating custom forms with attachments etc.
http://freshmeat.net/projects/phpfmg/


----------



## HOBOcs (Jan 5, 2004)

ok. I have a php routine that appears to work. text files anyways.
I'm going to resolve this thread.

I have a seperate issue which I will open another thread related to encode decode word files.


----------

