سؤال

I am trying to get a PHP form working (I have a linux server, if that makes a difference), I found the following code on this website

http://www.excellentwebworld.com/send-file-in-email-attachment-on-form-submit-using-php/

but for some reason it is returning as the message "Error in Sending Email", if I attach a file or not.

I am far from confident with PHP and it's probably (hopefully) a simple mistake, but for the life of me I can't see anything that looks out of place...

Can someone shed any light on this...?

SAMPLE HTML CODE

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Email Attachment Without Upload - Excellent Web World</title>
<style>
body{ font-family:Arial, Helvetica, sans-serif; font-size:13px;}
th{ background:#999999; text-align:right; vertical-align:top;}
input{ width:181px;}
</style>
</head>
<body>
    <form action="ATTACH.php" method="post" name="mainform" enctype="multipart/form-data">
    <table width="500" border="0" cellpadding="5" cellspacing="5">
       <tr>
        <th>Your Name</th>
        <td><input name="fieldFormName" type="text"></td>
    </tr>
    <tr>
    <tr>
        <th>Your Email</th>
        <td><input name="fieldFormEmail" type="text"></td>
    </tr>

    <tr>
        <th>Subject</th>
        <td><input name="fieldSubject" type="text" id="fieldSubject"></td>
    </tr>
    <tr>
        <th>Comments</th>
        <td><textarea name="fieldDescription" cols="20" rows="4" id="fieldDescription"></textarea></td>
    </tr>
    <tr>
      <th>Attach Your File</th>
      <td><input name="attachment" type="file"></td>
    </tr>
    <tr>
        <td colspan="2" style="text-align:center;"><input type="submit" name="Submit" value="Send"><input type="reset" name="Reset" value="Reset"></td>
    </tr>
    </table>
    </form>
</body>
</html>

SAMPLE PHP CODE

<?php
// Email address to which you want to send email
$to = "myemail@hotmail.com";

$subject = $_POST["fieldSubject"];
$message = nl2br($_POST["fieldDescription"]);

/*********Creating Uniqid Session*******/

$txtSid = md5(uniqid(time()));

$headers = "";
$headers .= "From: ".$_POST["fieldFormName"]."<".$_POST["fieldFormEmail"].">\nReply-To: ".$_POST["fieldFormEmail"]."";

$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$txtSid."\"\n\n";
$headers .= "This is a multi-part message in MIME format.\n";

$headers .= "--".$txtSid."\n";
$headers .= "Content-type: text/html; charset=utf-8\n";
$headers .= "Content-Transfer-Encoding: 7bit\n\n";
$headers .= $message."\n\n";

/***********Email Attachment************/
if($_FILES["attachment"]["name"] != "")
{
$txtFilesName = $_FILES["attachment"]["name"];
$txtContent = chunk_split(base64_encode(file_get_contents($_FILES["attachment"]["tmp_name"])));
$headers .= "--".$txtSid."\n";
$headers .= "Content-Type: application/octet-stream; name=\"".$txtFilesName."\"\n";
$headers .= "Content-Transfer-Encoding: base64\n";
$headers .= "Content-Disposition: attachment; filename=\"".$txtFilesName."\"\n\n";
$headers .= $txtContent."\n\n";
}

// @ is for skiping Errors //
$flgSend = @mail($to,$subject,null,$headers);

if($flgSend)
{
echo "Email Sent SuccessFully.";
}
else
{
echo "Error in Sending Email.";
}
?>
هل كانت مفيدة؟

المحلول 2

Well the above answer didn't work for me, but I have found a solution which works a treat, so now I have a fully functioning online form with upload facility - have a look here

http://www.poipleshadow.com/C-Form-Goa-India

here is the PHP code for the same, i have made this on a bit more basic and commented it a little more.

<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){

   // Set the "To" email address
   $to="myemail@hotmail.com";

   // Get the sender's name and email address plug them a variable to be used later
   $from = stripslashes($_POST['myname'])."<".stripslashes($_POST['myemail']).">";

    // Check for empty fields - make sure all files on form are filled in
   if( empty($_POST['myname'])  || 
       empty($_POST['myemail']) || 
       empty($_POST['mytitle']) || 
       empty($_POST['mymessage'])  
    {
        $errors .= "\n Error: all fields are required";
    }

    // Get all the values from input form and set up variable names for each
    $myname = $_POST['myname'];
    $myemailaddress = $_POST['myemail'];
    $mytitle = $_POST['mytitle'];
    $mymessage = $_POST['mymessage'];

    //Subject of the mail - I add in supplied name and title into subject
    $subject="C-Form Application : $myname - $mytitle";


   // Now Generate a random string to be used as the boundary marker
   $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";

   // Now Store the file information to a variables for easier access
   $tmp_name = $_FILES['image_file']['tmp_name'];
   $type = $_FILES['image_file']['type'];
   $file_name = $_FILES['image_file']['name'];
   $size = $_FILES['image_file']['size'];

   // Now here we setting up the message of the mail
   $message = "Message Details
    \n Name          : $myname
    \n Email Address : $myemailaddress
    \n Email Title   : $mytitle
    \n Email Message : $mymessage";

   // Check 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)){

         // Now Open the file for a binary read
         $file = fopen($tmp_name,'rb');

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

         // close the file
         fclose($file);

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

      // Now we'll build the message headers
      $headers = "From: $from\r\n" .
         "MIME-Version: 1.0\r\n" .
         "Content-Type: multipart/mixed;\r\n" .
         " boundary=\"{$mime_boundary}\"";

      // Next, we'll build the message body note that we insert two dashes in front of the  MIME boundary when we use it
      $message = "This is a multi-part message in MIME format.\n\n" .
         "--{$mime_boundary}\n" .
         "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
         "Content-Transfer-Encoding: 7bit\n\n" .
         $message . "\n\n";

      // Now we'll insert a boundary to indicate we're starting the attachment we have to specify the content type, file name, and disposition as an attachment, then add the file content and set another boundary to indicate that the end of the file has been reached
      $message .= "--{$mime_boundary}\n" .
         "Content-Type: {$type};\n" .
         " name=\"{$file_name}\"\n" .
         //"Content-Disposition: attachment;\n" .
         //" filename=\"{$fileatt_name}\"\n" .
         "Content-Transfer-Encoding: base64\n\n" .
         $data . "\n\n" .
         "--{$mime_boundary}--\n";

      // Thats all.. Now we need to send this mail... - if it works it will open a html page called Thank-You.htm otherwise a page Error-Message.htm :)
      if (@mail($to, $subject, $message, $headers))
      {
         header('Location: Thank-You.htm');
      }else
      {
         header('Location: Error-Message.htm');
      }

   }
}
?>

