-1

我们目前有一个表单,当提交用户回复时,它会作为附件的 excel 文件通过电子邮件发送给我们。我们还有另一种形式,允许用户上传多个不同类型的文件,然后作为附件通过电子邮件发送给我们。但是,我们无法将两者合并为一个表单,以便我们可以接收包含两种类型附件的电子邮件。我们是初学者...

下图为: 1. 现有表单,附上单个excel内容 2. 现有mailer.php 3. 现有表单,上传多个文件

1.excel内容表格

<?php

error_reporting(E_ALL ^ E_NOTICE); 

ob_start();


$taxyear = $_POST['taxyear'];
$company = $_POST['company'];
$tfn = $_POST['tfn'];
$director1 = $_POST['director1'];
$director2 = $_POST['director2'];
$email = $_POST['email'];


?>

<html>
<body> 
<form>
<table width="720" border="0" align="center" bgcolor="#FFFFFF">

  <tr align="center" valign="top">
    <td height="1730">COMPANY RETURN
      <table width="702" border="1" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
        <tr>
          <td colspan="6" align="left" class="whiteonred" >Company <strong><? echo $taxyear; ?> </strong></td>
        </tr>
          <tr class="excel7" style="height:18.0pt;">
          <td colspan="4" align="left" class="centgothicstd" ><strong>PRINCIPAL DETAILS </strong></td>
          <td colspan="2" align="right" class="centgothicsm">&nbsp;</td>

        </tr>
          <tr style="height:18.0pt;">
            <td align="left" class="centgothicsm" >COMPANY NAME &amp; TFN</td>
            <td colspan="3" align="left" class="borders"><strong><? echo $company; ?></strong></td>
            <td colspan="2" align="left" class="borders"><strong><? echo $tfn; ?></strong></td>

          </tr>
          <tr style="height:18.0pt;">
            <td width="213" align="left" class="centgothicsm" style="height:18.0pt;">DIRECTOR NAMES</td>
            <td colspan="3" align="left" class="borders"><? echo $director1; ?></td>
            <td colspan="2" align="left" class="borders"><? echo $director2; ?></td>

          </tr>
        <tr style="height:18.0pt;">
          <td width="213" align="left" class="centgothicsm" style="height:18.0pt;">EMAIL ADDRESS</td>
          <td colspan="5" align="left" class="borders"><? echo $email; ?></td>

        </tr>

      </table></td>
  </tr>
</table>
</form>
</body>
</html>

<?php

$FILE_CONTENTS = ob_get_contents(); 

ob_clean(); 

include("mailer.php"); 

$recipient = "darren@eto.net.au"; 

$subject = "Company submission"; 

$myEmail = new EPDEV_Emailer($recipient, $email, $subject); 

$myEmail->addFile("{$company}-{$taxyear}.xls", "application/vnd.ms-excel", $FILE_CONTENTS); 

$myEmail->send(); 

Header("Location: thankyouapplic.htm");
?>

* 2. mailer.php 代码 *

<?php 

class EPDEV_Emailer 
{ 
    var $message; 
    var $FILES; 
    var $EMAIL; 

    function EPDEV_Emailer($to_address, $from_address, $subject, $reply_address=null, $mailer=null, $custom_header=null) 
    { 
        $this->EMAIL = array( 
            "to" => $to_address, 
            "from" => $from_address, 
            "subject" => $subject, 
            "reply" => (empty($reply_address) ? $from_address : $reply_address), 
            "mailer" => (empty($mailer) ? "X-Mailer: PHP/" . phpversion() : $mailer), 
            "header" => (empty($custom_header) ? "" : $custom_header), 
            "boundary" => "_mimeboundary_".md5(uniqid(mt_rand(), 1)) 
            ); 

        $this->message = ""; 

        $this->FILES = array(); 
    } 

