0

I'm working on a form-mailer that includes a file upload but the file doesn't show up in the test E-mail at all.I've tweaked and re-tweaked but to no avail. Any suggestions?

The HTMl part of the form is as shown below

<div class="contactleft">


        <form action="former.php" method="post" enctype="multipart/form-data" id="former">

        <div class="textbox"><span id="sprytextfield1">
          <label for="fname"></label>
          <input name="fname" type="text" class="inputer" id="fname" />
          <span class="textfieldRequiredMsg">A value is required.</span></span></div><!-- End TectBox -->
        <div class="textbox"><span id="sprytextfield2">
          <label for="lname"></label>
          <input name="lname" type="text" class="inputer" id="lname" />
          <span class="textfieldRequiredMsg">A value is required.</span></span></div><!-- End TectBox -->
        <div class="textbox"><span id="sprytextfield3">
          <label for="email"></label>
          <input name="email" type="text" class="inputer" id="email" />
          <span class="textfieldRequiredMsg">A value is required.</span></span></div><!-- End TectBox -->
        <div class="textbox"><span id="sprytextfield4">
          <label for="file"></label>
          <label for="file"></label>
          <label for="fileField"></label>
          <input type="file" name="fileField" id="fileField" />
          <span class="textfieldRequiredMsg">A value is required.</span></span></div><!-- End TectBox -->


        <div class="textbox">
          <label for="sender"></label>
          <input type="submit" name="sender" id="sender" value="click to send message" />
        </div><!-- End TextBox -->

          </form>

     </div>

and the mailer is as shown below

   <?
   $mailto = '[email protected]'; // insert the email address you want the form sent to
    //$returnpage = 'thanks.php'; // insert the name of the page/location you want the user to be returned to//
    $sitename = '[siteripe.com]'; // insert the site name here, it will appear in the subject of your email

/* Do not edit below this line unless you know what you're doing */

  $fname = $_POST['fname'];
  $lname = $_POST['lname'];
  $email = $_POST['email'] ;
   $file = $_POST['file'];
   $subject = $_POST['subject'];


    if (!eregi("^[a-z0-9]+([-_\.]?[a-z0-9])+@[a-z0-9]+([-_\.]?[a-z0-9])+\.[a-z]{2,4}", $email)){
    print("<strong>Error:</strong> this email address is not in a valid format.<br/><br/><a href='javascript:history.go(-1)'>Back</a>");
         exit;
    }   

  $message = "\n$name submitted the following message:\n\n$message\n\n$name's contact details are as follows:\n\nFirst Name: $fname\nLast Name: $lname\nEmail Address: $email\nForm: $file";

  mail($mailto, "$subject", $message, "From: $email");

?> 
2
  • mail function returns true on success try $var = mail.. and see the the response Commented Mar 30, 2012 at 0:42
  • Well, $_POST['file'] will always fail, since your file upload input is named fileField. Try $_POST['fileField'], if you're after the file name. Commented Mar 30, 2012 at 0:44

4 Answers 4

1

You can't just pass $file as part of the $message without adding some additional headers indicating that the email is a 'multi-part' email, with $file as an attachment.

If you want to use vanilla PHP, take a look at the docs for the mail() function, there are some comments which show you how to do what you are asking.

If you are willing to look at a 3rd party library, I would suggest Zend Framework, they have a Zend_Mail_Attachment class which provides a clean interface for sending an email with an attachment.

Sign up to request clarification or add additional context in comments.

Comments

0

Try changing:

$file = $_POST['file'];

to:

$file = $_POST['fileField'];

Comments

0

This should work:

    <html>
    <head>
        <title>Test</title>
    </head>
    <body>
