Jump to content

Simple Attatchment Fix?? ...or so I thought!


Darrenjs1210

Recommended Posts

Was just wondering if anyone could help out with what should be a fairly simple fix for a contact form with image upload.

 

The image DOES get uploaded to the temporary folder, but doesn't appear when I receive the email into my account.

 

I think the form is almost good to go, just can't figure out why the image is not attatched to the email when i receive it - guessing the problem is somewhere in the last 10 lines of code.

 

Really appreciate if someone could figure what's I'm doing wrong. (...perhaps it was thinking I could learn PHP in the first place!).

 

Thanks.

 

<?php

ini_set("sendmail_from", "darren@mywebsite.co.uk");
ini_set("SMTP", "smtp.myhostingcompany.co.uk");

if($name = filter_var($_POST['name']))
if($address = filter_var($_POST['address']))
if($postcode = filter_var($_POST['postcode']))
if($phone = filter_var($_POST['phone']))
if($email = filter_var($_POST['email']))
if($details = filter_var($_POST['details']))
if($contactby = filter_var($_POST['contactby']))

/* Subject and Email Destinations */
$emailSubject = 'Work Email!';
$webMaster = 'darren@mywebsite.co.uk';

/* Gathering Data Variables */
$nameField = $_POST['name'];
$addressField = $_POST['address'];
$postcodeField = $_POST['postcode'];
$phoneField = $_POST['phone'];
$emailField = $_POST['email'];
$detailsField = $_POST['details'];
$contactbyField = $_POST['contactby'];
    
    $allowed_filetypes = array('.jpg','.gif','.bmp','.png'); 
    $max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB)  
    $upload_path = './uploads/'; 

    $filename = $_FILES['userfile']['name']; 
    $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); 

    if(!in_array($ext,$allowed_filetypes))
    die('The file you attempted to upload is not allowed.');

    if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
    die('The file you attempted to upload is too large.');

    if(!is_writable($upload_path))
    die('You cannot upload to the specified directory, please CHMOD it to 777.');

    if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
    echo 'Your file upload was successful, view the file <a href=
    "' . $upload_path . $filename . '" title="Your File">here</a>'; // It worked.
    else
    echo 'There was an error during the file upload.  Please try again.'; // It failed

    $body = <<<EOD
    Name: $name
    Address: $address
    Postcode: $postcode
    Phone: $phone
    Email: $email
    Details: $details
    Contactby: $contactby
    EOD;

$headers = "From: $email\r\n";
$headers .= "Contact-type: text/html\r\n";
$attachment = chunk_split(base64_encode(file_get_contents($temp_file)));
$tmp_file = $_FILES['userfile']['tmp_name'];
$success = mail($webMaster, $attatchment, $body, $headers, "-fdarren@mywebsite.co.uk");

    /* Results rendered as HTML */

    $theResults = <<<EOD
    SENT SENT SENT
    EOD;
    echo "$theResults"
    ?>

Link to comment
Share on other sites

Is a slightly different way of getting the attatchment in that example.

 

They use MIME rather than storing the image in a temporary folder. Better for security I guess.... but harder to get working.

 

I looked at the first answer from this thread:

 

http://stackoverflow.com/questions/7323943/php-contact-form-with-file-attachment

 

and thought it seemed a bit easier?? Well........ at least if it actually worked!

 

 

Link to comment
Share on other sites

The answer there is using a PHP class called PHPMailer, even though the OP hasnt got it in his code.

The only way (as far as I know) to send an attachment in an email is to use MIME.

Ive attached a basic example below of how you can add this to your script.. It might require a few tweaks to get it to work.

 

$body = <<<EOD
Name: $name
Address: $address
Postcode: $postcode
Phone: $phone
Email: $email
Details: $details
Contactby: $contactby
EOD;

// Create a Subject //
$subject = 'Test Attachment';

// Create your MIME boundary //
$semi_rand = md5(time());
$mime_boundary = '==Multipart_Boundary_x'.$semi_rand.'x';

// Make up some headers
$headers = 'From: ' . $email . "\r\n" .
'To: '.$webMaster."\r\n" .
'Reply-To: ' . $email . "\r\n" .
'MIME-Version: 1.0' . "\r\n" .
'Content-Type: multipart/mixed;' . "\r\n" . " boundary=\"".$mime_boundary."\"";

// Create a message variable that we can append the data to //
$message = '';

// Get the attachment ready //
$temp_file = $upload_path . $filename;
if (file_exists($temp_file)) {
$file_type = mime_content_type($temp_file);
$data = file_get_contents($temp_file);
$data = chunk_split(base64_encode($data));

// Add the attachment to the message data //
$message .= "--".$mime_boundary."\n" .
"Content-Type: {$file_type};\n" .
" name=\"{$filename}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$filename}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n";
}

// Chuck in the rest of the BODY //
$message .= "This is a multi-part message in MIME format.\n\n" .
"--".$mime_boundary."\n" .
"Content-Type: text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$body . "\n" . 
"--".$mime_boundary."--\n\n";

$success = mail($webMaster, $subject, $message, $headers, "-fdarren@mywebsite.co.uk");

You can see where this code is supposed to go in relation to your script.. I have never used the mail() function to send an attachment so I cannot make any guarantees.

 

Also this code was hacked together from a Mail class I wrote many moons ago.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.