    function addFile($filename, $type=null, $filecontents=null) 
    { 
        if ($filecontents !== null) 
        { 
            $index = count($this->FILES); 
            $this->FILES[$index]['data'] = chunk_split(base64_encode($filecontents)); 
            $this->FILES[$index]['name'] = basename($filename); 

            if (empty($type)) 
                $this->FILES[$index]['mime'] = mime_content_type($filename); 
            else 
                $this->FILES[$index]['mime'] = $type; 
        } 
        else if (file_exists($filename)) 
        { 
            $index = count($this->FILES); 
            $this->FILES[$index]['data'] = chunk_split(base64_encode(file_get_contents($filename))); 
            $this->FILES[$index]['name'] = basename($filename); 

            if (empty($type)) 
                $this->FILES[$index]['mime'] = mime_content_type($filename); 
            else 
                $this->FILES[$index]['mime'] = $type; 
        } 
        else 
        { 
            $this->Error_Handle("File specified -- {$filename} -- does not exist."); 
        } 
    } 



    function addText($text) 
    { 
        $this->message .= $text; 
    } 


    function getHeader() 
    { 
        $header = "From: {$this->EMAIL['from']}\r\n" 
                . "Reply-To: {$this->EMAIL['reply']}\r\n" 
                . "X-Mailer: {$this->EMAIL['mailer']}\r\n" 
                . "MIME-Version: 1.0\r\n" 
                . "Content-Type: multipart/mixed; boundary=\"{$this->EMAIL['boundary']}\";\r\n"; 

        return $header; 
    } 

    function getEmail() 
    {     
        $content .= "--{$this->EMAIL['boundary']}\r\n" 
                . "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n" 
                . "Content-Transfer-Encoding: 7bit\r\n\r\n" 
                . $this->message . "\r\n"; 

        if (!empty($this->FILES)) 
        { 
            foreach($this->FILES as $file) 
            { 
                $content .= "--{$this->EMAIL['boundary']}\r\n" 
                . "Content-Type: {$file['mime']}; name=\"{$file['name']}\"\r\n" 
                . "Content-Transfer-Encoding: base64\r\n" 
                . "Content-Disposition: attachment\r\n\r\n" 
                . $file['data'] . "\r\n"; 
            } 
        } 

        $content .= "--{$this->EMAIL['boundary']}--\r\n"; 


        return $content; 
    } 

    function send() 
    { 
        $result = mail($this->EMAIL['to'], $this->EMAIL['subject'], $this->getEmail(), $this->getHeader()); 

        if (!$result) 
            $this->Error_Handle("The email failed to send."); 
    } 


    function Error_Handle($error) 
    { 
        die($error); 
    } 
}

3.上传表格

<body>
<td height="353" colspan="9" align="left" valign="top">

  <form enctype="multipart/form-data" name="send" method="post" action="<?=$_SERVER['PHP_SELF']?>">
    <input type="hidden" name="action" value="send" />
    <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
    <tr>
      <td width="300" align="center">&nbsp;</td>

      <table width="500" height="407" border="0" align="center">
        <tr>
          <td width="120" align="left" class="indextextlight">Name:</td>
          <td colspan="2"><input name="fname" type="text" class="indextextlight" size="30" /></td>
          </tr>
        <tr>
          <td width="120" height="24" align="left" class="indextextlight">E-mail:</td>
          <td colspan="2"><label>
            <input name="email" type="text" class="indextextlight" size="30" />
          </label></td>
          </tr>
        <tr>
          <td width="120" align="left" class="indextextlight">Telephone:</td>
          <td colspan="2"><label>
            <input name="tel" type="text" class="indextextlight" id="tel" value="" size="15" />
          </label></td>
          </tr>
        <tr>
          <td width="120" align="left" valign="top" class="indextextlight">Message:

            </td>
          <td colspan="2" valign="top"><textarea name="comments" " id="comments" cols="45" rows="4"></textarea></td>
          </tr>
        <tr>
          <td width="120" valign="middle" class="indextextlight">Upload Files:</td>
          <td colspan="2"><p>
            <input name="attachment[]" type="file" multiple="" class="indextextlight" size="42">
            <br />
            <span class="centgothicmini">              note - hold the Ctrl key to select multiple files</span><br />
            <span class="centgothicmini">              note - max total size of all files can not  exceed 10mb</span></td>
          </tr>
        <tr>
          <td width="120" align="left" class="indextextlight">Send:</td>
          <td colspan="2" align="left"><input type="image" name="submit" value="Send Email" src="images/btnSubmit.gif" /></td>
          </tr>
        <tr>
          <td width="120" class="indextextlight">Result:</td>
          <td colspan="2" class="header"><?php