<?
if(count($_POST) > 0){
   $mailto = '[email protected]'; // insert the email address you want the form sent to
    //$returnpage = 'thanks.php'; // insert the name of the page/location you want the user to be returned to//
    $sitename = '[siteripe.com]'; // insert the site name here, it will appear in the subject of your email

/* Do not edit below this line unless you know what you're doing */

  $fname = $_POST['fname'];
  $lname = $_POST['lname'];
  $name = $lname . ', ' . $fname;
  $email = $_POST['email'] ;
   $subject = (array_key_exists('subject', $_POST))?$_POST['subject']:'Default subject';


    if (!@eregi("^[a-z0-9]+([-_\.]?[a-z0-9])+@[a-z0-9]+([-_\.]?[a-z0-9])+\.[a-z]{2,4}", $email)){
    print("<strong>Error:</strong> this email address is not in a valid format.<br/><br/><a href='javascript:history.go(-1)'>Back</a>");
         exit;
    }   

  $message = "\n$name submitted the following message:\n\n...\n\n$name's contact details are as follows:\n\nFirst Name: $fname\nLast Name: $lname\nEmail Address: $email";


    $rand = md5(time()); 
    $mime_boundary = '==Multipart_Boundary_x' . $rand . 'x'; 

    if(array_key_exists('fileField', $_FILES)){
        if(is_file($_FILES['fileField']['tmp_name'])){
            $message .= "--{$mime_boundary}\n";
            $fp =    @fopen($_FILES['fileField']['tmp_name'],"rb");
        $data =    @fread($fp,filesize($_FILES['fileField']['tmp_name']));
                    @fclose($fp);
            $data = chunk_split(base64_encode($data));
            $message .= "Content-Type: application/octet-stream; name=\"".$_FILES['fileField']['tmp_name']."\"\n" . 
            "Content-Description: ".$_FILES['fileField']['name']."\n" .
            "Content-Disposition: attachment;\n" . " filename=\"".$_FILES['fileField']['name']."\"; size=".filesize($_FILES['fileField']['tmp_name']).";\n" . 
            "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
            }
        }
    $message .= "--{$mime_boundary}--";

  $headers = 'From: ' . $email;
  mail($mailto, "$subject", $message, $headers);
}
?> 
<div class="contactleft">
        <form action="" method="post" enctype="multipart/form-data" id="former">
        <div class="textbox"><span id="sprytextfield1">
          <label for="fname"></label>
          <input name="fname" type="text" class="inputer" id="fname" />
          <span class="textfieldRequiredMsg">A value is required.</span></span></div><!-- End TectBox -->
        <div class="textbox"><span id="sprytextfield2">
          <label for="lname"></label>
          <input name="lname" type="text" class="inputer" id="lname" />
          <span class="textfieldRequiredMsg">A value is required.</span></span></div><!-- End TectBox -->
        <div class="textbox"><span id="sprytextfield3">
          <label for="email"></label>
          <input name="email" type="text" class="inputer" id="email" />
          <span class="textfieldRequiredMsg">A value is required.</span></span></div><!-- End TectBox -->
        <div class="textbox"><span id="sprytextfield4">
          <label for="file"></label>
          <label for="file"></label>
          <label for="fileField"></label>
          <input type="file" name="fileField" id="fileField" />
          <span class="textfieldRequiredMsg">A value is required.</span></span></div><!-- End TectBox -->
        <div class="textbox">
          <label for="sender"></label>
          <input type="submit" name="sender" id="sender" value="click to send message" />
        </div><!-- End TextBox -->
          </form>
     </div>
    </body>
</html>

1 Comment

Some changes in your code was made because there were several E_NOTICE errors ...
0

Also I'm seeing you make the mail() function call but $file isn't in it. I personally use the phpmailer class, then you declare a mail object then add components to it.

// SEND THANK YOU EMAIL
include('class.phpmailer.php');
$mail = new PHPMailer();
// Sender is the Reply-Path address; seems important
$mail->Sender  = "[email protected]";
$mail->From     = "[email protected]";
$mail->FromName = "Ca Cycleworks automated e-mail";
// $mail->AddAddress() is the To:
$blahtext=stripslashes($_SESSION['address_info']['b_first_name'])." ".stripslashes($_SESSION['address_info']['b_last_name']);
$mail->AddAddress($_SESSION['address_info']['b_email'],$blahtext );
$mail->AddReplyTo("[email protected]", "Ca Cycleworks Orders");
$subject = "Your Ca-Cycleworks.com Order # ".$_SESSION['order_number']." confirmation";
$mail->Subject = $subject;
$mail->AltBody = $text_body;
//  ISO 8859-1 summary: http://www.btinternet.com/~andrew.murphy/html_character_set.html
//  
//  $Encoding
//     PHPMailer::$Encoding in class.phpmailer.php
//     Sets the Encoding of the message. Options for this are "8bit", "7bit", "binary", "base64", and "quoted-printable".
//  string  $ContentType  =  "text/plain" (line 42)
// False Sets ContentType = "text/html" or "text/plain"
//$mail->IsHTML(false);
$mail->Body    = $body;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Username = "[email protected]";
$mail->Password = "xxxxxxxxxxxx";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.