PHP email form with attachments?

124678

Comments

  • edited September 2007
    how do I reset the page?
  • edited September 2007
    it keeps telling my that I forgot to add the email address?
  • KwitkoKwitko Sheriff of Banning (Retired) By the thing near the stuff Icrontian
    edited September 2007
    Take out the following:
    <code style="white-space: nowrap;"><code>[COLOR=#000000][COLOR=#007700]    if (!isset ([/COLOR][COLOR=#0000bb]$_POST[/COLOR][COLOR=#007700][[/COLOR][COLOR=#dd0000]'to_email'[/COLOR][COLOR=#007700]])) { [/COLOR][COLOR=#ff8000]//Oops, forgot your email addy! 
            [/COLOR][COLOR=#007700]die ([/COLOR][COLOR=#dd0000]"Oops!  You forgot to fill out the email address! Click on the back arrow to go back
    "[/COLOR][COLOR=#007700]); 
        } else {
    [/COLOR][/COLOR]
    
    </code></code> And take out one of the brackets right after:<code style="white-space: nowrap;"><code>
    </code></code><code style="white-space: nowrap;"><code>[COLOR=#000000][COLOR=#0000bb]fclose[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]$fp[/COLOR][COLOR=#007700]);[/COLOR][/COLOR]
    
    </code></code>
  • edited September 2007
    Thank you for the info but it gives me errors as to a line
    Kwitko wrote:
    Take out the following:
    <code style="white-space: nowrap;"><code>[COLOR=#000000][COLOR=#007700]    if (!isset ([/COLOR][COLOR=#0000bb]$_POST[/COLOR][COLOR=#007700][[/COLOR][COLOR=#dd0000]'to_email'[/COLOR][COLOR=#007700]])) { [/COLOR][COLOR=#ff8000]//Oops, forgot your email addy! 
            [/COLOR][COLOR=#007700]die ([/COLOR][COLOR=#dd0000]"Oops!  You forgot to fill out the email address! Click on the back arrow to go back
    "[/COLOR][COLOR=#007700]); 
        } else {
    [/COLOR][/COLOR]
    
    </code></code> And take out one of the brackets right after:<code style="white-space: nowrap;"><code>
    </code></code><code style="white-space: nowrap;"><code>[COLOR=#000000][COLOR=#0000bb]fclose[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]$fp[/COLOR][COLOR=#007700]);[/COLOR][/COLOR]
    
    </code></code>
  • edited September 2007
    Ok I have figured out the to email part but I need help with a redirecting and where to place the below
    Kwitko wrote:
    I have some code to check for file extensions:
    [php]$allowedext = new array('jpg', 'gif', 'png');
    $ext = substr($_FILES, strrpos($_FILES, '.') + 1);
    if (!in_array($ext, $allowedext)) {
    unlink($_FILES);
    exit("File type $ext is not allowed.");
    }
    [/php]You would throw this in before the part that encodes the files to base64.
  • edited September 2007
    Sorry I've been really busy lately. Has anyone had a chance to look at my code? :)
  • edited September 2007
    What are you tring to do??
  • edited November 2007
    Hi- i'm only a moderately-experienced user of PHP script, and this seems like the ONLY reliable script I have found so far on the web, and I am very very grateful to have found this site- you have no idea!

    I am trying to add an email form with attachment field to my website for my business---for the most part, I understand the script, but would it be possible to please list exactly which fields I would need to change?

    For example, I know I would need to add my email adddress to the "To" field, but which line exactly is this and are there any other lines that require modifications?

    Thank you very much
  • edited November 2007
    yayasolete wrote:
    Hi- i'm only a moderately-experienced user of PHP script, and this seems like the ONLY reliable script I have found so far on the web, and I am very very grateful to have found this site- you have no idea!

    I am trying to add an email form with attachment field to my website for my business---for the most part, I understand the script, but would it be possible to please list exactly which fields I would need to change?

    For example, I know I would need to add my email adddress to the "To" field, but which line exactly is this and are there any other lines that require modifications?

    Thank you very much


    anyone please ..? :( realllly need help...
  • LincLinc Owner Detroit Icrontian
    edited November 2007
    You're asking a very vague question - do you mean you need a breakdown of every place you need to substitute in personal values rather than the stock ones provided? That's kinda hard for us to figure out unless we know exactly what you're doing.


    In other news, there's a sweet PHP module to simplify sending email with attachments: http://phpmailer.sourceforge.net/
  • edited December 2007
    Good morning, I know it's been a while since this thread really got much lovin' but I've been beating my head against the wall for a bit here and it's not looking pretty.

    Kwitko, and A2J, I love you guys, thank you so much for your contributions here, can't say enough about that.

    I've got a form that I've whipped together for Incident reporting. Basically, a user will fill out the form, submit it, so that the receiving dept can open a trouble ticket for the issue and get the matter resolved.

    When I used Kwitko's code to send with 1 attachment, it worked fine (although I was still getting the weird .dat file if no attachments were found). After initial testing, the end users requested the ability to add multiple attachments. So I inserted A2Js dynamic table building code for adding multiple attachments.

    The table gets built dynamically, no question, the input fields seem to work, however, at run time, I'm presented with the following:
    [B]Notice[/B]: Undefined index: attachment in  [B]C:\Inetpub\wwwroot\ticket\test.php[/B] on line  [B]279[/B]
    
    [B]Notice[/B]: Undefined index: attachment in  [B]C:\Inetpub\wwwroot\ticket\test.php[/B] on line [B]280[/B]
    

    Now, I'm getting no attachments coming through correctly, and some new blank attachment placeholder with a name of 'Array'.

    Here is the source that I've been working from. (I'm still VERY green with PHP, I've written a whopping 2 php / mysql applications at this time, so if there is some newbness in the coding, please be gentle).
    I've included both the HTML for the form, as well as the PHP for the actual sendmail.

    The errors keep referring to the way that I call the attachment variable, but I'm not seeing what's different from the way that I was doing it using Kwitko's single attachment method, and A2j's dynamic multi-attachment technique. Any assistance would be fantabulous!

    [HTML]
    <body onload='init()' topmargin='0' leftmargin='0'>

    <table width='100%' cellpadding='0' cellspacing='0'>
    <tr>
    <td bgcolor='#000000'><br>
    <center><h2>Incident Ticket Request Form</h2></center>
    </td>
    </tr>
    <tr>
    <td bgcolor='#99cc00'>
    </td>
    </tr>
    </table>
    <form enctype="multipart/form-data" name="send" method="post" action="<?php $_SERVER?>">
    <input type="hidden" name="action" value="send" />
    <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />

    <table border='0' cellpadding='3 cellspacing='0' width='100%'>

    <tr valign='top'>
    <td><center>
    <fieldset>
    <legend><b>Contact Details</b></legend>
    <input type="hidden" name="action" value="send" />
    <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
    <!--Recipient Name: <input name="to_name" size="50" />-->
    <!--Recipient Email: <input name="to_email" size="50" />-->
    <br>
    <table border='0' cellpadding='3' cellspacing='0' width='750'>
    <tr valign='top'>
    <td align='right'>Your LAN ID: </td>
    <td align='left'><input name='lanid'></td>
    </tr>
    <tr valign='top'>
    <td align='right'>Your E-mail Address: </td>
    <td align='left'><input name='email'></td>
    </tr>
    <tr valign='top'>
    <td align='right'>Contact Person: </td>
    <td align='left'><input name='contact'></td>
    </tr>
    <tr valign='top'>
    <td align='right'>Contact Phone #: </td>
    <td align='left'><input name='contactphone'></td>
    </tr>
    </table><br><br><br>
    </fieldset>
    </center>
    </td>
    </tr>


    <td>
    <center>
    <fieldset><br>
    <legend><b>Incident Information</b></legend>
    <br>
    <table border='0' cellpadding='3' cellspacing='0' width='750'>
    <tr valign='top'>
    <td align='right'>Application Name: </td>
    <td align='left'><input name='appname'></td>
    </tr>
    <tr valign='top'>
    <td align='right'># of users impacted: </td>
    <td align='left'><input name='numusers' size='5'></td>
    </tr>
    <tr valign='top'>
    <td align='right'>User's Location: </td>
    <td align='left'><input name='location'></td>
    </tr>
    <tr valign='top'>
    <td align='right'>User's Department: </td>
    <td align='left'><input name='dept'></td>
    </tr>
    <tr valign='top'>
    <td align='right'>Error Message: </td>
    <td align='left'><textarea name='error' cols='50' rows='5' wrap='physical'></textarea></td>
    </tr>
    <tr valign='top'>
    <td align='right'>When does the error occur: </td>
    <td align='left'><textarea name='occurs' cols='50' rows='5' wrap='physical'></textarea></td>
    </tr>
    </table><br>
    </fieldset>
    </center>
    </td>
    </tr>
    <tr valign='top'>
    <tr valign='top'>
    <td>
    <center>
    <fieldset>
    <legend><b>Impacted User Info</b></legend>
    <br>
    <table border='0' cellpadding='3' cellspacing='0' width='750'>

    <tr valign='bottom'>
    <td align='middle'>LAN ID</td>
    <td align='middle'>Workstation</td>
    <td align='middle'>App ID</td>
    <td align='middle'>Account Example</td>
    </tr>
    <tr valign='top'>
    <td>1.<input name='lan1'></td>
    <td><input name='workstation1'></td>
    <td><input name='appid1'></td>
    <td><input name='account1'></td>
    </tr>
    <tr valign='top'>
    <td>2.<input name='lan2'></td>
    <td><input name='workstation2'></td>
    <td><input name='appid2'></td>
    <td><input name='account2'></td>
    </tr>
    <tr valign='top'>
    <td>3.<input name='lan3'></td>
    <td><input name='workstation3'></td>
    <td><input name='appid3'></td>
    <td><input name='account3'></td>
    </tr>
    <tr valign='top'>
    <td>4.<input name='lan4'></td>
    <td><input name='workstation4'></td>
    <td><input name='appid4'></td>
    <td><input name='account4'></td>
    </tr>
    <tr valign='top'>
    <td>5.<input name='lan5'></td>
    <td><input name='workstation5'></td>
    <td><input name='appid5'></td>
    <td><input name='account5'></td>
    </tr>

    </table><br>
    </fieldset>
    </center>
    </td>
    </tr>
    <tr valign='top'>
    <td>
    <center>
    <fieldset><br>
    <legend><b>Impact Information</b></legend>
    <br>
    <table border='0' cellpadding='3' cellspacing='0' width='750'>
    <tbody id="emailTBody">
    <tr valign='top'>
    <td align='right'>Requested Impact: </td>
    <td align='left'><select name="impact" size="1">
    <!--<option value="3-Medium">3-Medium</option>-->
    <option value="2-High">2-High</option>
    <option value="1-Critical">1-Critical</option>
    </select>
    </td>
    </tr>
    <tr valign='top'>
    <td align='right'>What is the impact to the Department or Client?</td>
    <td align='left'><textarea name='impactdesc' cols='50' rows='5' wrap='physical'></textarea></td>
    </tr>
    <tr valign='top'>
    <td align='right'>Is there a Work Around? If so, what is it?</td>
    <td align='left'><textarea name='workaround' cols='50' rows='5' wrap='physical'></textarea></td>
    </tr>
    <tr valign='top'>
    <td align='right'>Attachment: </td>
    <td align='left'>
    <!-- <input type="file" name="attachment"/>-->
    <input type="button" name="add_attachment" value="Add attachment" onclick="addAttachment()">
    </td>
    <tr valign='top'>
    <td></td>
    <td>
    <br>
    <input type="submit" value="Send Email" />
    </td>
    </tr>
    </table><br><br>
    </fieldset>
    </center>
    </td>
    </tr>


    </table>
    </form>

    ******** type="text/javascript" language="javascript">
    var trow;

    function init()
    {
    trow = 2;
    }

    function addAttachment()
    {
    var tbodyElem = document.getElementById("emailTBody");
    var trElem, tdElem, txtNode, tmpString;

    trElem = tbodyElem.insertRow(trow);

    tdElem = trElem.insertCell(0);
    tdElem.className = "gradient";
    tdElem.setAttribute("style", "text-align: right;");
    txtNode = document.createTextNode("Attachment " + (trow - 1) + ":");
    tdElem.appendChild(txtNode);


    tdElem = trElem.insertCell(1);
    tdElem.className = "gradient";
    tdElem.setAttribute("style", "text-align: left;");
    tdElem.setAttribute("colspan", "3");

    var input = document.createElement("input");
    input.setAttribute("type", "file");
    input.setAttribute("name", "attachment" + (trow - 1));
    input.setAttribute("size", "60");
    input.setAttribute("value", "");
    input.setAttribute("tmp_name", "attachment" + (trow - 1));
    tdElem.appendChild(input);

    trow++;
    }
    *********>
    <br><br>

    [/HTML]
    [PHP]
    <?php
    }

    function sendMail() {





    // if (!isset ($_POST)) { //Oops, forgot your email addy!
    // die ("<p>Oops! You forgot to fill out the email address! Click on the back arrow to go back</p>");
    // }
    // else {
    //$to_name = "Incident Control ";

    //******* VARIABLE SETUP *******//
    //$from_name = stripslashes($_POST);
    //$subject = stripslashes($_POST);
    $filecount = 0;




    //$body = stripslashes($_POST);

    $to_email = "xxx@xxxx.com";
    $attachment = $_FILES;
    $attachment_name = $_FILES;
    $lanid = stripslashes($_POST);
    $email = stripslashes($_POST);
    $contact = stripslashes($_POST);
    $contactphone = stripslashes($_POST);
    $lan1 = stripslashes($_POST);
    $lan2 = stripslashes($_POST);
    $lan3 = stripslashes($_POST);
    $lan4 = stripslashes($_POST);
    $workstation1 = stripslashes($_POST);
    $workstation2 = stripslashes($_POST);
    $workstation3 = stripslashes($_POST);
    $workstation4 = stripslashes($_POST);
    $appid1 = stripslashes($_POST);
    $appid2 = stripslashes($_POST);
    $appid3 = stripslashes($_POST);
    $appid4 = stripslashes($_POST);
    $account1 = stripslashes($_POST);
    $account2 = stripslashes($_POST);
    $account3 = stripslashes($_POST);
    $account4 = stripslashes($_POST);
    $appname = stripslashes($_POST);
    $numusers = stripslashes($_POST);
    $location = stripslashes($_POST);
    $dept = stripslashes($_POST);
    $error = stripslashes($_POST);
    $occurs = stripslashes($_POST);
    $impact = $_POST;
    $impactdesc = stripslashes($_POST);
    $workaround = stripslashes($_POST);
    $subject = $impact . " Impact Ticket Request.";
    //if ($impact != "3-Medium")
    // $to_email = "yyy@yyyy.com";
    //else
    // $to_email = "xxxx@xxxx.com";
    //***** END VARIABLE SETUP *****//

    foreach($_FILES as $file => $value) {
    $attachment[(int)$filecount] = $_FILES[$file];
    $attachment_name[(int)$filecount] = $_FILES[$file];
    if (is_uploaded_file($attachment[(int)$filecount])) { //Do we have a file uploaded?
    $fp = fopen($attachment[(int)$filecount], "rb"); //Open it
    $data[(int)$filecount] = fread($fp, filesize($attachment[(int)$filecount])); //Read it
    $data[(int)$filecount] = chunk_split(base64_encode($data[(int)$filecount])); //Chunk it up and encode it as base64 so it can emailed
    fclose($fp);
    $filecount++;
    }


    }
    //Let's start our headers
    $headers = "From: $lanid<" . $_POST . ">\n";
    $headers .= "Reply-To: <" . $_POST . ">\n";
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n";
    $headers .= "X-Sender: $lanid<" . $_POST . ">\n";
    $headers .= "X-Mailer: PHP4\n";
    $headers .= "X-Priority: 3\n"; //1 = Urgent, 3 = Normal
    $headers .= "Return-Path: <" . $_POST . ">\n";
    $headers .= "This is a multi-part message in MIME format.\n";
    $headers .= "
    =MIME_BOUNDRY_main_message \n";
    $headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDRY_message_parts\"\n";

    $message = "
    =MIME_BOUNDRY_message_parts\n";
    $message .= "Content-Type: text/html; charset=\"iso-8859-1\"\n";
    $message .= "Content-Transfer-Encoding: quoted-printable\n";
    $message .= "\n";
    /* Add our message, in this case it's plain text. You could also add HTML by changing the Content-Type to text/html */
    //$message .= "$body\n";
    $message .= "<b><u>Contact Details</u></b><br>";
    $message .= "<b>Requestor:</b> " . $lanid . "<br>";
    $message .= "<b>Requestor E-mail:</b> " . $email . "<br>";
    $message .= "<b>Contact Person:</b> " . $contact . "<br>";
    $message .= "<b>Must Answer #:</b> " . $contactphone . "<br><br><br>";
    $message .= "<b><u>Incident Information</u></b><br>";
    $message .= "<b>Application:</b> " . $appname . "<br>";
    $message .= "<b># of Users Impacted:</b> " . $numusers . "<br>";
    $message .= "<b>Location of Users:</b> " . $location . "<br>";
    $message .= "<b>Department:</b> " . $dept . "<br>";
    $message .= "<b>Error Message:</b> " . $error . "<br>";
    $message .= "<b>Error Occurs When:</b> " . $occurs . "<br><br><br>";
    $message .= "<b><u>Example ID's</u></b><br>";
    $message .= "<table border='0' cellpadding='5' cellspacing='0'>";
    $message .= "<tr valign='top'><td>LAN ID</td><td>Workstation</td><td>Application ID</td><td>Account Example</td></tr>";
    $message .= "<tr valign='top'><td>" . $lan1 . "</td><td>" . $workstation1 . "</td><td>" . $appid1 . "</td><td>" . $account1 . "</td></tr>";
    $message .= "<tr valign='top'><td>" . $lan2 . "</td><td>" . $workstation2 . "</td><td>" . $appid2 . "</td><td>" . $account2 . "</td></tr>";
    $message .= "<tr valign='top'><td>" . $lan3 . "</td><td>" . $workstation3 . "</td><td>" . $appid3 . "</td><td>" . $account3 . "</td></tr>";
    $message .= "<tr valign='top'><td>" . $lan4 . "</td><td>" . $workstation4 . "</td><td>" . $appid4 . "</td><td>" . $account4 . "</td></tr>";
    $message .= "</table><br><br>";
    $message .= "<b><u>Impact Information</u></b><br>";
    $message .= "<b>WTS Requested Impact:</b> " . $impact . "<br>";
    $message .= "<b>Impact Description:</b> " . $impactdesc . "<br>";
    $message .= "<b>Workaround:</b> " . $workaround . "<br>";



    $message .= "\n";
    $message .= "
    =MIME_BOUNDRY_message_parts--\n";
    $message .= "\n";
    for ($i = 0, $filecount = (int) count($data); $i < $filecount; $i++) {
    $message .= "
    =MIME_BOUNDRY_main_message\n";
    $message .= "Content-Type: application/octet-stream;\n\tname=\"" . $attachment_name . "\"\n";
    $message .= "Content-Transfer-Encoding: base64\n";
    $message .= "Content-Disposition: attachment;\n\tfilename=\"" . $attachment_name . "\"\n\n";
    $message .= (isset($data)); //The base64 encoded message
    $message .= "\n";
    }
    $message .= "
    =MIME_BOUNDRY_main_message--\n";

    // send the message
    mail("$to_email", $subject, $message, $headers);
    print "Ticket Request Sent. Thank you.";





    }
    //}

    ?>



    <?php
    switch ($action) {
    case "send":
    sendMail();
    showForm();
    break;
    default:
    showForm();
    }

    ?>
    [/PHP]
  • edited December 2007
    Sorry for the ginormous post above :(
  • edited December 2007
    I dont understand why we cant get a script that works??? True this is one of the few places on the net that has anything even close to working, but why is it so difficult? Why is there not 1 file with text that reads- use this it works?

    I dotn get why there are so many bugs. Surely this isnt rocket science
  • LincLinc Owner Detroit Icrontian
    edited December 2007
    Grimm, you reference $_FILES but it looks like the name of the file field in the HTML is 'add_attachment', which means you should be referencing $_FILES instead.

    Yaya, if you're impatient with the format in which the solution is presented, you're welcome to improve upon it until your heart is content. The rest of us have day jobs and social lives so we settle for "good enough". Let's not take out impatience about not understanding the solution (and i.e. how to resolve issues on your own) on the people who've taken the time to explain so much already.
  • edited December 2007
    >>GRIMM
    I believe I might have fixed your script, here's the whole thing in one file I put together:

    [PHP]
    <?php
    error_reporting(E_ALL);
    if(isset($_POST)&&$_POST=="send") {
    sendMail();
    } else {
    ?>
    [/PHP]
    [HTML]
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
    <html>
    <head>
    ****** http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    <body>
    <body onload='init()' topmargin='0' leftmargin='0'>

    <table width='100%' cellpadding='0' cellspacing='0'>
    <tr>
    <td bgcolor='#000000'>
    <center><h2>Incident Ticket Request Form</h2></center>
    </td>
    </tr>
    <tr>
    <td bgcolor='#99cc00'>
    </td>
    </tr>
    </table>
    <form enctype="multipart/form-data" name="send" method="post" action="<?php $_SERVER?>">
    <input type="hidden" name="action" value="send" />
    <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />

    <table border='0' cellpadding='3 cellspacing='0' width='100%'>

    <tr valign='top'>
    <td><center>
    <fieldset>
    <legend><b>Contact Details</b></legend>
    <input type="hidden" name="action" value="send" />
    <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
    <!--Recipient Name: <input name="to_name" size="50" />-->
    <!--Recipient Email: <input name="to_email" size="50" />-->

    <table border='0' cellpadding='3' cellspacing='0' width='750'>
    <tr valign='top'>
    <td align='right'>Your LAN ID: </td>
    <td align='left'><input name='lanid'></td>
    </tr>
    <tr valign='top'>
    <td align='right'>Your E-mail Address: </td>
    <td align='left'><input name='email'></td>
    </tr>
    <tr valign='top'>
    <td align='right'>Contact Person: </td>
    <td align='left'><input name='contact'></td>
    </tr>
    <tr valign='top'>
    <td align='right'>Contact Phone #: </td>
    <td align='left'><input name='contactphone'></td>
    </tr>
    </table>
    </fieldset>
    </center>
    </td>
    </tr>


    <td>
    <center>
    <fieldset>
    <legend><b>Incident Information</b></legend>

    <table border='0' cellpadding='3' cellspacing='0' width='750'>
    <tr valign='top'>
    <td align='right'>Application Name: </td>
    <td align='left'><input name='appname'></td>
    </tr>
    <tr valign='top'>
    <td align='right'># of users impacted: </td>
    <td align='left'><input name='numusers' size='5'></td>
    </tr>
    <tr valign='top'>
    <td align='right'>User's Location: </td>
    <td align='left'><input name='location'></td>
    </tr>
    <tr valign='top'>
    <td align='right'>User's Department: </td>
    <td align='left'><input name='dept'></td>
    </tr>
    <tr valign='top'>
    <td align='right'>Error Message: </td>
    <td align='left'><textarea name='error' cols='50' rows='5' wrap='physical'></textarea></td>
    </tr>
    <tr valign='top'>
    <td align='right'>When does the error occur: </td>
    <td align='left'><textarea name='occurs' cols='50' rows='5' wrap='physical'></textarea></td>
    </tr>
    </table>
    </fieldset>
    </center>
    </td>
    </tr>
    <tr valign='top'>
    <tr valign='top'>
    <td>
    <center>
    <fieldset>
    <legend><b>Impacted User Info</b></legend>

    <table border='0' cellpadding='3' cellspacing='0' width='750'>

    <tr valign='bottom'>
    <td align='middle'>LAN ID</td>
    <td align='middle'>Workstation</td>
    <td align='middle'>App ID</td>
    <td align='middle'>Account Example</td>
    </tr>
    <tr valign='top'>
    <td>1.<input name='lan1'></td>
    <td><input name='workstation1'></td>
    <td><input name='appid1'></td>
    <td><input name='account1'></td>
    </tr>
    <tr valign='top'>
    <td>2.<input name='lan2'></td>
    <td><input name='workstation2'></td>
    <td><input name='appid2'></td>
    <td><input name='account2'></td>
    </tr>
    <tr valign='top'>
    <td>3.<input name='lan3'></td>
    <td><input name='workstation3'></td>
    <td><input name='appid3'></td>
    <td><input name='account3'></td>
    </tr>
    <tr valign='top'>
    <td>4.<input name='lan4'></td>
    <td><input name='workstation4'></td>
    <td><input name='appid4'></td>
    <td><input name='account4'></td>
    </tr>
    <tr valign='top'>
    <td>5.<input name='lan5'></td>
    <td><input name='workstation5'></td>
    <td><input name='appid5'></td>
    <td><input name='account5'></td>
    </tr>

    </table>
    </fieldset>
    </center>
    </td>
    </tr>
    <tr valign='top'>
    <td>
    <center>
    <fieldset>
    <legend><b>Impact Information</b></legend>

    <table border='0' cellpadding='3' cellspacing='0' width='750'>
    <tbody id="emailTBody">
    <tr valign='top'>
    <td align='right'>Requested Impact: </td>
    <td align='left'><select name="impact" size="1">
    <!--<option value="3-Medium">3-Medium</option>-->
    <option value="2-High">2-High</option>
    <option value="1-Critical">1-Critical</option>
    </select>
    </td>
    </tr>
    <tr valign='top'>
    <td align='right'>What is the impact to the Department or Client?</td>
    <td align='left'><textarea name='impactdesc' cols='50' rows='5' wrap='physical'></textarea></td>
    </tr>
    <tr valign='top'>
    <td align='right'>Is there a Work Around? If so, what is it?</td>
    <td align='left'><textarea name='workaround' cols='50' rows='5' wrap='physical'></textarea></td>
    </tr>
    <tr valign='top'>
    <td align='right'>Attachment: </td>
    <td align='left'>
    <!-- <input type="file" name="attachment"/>-->
    <input type="button" name="add_attachment" value="Add attachment" onclick="addAttachment()">
    </td>
    <tr valign='top'>
    <td></td>
    <td>

    <input type="submit" value="Send Email" />
    </td>
    </tr>
    </table>
    </fieldset>
    </center>
    </td>
    </tr>


    </table>
    </form>

    ******** type="text/javascript" language="javascript">
    var trow;

    function init()
    {
    trow = 2;
    }

    function addAttachment()
    {
    var tbodyElem = document.getElementById("emailTBody");
    var trElem, tdElem, txtNode, tmpString;

    trElem = tbodyElem.insertRow(trow);

    tdElem = trElem.insertCell(0);
    tdElem.className = "gradient";
    tdElem.setAttribute("style", "text-align: right;");
    txtNode = document.createTextNode("Attachment " + (trow - 1) + ":");
    tdElem.appendChild(txtNode);


    tdElem = trElem.insertCell(1);
    tdElem.className = "gradient";
    tdElem.setAttribute("style", "text-align: left;");
    tdElem.setAttribute("colspan", "3");

    var input = document.createElement("input");
    input.setAttribute("type", "file");
    input.setAttribute("name", "attachment[]");
    input.setAttribute("size", "60");
    input.setAttribute("value", "");
    tdElem.appendChild(input);

    trow++;
    }
    *********>


    </body>
    </html>
    [/HTML]
    [PHP]
    <?php

    }

    function sendMail() {

    // if (!isset ($_POST)) { //Oops, forgot your email addy!
    // die ("<p>Oops! You forgot to fill out the email address! Click on the back arrow to go back</p>");
    // }
    // else {
    //$to_name = "Incident Control ";

    //******* VARIABLE SETUP *******//
    //$from_name = stripslashes($_POST);
    //$subject = stripslashes($_POST);
    $filecount = 0;


    //$body = stripslashes($_POST);

    $to_email = "xxxx@xxxx.com";
    $lanid = stripslashes($_POST);
    $email = stripslashes($_POST);
    $contact = stripslashes($_POST);
    $contactphone = stripslashes($_POST);
    $lan1 = stripslashes($_POST);
    $lan2 = stripslashes($_POST);
    $lan3 = stripslashes($_POST);
    $lan4 = stripslashes($_POST);
    $workstation1 = stripslashes($_POST);
    $workstation2 = stripslashes($_POST);
    $workstation3 = stripslashes($_POST);
    $workstation4 = stripslashes($_POST);
    $appid1 = stripslashes($_POST);
    $appid2 = stripslashes($_POST);
    $appid3 = stripslashes($_POST);
    $appid4 = stripslashes($_POST);
    $account1 = stripslashes($_POST);
    $account2 = stripslashes($_POST);
    $account3 = stripslashes($_POST);
    $account4 = stripslashes($_POST);
    $appname = stripslashes($_POST);
    $numusers = stripslashes($_POST);
    $location = stripslashes($_POST);
    $dept = stripslashes($_POST);
    $error = stripslashes($_POST);
    $occurs = stripslashes($_POST);
    $impact = $_POST;
    $impactdesc = stripslashes($_POST);
    $workaround = stripslashes($_POST);
    $subject = $impact . " Impact Ticket Request.";
    //if ($impact != "3-Medium")
    // $to_email = "yyy@yyyy.com";
    //else
    // $to_email = "xxxx@xxxx.com";
    //***** END VARIABLE SETUP *****//

    //Let's start our headers
    $headers = "From: $lanid<" . $_POST . ">\n";
    $headers .= "Reply-To: <" . $_POST . ">\n";
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n";
    $headers .= "X-Sender: $lanid<" . $_POST . ">\n";
    $headers .= "X-Mailer: PHP4\n";
    $headers .= "X-Priority: 3\n"; //1 = Urgent, 3 = Normal
    $headers .= "Return-Path: <" . $_POST . ">\n";
    $headers .= "This is a multi-part message in MIME format.\n";
    $headers .= "
    =MIME_BOUNDRY_main_message \n";
    $headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDRY_message_parts\"\n";

    $message = "
    =MIME_BOUNDRY_message_parts\n";
    $message .= "Content-Type: text/html; charset=\"iso-8859-1\"\n";
    $message .= "Content-Transfer-Encoding: quoted-printable\n";
    $message .= "\n";
    /* Add our message, in this case it's plain text. You could also add HTML by changing the Content-Type to text/html */
    //$message .= "$body\n";
    $message .= "<b><u>Contact Details</u></b>";
    $message .= "<b>Requestor:</b> " . $lanid . "";
    $message .= "<b>Requestor E-mail:</b> " . $email . "";
    $message .= "<b>Contact Person:</b> " . $contact . "";
    $message .= "<b>Must Answer #:</b> " . $contactphone . "";
    $message .= "<b><u>Incident Information</u></b>";
    $message .= "<b>Application:</b> " . $appname . "";
    $message .= "<b># of Users Impacted:</b> " . $numusers . "";
    $message .= "<b>Location of Users:</b> " . $location . "";
    $message .= "<b>Department:</b> " . $dept . "";
    $message .= "<b>Error Message:</b> " . $error . "";
    $message .= "<b>Error Occurs When:</b> " . $occurs . "";
    $message .= "<b><u>Example ID's</u></b>";
    $message .= "<table border='0' cellpadding='5' cellspacing='0'>";
    $message .= "<tr valign='top'><td>LAN ID</td><td>Workstation</td><td>Application ID</td><td>Account Example</td></tr>";
    $message .= "<tr valign='top'><td>" . $lan1 . "</td><td>" . $workstation1 . "</td><td>" . $appid1 . "</td><td>" . $account1 . "</td></tr>";
    $message .= "<tr valign='top'><td>" . $lan2 . "</td><td>" . $workstation2 . "</td><td>" . $appid2 . "</td><td>" . $account2 . "</td></tr>";
    $message .= "<tr valign='top'><td>" . $lan3 . "</td><td>" . $workstation3 . "</td><td>" . $appid3 . "</td><td>" . $account3 . "</td></tr>";
    $message .= "<tr valign='top'><td>" . $lan4 . "</td><td>" . $workstation4 . "</td><td>" . $appid4 . "</td><td>" . $account4 . "</td></tr>";
    $message .= "</table>";
    $message .= "<b><u>Impact Information</u></b>";
    $message .= "<b>WTS Requested Impact:</b> " . $impact . "";
    $message .= "<b>Impact Description:</b> " . $impactdesc . "";
    $message .= "<b>Workaround:</b> " . $workaround . "";



    $message .= "\n";
    $message .= "
    =MIME_BOUNDRY_message_parts--\n";
    $message .= "\n";

    foreach ($_FILES["attachment"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
    $tmp_name = $_FILES["attachment"]["tmp_name"][$key];
    $name = $_FILES["attachment"]["name"][$key];
    $message .= "
    =MIME_BOUNDRY_main_message\n";
    $message .= "Content-Type: application/octet-stream;\n\tname=\"" . $name . "\"\n";
    $message .= "Content-Transfer-Encoding: base64\n";
    $message .= "Content-Disposition: attachment;\n\tfilename=\"" . $name . "\"\n\n";
    $fp = fopen($tmp_name, "rb"); //Open it
    $tmp_file = fread($fp, filesize($tmp_name)); //Read it
    $message .= chunk_split(base64_encode($tmp_file)); //Chunk it up and encode it as base64 so it can emailed
    fclose($fp);
    $message .= "\n";

    }
    }

    $message .= "
    =MIME_BOUNDRY_main_message--\n";

    // send the message
    mail("$to_email", $subject, $message, $headers);
    print "Ticket Request Sent. Thank you.";
    }
    ?>

    [/PHP]

    This works perfectly for me and I have to thank you and everyone here for figuring this all out
  • edited December 2007
    Keebler wrote:
    Grimm, you reference $_FILES but it looks like the name of the file field in the HTML is 'add_attachment', which means you should be referencing $_FILES instead.

    Keebler, thanks for the quick reply!

    I just tried modifying the 2 lines that were references $_FILES as follows:

    [php] $attachment = $_FILES;
    $attachment_name = $_FILES;
    [/php]Unfortunately, it returned the same result.

    Still getting
    [B]Notice[/B]: Undefined index: add_attachment in  [B]C:\Inetpub\wwwroot\ticket\test.php[/B] on line  [B]280[/B]
    
    [B]Notice[/B]: Undefined index: add_attachment in  [B]C:\Inetpub\wwwroot\ticket\test.php[/B] on line [B]281[/B]
    
    And the attachment that is coming through is an invalid file named Array (64B). If I attach more than one file, i get multiple invalid attachments named Array (64B). :(

    The lines specifically mentioned in the errors are as follows:

    [php]
    279 $attachment = $_FILES;
    280 $attachment_name = $_FILES;
    281 $lanid = stripslashes($_POST);
    282 $email = stripslashes($_POST);[/php]
    I've got to be missing something else. I took some time away from this project today to build a dynamic url builder to select a particular file from a directory and display it on screen (not incredibly difficult) and I'll hope to come back to this with a clear head tomorrow.

    Thanks again for the input, any further assistance in righting the ways of this poor php programmer *cough hack job cough* would be greatly appreciated.
  • edited December 2007
    Mat. (Great name from a great series of books, too bad about ol' RJ though :()

    I'll definitely take a look at this tomorrow when I get back, thank you so much!!
  • edited December 2007
    Keebler wrote:
    Grimm, you reference $_FILES but it looks like the name of the file field in the HTML is 'add_attachment', which means you should be referencing $_FILES instead.

    Yaya, if you're impatient with the format in which the solution is presented, you're welcome to improve upon it until your heart is content. The rest of us have day jobs and social lives so we settle for "good enough". Let's not take out impatience about not understanding the solution (and i.e. how to resolve issues on your own) on the people who've taken the time to explain so much already.

    sorry- i only settle for the best, not good enough
  • RyderRyder Kalamazoo, Mi Icrontian
    edited December 2007
    The point is that no one here is getting paid to do this for you, if you don't like the progress....get busy?

    This site is about people helping people, if your going to stand there and crack the whip, crack it somewhere else, or offer something constructive.

    I do mean that in the nicest way, no insult intended :)
  • ThraxThrax 🐌 Austin, TX Icrontian
    edited December 2007
    yayasolete wrote:
    sorry- i only settle for the best, not good enough

    Good, then make it happen.
  • edited December 2007
    RyderOCZ wrote:
    The point is that no one here is getting paid to do this for you, if you don't like the progress....get busy?

    This site is about people helping people, if your going to stand there and crack the whip, crack it somewhere else, or offer something constructive.

    I do mean that in the nicest way, no insult intended :)

    I understand- I'm just too dumb to contribute anything to this thread.

    Therefore, I rely on your intelligent minds to do my work for me. You should be honored- I have so much faith in you that I expected this script to be resolved sooner

    get workin boys! *crack*
  • LincLinc Owner Detroit Icrontian
    edited December 2007
    Grimm, I made a mistake. It should indeed be "attachment", not "add_attachment". Your issue is that the file input field is commented out. If you don't want it to show up, you must use CSS to set "display: none;" for that element. Commenting out means it literally does not exist, and so it will not submit with the form.

    Also, I don't understand what your "Add Attachment" button is doing, but most browsers won't let you modify a file input box with javascript - that would be a huge security issue.

    yayasolete wrote:
    I rely on your intelligent minds to do my work for me.
    None of us are here to do your work for you; we simply offer guidance. I suggest moving on now unless you have a new question or want to expand on your previous one.
  • edited December 2007
    Keebler wrote:
    Grimm, I made a mistake. It should indeed be "attachment", not "add_attachment". Your issue is that the file input field is commented out. If you don't want it to show up, you must use CSS to set "display: none;" for that element. Commenting out means it literally does not exist, and so it will not submit with the form.


    None of us are here to do your work for you; we simply offer guidance. I suggest moving on now unless you have a new question or want to expand on your previous one.

    no- i have no more useless input

    i will move on

    thank you all for the script.
  • edited December 2007
    >>GRIMM
    I believe I might have fixed your script, here's the whole thing in one file I put together:

    This works perfectly for me and I have to thank you and everyone here for figuring this all out


    I <3 You! I won't lie, I'm not sure what you did to my code, but it's working!!! Bwhahahahaha! <evil laugh>

    Thank you!!!
  • edited December 2007
    One small thing, not a huge deal as the tool is functioning, but when 'NO' attachment is added (more accurately, if the 'Add attachment' button is not clicked) and the form gets submitted, I get the following error (note, the message still gets sent, just presents an ugly error to the user):
    [B]Notice[/B]: Undefined index: attachment in  [B]C:\Inetpub\wwwroot\ticket\test3.php[/B] on line  [B]384[/B]
    
    [B]Warning[/B]: Invalid argument supplied for foreach() in  [B]C:\Inetpub\wwwroot\ticket\test3.php[/B] on line [B]384[/B]
    
    The line of code it's referring to is as follows:
    [php]
    foreach ($_FILES["attachment"]["error"] as $key => $error)
    [/php]

    If I throw in an isset check, it gets rid of the notice message, but prevents any attachments from being sent, and the Invalid Argument warning shows up whether a file has been attached or not.

    [php]
    foreach ((isset($_FILES["attachment"]["error"])) as $key => $error)
    [/php]Any thoughts?
  • edited December 2007
    check if there are files outside of the loop first:
    [php]
    if(isset($_FILES["attachment"])) {
    foreach ($_FILES["attachment"]["error"] as $key => $error) {
    [/php]etc...

    the first parameter of the for each is the collection it's looping through, so that can't be the boolean result of "is the file set?".
  • edited December 2007
    **** EDIT (again...) ****

    Thank you Mat, I did the isset as suggested, seems to be working fine now, with and without attachments. I'm going to try to stop harassing you guys about this for a while :D

    **** END EDIT ****

    Cool! Any thoughts as to the Invalid Argument message that I'm getting if no attachment is selected (or more accurately if the "Add Attachment" button has NOT been pressed at least once)?
  • edited December 2007
    To work around this problem, use SQLSetConnectOption to set the SQL_ODBC_CURSORS to SQL_CUR_USE_IF_NEEDED or SQL_CUR_USE_DRIVER. The ODBC Cursor Library version 3.00.2822 resolves this issue.
  • LincLinc Owner Detroit Icrontian
    edited December 2007
    Grimm, on the server that the client is using, you may want to turn off error reporting or have it log errors to a file rather than printing them to the screen. That way the user won't get confused if something minor goes wrong.

    Edit the php.ini file, looking for these values:

    display_errors = Off
    display_startup_errors = Off
    log_errors = On
    log_errors_max_len = 1024
    error_log = /path/to/error/log

    If you can't edit the php.ini file, you can also turn off errors at the start of the script using ini_set()
  • edited December 2007
    Kwitko wrote:
    I have some code to check for file extensions:
    [php]$allowedext = new array('jpg', 'gif', 'png');
    $ext = substr($_FILES, strrpos($_FILES, '.') + 1);
    if (!in_array($ext, $allowedext)) {
    unlink($_FILES);
    exit("File type $ext is not allowed.");
    }
    [/php]You would throw this in before the part that encodes the files to base64.

    Has anyone ever had success with getting this to work? The original code Kwitko gave for sending attachments works like a charm, but when I try to add this snippet to check extensions, all I get is a blank screen. If I take 'new' out of 'new array' I get my form back but it blocks all attachments.

    Can anyone provide instructions as to where exactly this should be placed? Or another code to check against specified extenstions?
Sign In or Register to comment.