/* Mailer with Attachments */

$action = $_REQUEST['action'];
global $action;

function showform(){
?></td>
          </tr>
        <tr>
          <td class="indextextlight">&nbsp;</td>
          <td colspan="2" class="header">&nbsp;</td>
        </tr>
        <tr><td colspan="3" align="right" valign="baseline" class="footer"><span class="footerheadings">&nbsp;</span> © 2009-13 BC Accountants Australia Pty Ltd</td>
        </tr>
        </table>

  </form>   
  <script type="text/javascript">
var frmvalidator  = new Validator("send");
frmvalidator.addValidation("fname","req","Please enter your Name");
frmvalidator.addValidation("email","maxlen=50");
frmvalidator.addValidation("email","req");
frmvalidator.addValidation("email","email");
frmvalidator.addValidation("tel","maxlen=15");
frmvalidator.addValidation("tel","numeric");

</script>      
  <?php
}

function sendMail() {
  if (!isset ($_POST['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 {

    $fname = stripslashes($_POST['fname']);
    $email = $_POST['email'];
    $phone = $_POST['tel'];
    $comments = stripslashes($_POST['comments']);
    $headers .= "\n";

    //Uniqid session
    $strSid = md5(uniqid(time()));

    //Let's start our headers
    $headers = "From: $fname<" . $_POST['email'] . ">\n";
    $headers .= "Reply-To: <" . $_POST['email'] . ">\n"; 
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";  
    $headers .= "This is a multi-part message in MIME format.\n";

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

    /* Add our message, in this case it's plain text.  You could also add HTML by changing the Content-Type to text/html */
    $headers .= "<table>";

    $headers .= "<tr><td>CONTACT FORM</td><td>&nbsp;</td></tr>";
    $headers .= "<tr><td>Check for attachments</td><td>&nbsp;</td>      </tr>";

    $headers .= "<tr>
    <td>&nbsp;</td>
    <td > </td>
    </tr>";

    $headers .= "<tr>
    <td >Name:  " . strip_tags($_POST["fname"]) . "</td><td >&nbsp;</td>
    </tr>";

    $headers .= "<tr>
    <td>Email:  " . strip_tags($_POST["email"]) . "</td><td >&nbsp;</td>
    </tr>";

    $headers .= "<tr>
    <td>Phone:  " . strip_tags($_POST["phone"]) . "</td><td>&nbsp;</td>
    </tr>";

    $headers .= "<tr>
    <td>&nbsp;</td>
    <td > </td>
    </tr>";

    $headers .= "<tr>
    <td>MESSAGE</td><td>&nbsp;</td></tr>";

    $headers .= "<tr> <td COLSPAN = '2'>" . strip_tags($_POST["comments"]) . "</td>
    </tr>"; 

    $headers .= "<tr>
    <td>&nbsp;</td>
    <td > </td>
    </tr>";

    $headers .= "</table>";
    $headers .= "</body></html>";
    $headers .= "\n"; 



    //**multi attach**//
    for($i=0;$i<count($_FILES["attachment"]["name"]);$i++)
    {
    if($_FILES["attachment"]["name"][$i] != "")
    {
        $file_name = $_FILES["attachment"]["name"][$i];
        $data = chunk_split(base64_encode(file_get_contents($_FILES["attachment"]["tmp_name"][$i])));
        $headers .= "--".$strSid."\n";
        $headers .= "Content-Type: application/octet-stream;\n\tname=\"" . $file_name . "\"\n";
        $headers .= "Content-Transfer-Encoding: base64\n";
        $headers .= "Content-Disposition: attachment;\n\tfilename=\"" . $file_name . "\"\n\n";
        $headers .= $data."\n\n"; //The base64 encoded message
    }

    }
    $headers .= "\n"; 
    $headers .= "------=MIME_BOUNDRY_main_message--\n"; 
    $subject .= "Contact Form";
    // send the message
    mail("darrenmillbca@gmail.com", $subject, $message, $headers); 
    print "Mail sent. Thank you!";
$to = $email; 

$subject = "Contact Form"; 

$message = '
<html>
<body>

<p>Dear '; 
$message .= $fname;
$message .= '</p>
<p>Thankyou for your message. </p>
<p>Our staff will get back to you shortly. </p>

<p>Kind Regards <br />
  <br />
  </p>

</body>
</html>
';

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To:' . "\r\n";
$headers .= 'From: darrenmillbca@gmail.com' . "\r\n";
//$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
//$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);



  }
}

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


?>

</html>
4

1 回答 1

1

你所要求的是一个相当大的变化,所以我会尽力让你们变得简单。您的 excel 内容表单使用电子邮件包装器(mailer.php),它将附加文件转换为仅 $myEmail->addFile("file name","mimetype","file content")。另一方面,上传表单没有使用这个包装器,而是单独生成一个完整的电子邮件。因此,您需要专注于将上传内容转移到使用包装器上;这是将内容从上传表单移动到 excel 表单。

因此,我们的重点是上传文件中的以下块:

for($i=0;$i<count($_FILES["attachment"]["name"]);$i++)
    {
    if($_FILES["attachment"]["name"][$i] != "")
    {
        $file_name = $_FILES["attachment"]["name"][$i];
        $data = chunk_split(base64_encode(file_get_contents($_FILES["attachment"]["tmp_name"][$i])));
        $headers .= "--".$strSid."\n";
        $headers .= "Content-Type: application/octet-stream;\n\tname=\"" . $file_name . "\"\n";
        $headers .= "Content-Transfer-Encoding: base64\n";
        $headers .= "Content-Disposition: attachment;\n\tfilename=\"" . $file_name . "\"\n\n";
        $headers .= $data."\n\n"; //The base64 encoded message
    }

这就是说'对于每个文件上传,将其写入我们正在建立的电子邮件中'。取而代之的是,我们想要“为每个文件上传,使用包装器附加它”。可以这样做:

for($i=0;$i<count($_FILES["attachment"]["name"]);$i++){
        if($_FILES["attachment"]["name"][$i] != "")
        {
            $file_name = $_FILES["attachment"]["name"][$i];
            $data = file_get_contents($_FILES["attachment"]["tmp_name"][$i]);
            $myEmail->addFile($file_name,"application/octet-stream",$data);
        }
}

为此,上面的代码将在现有的 $myEmail->addFile 调用之后立即进入您的 Excel 表单。换句话说,在您附加您的 Excel 表格后,立即查找任何附件,然后将它们也添加到电子邮件中:

$myEmail->addFile("{$company}-{$taxyear}.xls", "application/vnd.ms-excel", $FILE_CONTENTS); 
 // Drop it right here.
$myEmail->send();

到目前为止一切顺利 - 此时,您的 Excel 表单可以处理多个上传,但无法创建它们。为了解决这个问题,您需要开始传输允许您的用户将文件上传到您的 Excel 表单的 html。就是这部分:

<tr>
          <td width="120" valign="middle" class="indextextlight">Upload Files:</td>
          <td colspan="2"><p>
            <input name="attachment[]" type="file" multiple="" class="indextextlight" size="42">
            <br />
            <span class="centgothicmini">              note - hold the Ctrl key to select multiple files</span><br />
            <span class="centgothicmini">              note - max total size of all files can not  exceed 10mb</span></td>
          </tr>
        <tr>

您可以做的一件简单的事情是将其转储到适合您的 Excel 表单的任何位置,因此您的 Excel 表单应该接受多次上传。从那里,所需要的就是将您想要的任何其他字段从上传转移到 excel :)

于 2013-10-12T14:15:14.103 回答