PDA

View Full Version : Sending File Attachment with PHP... How???


SmoothCanvas
10-03-2007, 10:08 PM
Yay Dreamweaver for giving us a pre-made attach file field. But how to I get the file to send to an e-mail address??

The php I have so far:

<?PHP
if(!empty($_POST['theEmail'])
|| !empty($_POST['theSubject'])
|| !empty($_POST['theMessage'])
|| !empty($_POST['theName']))
{
$to = "dhuang@spicenet.net";
$subject = "SRE Website E-mail Submission";
$message = "Name: " . $_POST['theName'];
$message .= "\nPhone #: " . $_POST['thePhone'];
$message .= "\nBusiness Name: " . $_POST['theBusiness'];
$message .= "\nPosition Title: " . $_POST['theTitle'];
$message .= "\nEmail: " . $_POST['theEmail'];
$message .= "\nSubject: " . $_POST['theSubject'];
$message .= "\n\nMessage: " . $_POST['theMessage'];
$headers = "From:". $_POST['theEmail'];
$headers .= "\nReply-To:".$_POST['theEmail'];


$sentOk = mail($to,$subject,$message,$headers);
if($sentOk){
echo header ("Location:thankyou.html");
}else{
echo "sentError=" . $sentError;
}
}
?>

SmoothCanvas
10-09-2007, 11:18 PM
<?PHP
if(!empty($_POST['theEmail'])
|| !empty($_POST['theSubject'])
|| !empty($_POST['theMessage'])
|| !empty($_POST['theName']))
{
$to = "dhuang@spicenet.net";
$subject = "SRE Website E-mail Submission";
$message = " Name: " . $_POST['theName'];
$message .= "\n Phone #: " . $_POST['thePhone'];
$message .= "\n Business Name: " . $_POST['theBusiness'];
$message .= "\nPosition Title: " . $_POST['theTitle'];
$message .= "\n Email: " . $_POST['theEmail'];
$message .= "\n\nSubject: " . $_POST['theSubject'];
$message .= "\n\nMessage: " . $_POST['theMessage'];
$headers = "From:". $_POST['theEmail'];
$headers .= "\nReply-To:".$_POST['theEmail'];

$fileatt = $_POST['resumeFile'];
$fileatttype = "application/pdf";
$fileattname = "resume.pdf";

$file = fopen( $fileatt, 'rb' );
$data = fread( $file, filesize( $fileatt ) );
fclose( $file );

$semi_rand = md5( time() );
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";

$message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";

$data = chunk_split( base64_encode( $data ) );

$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatttype};\n" .
" name=\"{$fileattname}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileattname}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";


$sentOk = mail($to,$subject,$message,$headers);
if($sentOk){
echo header ("Location:thankyou.html");
}else{
echo header ("Location:error.html");die;
}
}
?>