there is a little bit of Javascript to go with the solution also, included on this link which i used as the basis to the solution :

http://www.script-tutorials.com/pure-html5-file-upload/

although instead of the 'upload' i converted it into an emailable form.

Hope this helps!!!

نصائح أخرى

To me it seems as if you haven't checked to see if the form is set.

Try doing the following so that if the button is clicked, the form is attached:

 <?php
if(isset($_POST['Submit']))
    {
// Email address to which you want to send email
$to = "myemail@hotmail.com";

$subject = $_POST["fieldSubject"];
$message = nl2br($_POST["fieldDescription"]);

/*********Creating Uniqid Session*******/

$txtSid = md5(uniqid(time()));

$headers = "";
$headers .= "From: ".$_POST["fieldFormName"]."<".$_POST["fieldFormEmail"].">\nReply-To:   ".$_POST["fieldFormEmail"]."";

$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$txtSid."\"\n\n";
$headers .= "This is a multi-part message in MIME format.\n";

$headers .= "--".$txtSid."\n";
$headers .= "Content-type: text/html; charset=utf-8\n";
$headers .= "Content-Transfer-Encoding: 7bit\n\n";
$headers .= $message."\n\n";

/***********Email Attachment************/
if($_FILES["attachment"]["name"] != "")
    {
    $txtFilesName = $_FILES["attachment"]["name"];
    $txtContent = chunk_split(base64_encode(file_get_contents($_FILES["attachment"]["tmp_name"])));
    $headers .= "--".$txtSid."\n";
    $headers .= "Content-Type: application/octet-stream; name=\"".$txtFilesName."\"\n";
    $headers .= "Content-Transfer-Encoding: base64\n";
    $headers .= "Content-Disposition: attachment; filename=\"".$txtFilesName."\"\n\n";
    $headers .= $txtContent."\n\n";
    }

// @ is for skiping Errors //
$flgSend = @mail($to,$subject,null,$headers);

if($flgSend)
    {
    echo "Email Sent SuccessFully.";
    }
else
    {
    echo "Error in Sending Email.";
    }

}
?>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top