Web form to email with local file attachments

June 30th, 2009

Following on from how to email a file already residing on your web server, I thought it would be best to next give your users the ability to choose a file on their local machine, and attach it to an email to send to you.

My current employer asked me to do this to allow disco lighting customers to upload a picture of their desired lighting rig, so we can give them a quote based on their drawing or mspaint art!

First we need a form for your user/customer to fill in.


<html>

    <head>
        <title>Mailer Form</title>
    </head>

    <body>
        <form method="post" action="attach.php" enctype="multipart/form-data">
            <label for="file">Filename:</label>
            <input type="file" name="file" id="file" /><br />
            <input type="submit" name="submit" value="Submit" />
        </form>
    </body>

</html>

Nothing to fancy there. A simple file input form. I remember having problems if I didn’t use the id=”file” so be sure that is included.

The back of this will look very similar to the original email php script. But we will be making use of PHP’s $_FILES superglobal variable. Replace the static filename of our last example, with the more useful dynamic filename defined here.

email_attachment('to.this@address.com', 'my.email@address.com', 'Email attachment', 'My Name', 'my.email@address.com', $_FILES["file"]["tmp_name"]);

That will give you a very broad range of what can be temporarily uploaded to your site and what will end up in your email inbox. In your own scripts, you can use the $_FILES["file"]["type"] variable to check what sort of thing your user is uploading and prevent it if necessary. Remember to update $default_filetype as necessary.


<?php
    if ($_FILES["file"]["type"] == "image/jpeg"){
        email_attachment(...);
    }else{
        echo "Error";
    }
?>

A few other useful variables are

$_FILES["file"]["name"]
$_FILES["file"]["size"]
$_FILES["file"]["error"]

A bit of experimentation with those will be easy enough.

Feedback on my blog posts is welcomed and encouraged!

Email Attachments with PHP

June 19th, 2009

I wasted a lot of time trawling the internet for an example of sending an email with attachment, the closest I could get to a working example I found at EdmondsCommerce.co.uk so primary credit goes to him. However I have made some minor modifications which I think make it a little bit easier/nicer.

Copy this straight into a new text file on your web server and modify the first line starting ‘email_attachment( ‘ to represent your own details. Open it up in your browser, and then check your email inbox.

<?php
    email_attachment('to.this@address.com', 'my.email@address.com', 'Email attachment', 'My Name', 'my.email@address.com', 'myfile.jpg');

    function email_attachment($to_email, $email, $subject, $our_email_name, $our_email, $file_location, $default_filetype='application/zip'){

    $fileatt = $file_location;

    if(function_exists(mime_content_type)){
        $fileatttype = mime_content_type($file_location);
    }else{
        $fileatttype = $default_filetype;;
    }

    $fileattname =basename($file_location);
    //prepare attachment
    $file = fopen( $fileatt, 'rb' );
    $data = fread( $file, filesize( $fileatt ) );
    fclose( $file );
    $data = chunk_split( base64_encode( $data ) );

    //create mime boundary
    $semi_rand = md5( time() );
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

    //Create email  section
    $message = "This is a multi-part message in MIME format.\n\n" .
        "--{$mime_boundary}\n" .
        "Content-type: text/html; charset=us-ascii\n" .
        "Content-Transfer-Encoding: 7bit\n\n" .
    $email . "\n\n";

    //create attachment section
    $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";

    //headers
    $exp=explode('@', $our_email);
    $domain = $exp[1];
    $headers = "From: $our_email_name<$our_email>" . "\n";
    $headers .= "Reply-To: $our_email"."\n";
    $headers .= "Return-Path: $our_email" . "\n";    // these two to set reply address
    $headers .= "Message-ID: <".time()."@" . $domain . ">"."\n";
    $headers .= "Date: ".date("r")."\n";
    $headers .= "MIME-Version: 1.0\n" .
        "Content-Type: multipart/mixed;\n" .
        " boundary=\"{$mime_boundary}\"";

    if (mail($to_email,$subject,$message, $headers, '-f ' . $our_email))
        echo 'Success';
    else
        echo 'Error';
    }
?>

Fedora 11

June 16th, 2009

A few days of using Fedora 11 (Leonidas) I thought it was about time I wrote a proper post, and what better topic?

On first impressions, integration with GNOME looks beautiful. A number of minor changes from the previous version have paid off, giving Leonidas a look like that of a modern operating system. At the moment I’m using GNOME for everything just because it’s so pretty. How long it will be before I end up back on fluxbox I can’t say, although I can’t imagine it will be anytime soon.

Fedora 10 gave me so many bizarre problems. For example making a menu selection from the ‘Places’ menu usually resulted in absolutely nothing happening. I put this down to hardware incompatibility, but Leonidas has no similar events. In fact the entire install procedure went without any trouble, which is a rare occurrence for myself with any Linux installation.

No doubt there will be problems in the near future, but it will be a good subject for a tutorial post.

FYP Proposal

June 5th, 2009

The final version of my Final Year Project Proposal has just been finished. Anyone interested can have a look at it here.

Feel free to give feedback should you desire.

First post

May 7th, 2009

Find the website at www.s-carter.co.uk although there’s not much content yet. Subscribe etc.