Jump to content

Upload attachment and email


stary_eyes47

Recommended Posts

I was wondering if anyone could help me.  I am have made a very simple html form that uses php to email the data collected.  What I am trying to do is have an upload attachment in the form, and have this also sent in the email.  I have tried a few things, but nothing seems to work.  This is waht I have so far:

 

In my html form:

 

<td colspan="2"><p class="style3">Please fill out the form and we will endeavour to get back to you as soon as possible. <br />

          <span class="style7">.</span></p>          </td>

      </tr>

      <tr>

        <td colspan="2"><table width="400" border="0" align="center" cellpadding="0" cellspacing="1">

          <tr>

            <td>

<?php include("send_contact_MAW_001.php"); ?>

<?

switch ($action) {

  case "send":

    sendMail();

    showForm();

    break;

  default:

    showForm();

}

?>

 </td>

          </tr>

        </table></td>

 

 

and in my php handeller:

 

<table width="397" border="0">

          <tr>

            <td width="127"><span class="style3">

              <input type="hidden" name="action" value="send" />

              <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />

              <label>Name</label>

            </span></td>

            <td width="10"><span class="style3">

              <label>:</label>

            </span></td>

            <td width="246"><input name="Name" class="text " id="Name" size="50" /></td>

          </tr>

          <tr>

            <td><span class="style3">Email</span></td>

            <td><span class="style3">

              <label>:</label>

            </span></td>

            <td><input name="Email" class="text " id="Email" size="50" /></td>

          </tr>

          <tr>

            <td><span class="style3">Phone</span></td>

            <td><span class="style3">:</span></td>

            <td><input name="Phone" class="text " id="Phone" size="50" /></td>

          </tr>

          <tr>

            <td><span class="style3">Subject</span></td>

            <td><span class="style3">

              <label>:</label>

            </span></td>

            <td><input name="Subject" class="text " id="Subject" size="50" /></td>

          </tr>

          <tr>

            <td><span class="style3">Industry</span></td>

            <td><span class="style3">:</span></td>

            <td><input name="Industry" class="text " id="Industry" size="50" /></td>

          </tr>

          <tr>

            <td><span class="style3">Attachment</span></td>

            <td><span class="style3">:</span></td>

            <td><input name="Attachment" type="file" class="text " id="Attachment" size="50" /></td>

          </tr>

          <tr>

            <td valign="top"><span class="style3">Enquiry</span></td>

            <td valign="top"><span class="style3">:</span></td>

            <td><span class="style3">

              <textarea name="Enquiry" cols="50" rows="10" id="Enquiry"></textarea>

              </span>

                <p class="style3"></p></td>

          </tr>

          <tr>

            <td valign="top"><span class="style3"></span></td>

            <td valign="top"><span class="style3"></span></td>

            <td><span class="style3">

              <input name="submit" type="submit" class="button2" value="Submit" />

              <?

}

 

function sendMail() {

  if (!isset ($_POST['from_email'])) { //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 {

    $Name = stripslashes($_POST['Name']);

    $Subject = stripslashes($_POST['Subject']);

$Phone = stripslashes($_POST['Phone']);

$Industry = stripslashes($_POST['Industry']);

    $Enquiry = stripslashes($_POST['Enquiry']);

    $Email = $_POST['Email'];

    $Attachment = $_FILES['Attachment']['tmp_name'];

    $attachment_name = $_FILES['attachment']['name'];

    if (is_uploaded_file($attachment)) { //Do we have a file uploaded?

      $fp = fopen($attachment, "rb"); //Open it

      $data = fread($fp, filesize($attachment)); //Read it

      $data = chunk_split(base64_encode($data)); //Chunk it up and encode it as base64 so it can emailed

        fclose($fp);

    }

    //Let's start our headers

    $headers = "From: $Name<" . $_POST['Email'] . ">\n";

    $headers .= "Reply-To: <" . $_POST['Email'] . ">\n";

    $headers .= "MIME-Version: 1.0\n";

    $headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n";

    $headers .= "X-Sender: $Name<" . $_POST['Email'] . ">\n";

    $headers .= "X-Mailer: PHP4\n";

    $headers .= "X-Priority: 3\n"; //1 = Urgent, 3 = Normal

    $headers .= "Return-Path: <" . $_POST['Email'] . ">\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/plain; 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 .= "$Enquiry\n";

    $message .= "\n";

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

    $message .= "\n";

    $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 .= $data; //The base64 encoded message

    $message .= "\n";

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

 

    // send the message

    mail("test@test.com.au", $subject, $message, $headers);

  }

}

 

print <<< EOT

<?xml version="1.0" encoding="iso-8859-1"?>

              EOT;

              ?></span></td>

          </tr>

        </table>

 

 

 

Any help would be greatly appreciated

Link to comment
Share on other sites

This is the entire form.html file that I have.  It just doesn't work.

 

<html>

<form enctype="multipart/form-data" method="POST">

   

 

<td colspan="2"><p class="style3">Please fill out the form and we will endeavour to get back to you as soon as possible.

 

          <span class="style7">.</span></p>          </td>

      </tr>

      <tr>

        <td colspan="2"><table width="400" border="0" align="center" cellpadding="0" cellspacing="1">

          <tr>

            <td>

        <?php include("send_contact_MAW_001.php"); ?> 

        <?

switch ($action) {

  case "send":

    sendMail();

    showForm();

    break;

  default:

    showForm();

}

?>

               </td>

          </tr>

        </table></td>

</form>

</html>

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.