带附件的PHP Webform

时间:2014-02-04 16:03:49

标签: php forms email-attachments

我正在尝试使用PHP表单(我有一个Linux服务器,如果这有所不同),我在这个网站上找到了以下代码

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

但由于某种原因,如果我附加了某个文件,它将作为消息“发送电子邮件中的错误”返回。

我对PHP很有信心,它可能(希望)是一个简单的错误,但对于我的生活,我看不到任何看起来不合适的东西......

有人可以对此有所了解吗?

示例HTML代码

<!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>

示例PHP代码

<?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 个答案:

答案 0 :(得分:0)

对我来说,好像你没有检查表格是否已设定。

尝试执行以下操作,以便在单击按钮时附加表单:

 <?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.";
    }

}
?>

答案 1 :(得分:0)

上面的答案对我来说不起作用,但我找到了一个有效的解决方案,所以现在我有一个功能齐全的在线表格,上传设施 - 看看这里

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

这里是同样的PHP代码,我已经把它做得更基础了,并且稍微评论了一下。

<?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');
      }

   }
}
?>

还有一些Javascript可以与解决方案一起使用,包含在此链接中,我用作解决方案的基础:

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

虽然不是“上传”,但我将其转换为可通过电子邮件发送的形式。

希望这有帮助!!!