Grow your CSS skills. Land your dream job.

Contact Form with Function to Add Attachment

  • # December 3, 2009 at 6:49 am

    Hi there,

    I have tried to find out a way to add attachment along with the mail form i have made, but things doesnt turn out as i wanted. As my php knowledge is not that great i am seeking for help here and hope to get the answer. My contact form look like this

    Code:
    < ?php
    //start php session for error return
    SESSION_START();
    //error_reporting(0);
    if($_REQUEST['verstuur']){
    process_mail($_REQUEST);
    $_SESSION['new'] = 1;
    }

    function checkmail($mail)
    {
    // explode het e-mail adres op een @
    $email_host = explode("@", $mail);
    // pak alles na de @
    $email_host = $email_host['1'];
    // zet een var met de (eventuele) DNS van de domein
    $email_resolved = gethostbyname($email_host);

    // kijk of er een DNS aan zit en of alles voor en na het e-mail adres klopt
    if ($email_resolved != $email_host && eregi("^[0-9a-z]([-_.~]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\.[a-z]{2,4}$",$mail))
    $valid = 1;

    // wanneer het geldig is, is $valid = 1, zo niet, dan geeft hij geen result (NULL)
    return $valid;
    }

    function process_mail($data){
    // get posted data into local variables
    $EmailTo = "wing72@hotmail.com";
    $Subject = "Mail vanuit website";
    $name = mysql_escape_string($data['name']);
    $tel = mysql_escape_string($data['tel']);
    $email = mysql_escape_string($data['email']);
    $message = mysql_escape_string($data['message']);

    //validate the input
    if(!isset($name) || $name == "") $_SESSION['errors']['Naam'] = true;
    if(!isset($tel) || $tel == "") $_SESSION['errors']['Telefoon Nummer'] = true;
    if(!checkmail($email)) $_SESSION['errors']['Email Adres'] = true;
    if(!isset($message) || $message == "") $_SESSION['errors']['Bericht'] = true;

    //als er errors zijn gaan we stoppen met sturen van email!
    if(sizeof($_SESSION['errors']) > 0){
    $_SESSION['error'] = true;
    return;
    }

    //vorige errors verwijderen
    unset($_SESSION['error']);
    unset($_SESSION['errors']);
    // prepare email body text
    $Body = “”;
    $Body .= “Name: “;
    $Body .= $name;
    $Body .= “n”;
    $Body .= “Email: “;
    $Body .= $email;
    $Body .= “n”;
    $Body .= “Tel: “;
    $Body .= $tel;
    $Body .= “n”;
    $Body .= “Details: “;
    $Body .= $message;
    $Body .= “n”;

    // send email
    $success = mail($EmailTo, $Subject, $Body, “From: < $email>“);
    if($success){
    //generate auto reply!
    mail($email, $Subject, “Beste $name,nnUw bericht is ontvangen door Chocolaterie Henri.nWe zullen zo spoedig contact met u opnemen.nnMet vriendelijke groet,nn
    A test page
    Tel: 013-5054939 | Email: wing72@hotmail.com
    “, “From: < $EmailTo>“);
    } else {
    $_SESSION['error'] = true;
    $_SESSION['sysError'] = “Er is een fout opgetreden tijdens het versturen van het formulier. Probeer later nog eens”;
    }
    }
    ?>

    < !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


    < ?php if($_SESSION['error'] || !isset($_REQUEST['verstuur']) ){
    //first time visit this page
    ?>

    Chocolaterie Henri

    Helfheuvelpassage 118
    5224 AP ‘s-Hertogenbosch
    073 622 1994

    Vul hier uw naam in.“; ?>
    Vul hier uw Email in.“; ?>
    Vul hier uw telefoonnummer in.“; ?>
    Laat uw bericht hier achter.“; ?>


    < ?php if(sizeof($_SESSION['errors']) > 0){
    unset($_SESSION['error']);
    unset($_SESSION['errors']);
    }?>

    < ?php } else {
    echo "

    “;
    echo “

    Uw bericht is verzonden.

    “;
    echo “

    “;
    session_destroy();
    }
    ?>


    Chocolaterie Henri

    so what i do want it to have a button for the viewer to send a picture with the mail.

    i hope you guys can help me out

    Greets

    wing

    # December 3, 2009 at 12:53 pm

    This looks like a pretty good tutorial:

    http://apptools.com/phptools/forms/forms5.php

    # December 4, 2009 at 5:02 am

    Hi thanks for a quick reply, so if i am correct i can just use the code for attachment for my form ?

    Code:
    // generate a random string to be used as the boundary marker
    $mime_boundary=”==Multipart_Boundary_x”.md5(mt_rand()).”x”;

    // store the file information to variables for easier access
    $tmp_name = $_FILES['filename']['tmp_name'];
    $type = $_FILES['filename']['type'];
    $name = $_FILES['filename']['name'];
    $size = $_FILES['filename']['size'];

    // here we’ll hard code a text message
    // again, in reality, you’ll normally get this from the form submission
    $message = “Here is your file: $name”;

    // if the upload succeded, the file will exist
    if (file_exists($tmp_name)){

    // check to make sure that it is an uploaded file and not a system file
    if(is_uploaded_file($tmp_name)){

    // open the file for a binary read
    $file = fopen($tmp_name,’rb’);

    // read the file content into a variable
    $data = fread($file,filesize($tmp_name));

    // close the file
    fclose($file);

    // now we encode it and split it into acceptable length lines
    $data = chunk_split(base64_encode($data));
    }

    // now we’ll build the message headers
    $headers = “From: $fromrn” .
    “MIME-Version: 1.0rn” .
    “Content-Type: multipart/mixed;rn” .
    ” boundary=”{$mime_boundary}””;

    that bit of code i should add to my form right..

    # December 9, 2009 at 2:06 pm

    Hi guys,

    again the same question as before.. but now i try to add the code to my current script but i cant get it to work… there is no error or so but the file just wont upload and send with the mail. below is the code can you pls help me out thanks

    Code:
    < ?php
    //start php session for error return
    SESSION_START();
    //error_reporting(0);
    if($_REQUEST['verstuur']){
    process_mail($_REQUEST);
    $_SESSION['new'] = 1;
    }

    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/pjpeg"))
    && ($_FILES["file"]["size"] < 20000))
    {
    if ($_FILES["file"]["error"] > 0)
    {
    echo “Return Code: ” . $_FILES["file"]["error"] . “
    “;
    }
    else
    {
    echo “Upload: ” . $_FILES["file"]["name"] . “
    “;
    echo “Type: ” . $_FILES["file"]["type"] . “
    “;
    echo “Size: ” . ($_FILES["file"]["size"] / 1024) . ” Kb
    “;
    echo “Temp file: ” . $_FILES["file"]["tmp_name"] . “
    “;

    if (file_exists(“upload/” . $_FILES["file"]["name"]))
    {
    echo $_FILES["file"]["name"] . ” already exists. “;
    }
    else
    {
    move_uploaded_file($_FILES["file"]["tmp_name"],
    “upload/” . $_FILES["file"]["name"]);
    echo “Stored in: ” . “upload/” . $_FILES["file"]["name"];
    }
    }
    }
    else
    {
    echo “Invalid file”;
    }

    function checkmail($mail)
    {
    // explode het e-mail adres op een @
    $email_host = explode(“@”, $mail);
    // pak alles na de @
    $email_host = $email_host['1'];
    // zet een var met de (eventuele) DNS van de domein
    $email_resolved = gethostbyname($email_host);

    // kijk of er een DNS aan zit en of alles voor en na het e-mail adres klopt
    if ($email_resolved != $email_host && eregi(“^[0-9a-z]([-_.~]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\.[a-z]{2,4}$”,$mail))
    $valid = 1;

    // wanneer het geldig is, is $valid = 1, zo niet, dan geeft hij geen result (NULL)
    return $valid;
    }

    function process_mail($data){
    // get posted data into local variables
    $EmailTo = “wing72@hotmail.com”;
    $Subject = “Mail vanuit website”;
    $name = mysql_escape_string($data['name']);
    $tel = mysql_escape_string($data['tel']);
    $email = mysql_escape_string($data['email']);
    $message = mysql_escape_string($data['message']);

    //validate the input
    if(!isset($name) || $name == “”) $_SESSION['errors']['Naam'] = true;
    if(!isset($tel) || $tel == “”) $_SESSION['errors']['Telefoon Nummer'] = true;
    if(!checkmail($email)) $_SESSION['errors']['Email Adres'] = true;
    if(!isset($message) || $message == “”) $_SESSION['errors']['Bericht'] = true;

    //als er errors zijn gaan we stoppen met sturen van email!
    if(sizeof($_SESSION['errors']) > 0){
    $_SESSION['error'] = true;
    return;
    }

    //vorige errors verwijderen
    unset($_SESSION['error']);
    unset($_SESSION['errors']);
    // prepare email body text
    $Body = “”;
    $Body .= “Name: “;
    $Body .= $name;
    $Body .= “n”;
    $Body .= “Email: “;
    $Body .= $email;
    $Body .= “n”;
    $Body .= “Tel: “;
    $Body .= $tel;
    $Body .= “n”;
    $Body .= “Details: “;
    $Body .= $message;
    $Body .= “n”;

    // send email
    $success = mail($EmailTo, $Subject, $Body, “From: < $email>“);
    if($success){
    //generate auto reply!
    mail($email, $Subject, “Beste $name,nnUw bericht is ontvangen door Chocolaterie Henrie.nWe zullen zo spoedig contact met u opnemen.nnMet vriendelijke groet,nn
    Chocolaterie Henri
    Tel: 013-5054939 | Email: info@chocolateriehenrie.nl
    “, “From: < $EmailTo>“);
    } else {
    $_SESSION['error'] = true;
    $_SESSION['sysError'] = “Er is een fout opgetreden tijdens het versturen van het formulier. Probeer later nog eens”;
    }
    }
    ?>

    < !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


    < ?php if($_SESSION['error'] || !isset($_REQUEST['verstuur']) ){
    //first time visit this page
    ?>

    Chocolaterie Henri

    Helfheuvelpassage 118
    5224 AP ‘s-Hertogenbosch
    073 622 1994

    Vul hier uw naam in.“; ?>
    Vul hier uw Email in.“; ?>
    Vul hier uw telefoonnummer in.“; ?>
    Laat uw bericht hier achter.“; ?>




    < ?php if(sizeof($_SESSION['errors']) > 0){
    unset($_SESSION['error']);
    unset($_SESSION['errors']);
    }?>

    < ?php } else {
    echo "

    “;
    echo “

    Uw bericht is verzonden.

    “;
    echo “

    “;
    session_destroy();
    }
    ?>


    Chocolaterie Henri

    hope to hear from you professionals soon

    wing

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".