动态表单,电子邮件附件仅发送标题

时间:2014-01-15 17:04:39

标签: php email-attachments contact-form

我正在尝试使用HTML,PHP和JS进行动态联系。

到目前为止我已经发送了电子邮件,但是标题会阻止内容,附件不会随电子邮件一起发送。

HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Order Form</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <link rel="stylesheet" type="text/css" href="css/default.css"/>
    <script type="text/javascript" src="js/script.js"></script> 
</head>
<body>    
    <form action="process.php" class="register" method="POST">
        <h1>Artwork Upload</h1>
        <fieldset class="row1">
            <legend>Order Information</legend>
            <p>
                <label>Order No * 
                </label>
                <input name="orderno" type="text" required="required"/>
                <label>Name</label>
                <input name="name" type="text" required="required" />
                <label>E mail</label>
                <input name="email" type="text" required="required" />

            </p>
            <div class="clear"></div>
        </fieldset>
        <fieldset class="row2">
            <legend>Graphics per Product</legend>
            <p> 
                <input type="button" value="Add more images" onClick="addRow('dataTable')" /> 
                <input type="button" value="Remove selected lines" onClick="deleteRow('dataTable')"  /> 
                <p>(All acions apply only to entries with check marked check boxes only.)</p>
            </p>
           <table id="dataTable" class="form" border="1">
              <tbody>
                <tr>
                  <p>
                    <td><input type="checkbox" required="required" name="chk[]" checked="checked" /></td>
                    <td>
                        <label>Product code (SKU)</label>
                        <input type="text" required="required" name="BX_SKU[]">
                     </td>
                     <td>
                        <label for="BX_age">Product Name</label>
                        <input type="text" required="required" name="BX_Item[]">
                     </td>
                     <td>
                        <label for="BX_gender">Upload</label>
                        <input name="attachment" type="file">
                     </td>
                     <td>
                        <label for="BX_birth">Comments</label>
                        <input type="textarea" name="BX_birth" required="required">
                     </td>
                        </p>
                </tr>
                </tbody>
            </table>
            <div class="clear"></div>
        </fieldset>
        <input class="submit" type="submit" value="Confirm &raquo;" />

        <div class="clear"></div>
    </form>

</body>

PHP

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>A3M Artwork Upload</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" type="text/css" href="css/default.css"/>
</head>
<body>    
<form action="" class="register">
<h1>Artwork Upload</h1>
<?php if(isset($_POST)==true && empty($_POST)==false): 
$chkbox = $_POST['chk'];
$bus = $_POST['bus'];
$day = $_POST['day'];
$month = $_POST['month'];
$mob = $_POST['mob'];
$type = $_POST['type'];
$from = $_POST['from'];
$to=$_POST['to'];
$root=$_POST['root'];
$BX_NAME=$_POST['BX_SKU'];
$BX_age=$_POST['BX_age'];           
$BX_gender=$_POST['BX_gender'];
$BX_birth=$_POST['BX_birth'];                           
// Email address to which you want to send email
$to = "lee@boxchilli.com";
$subject = "A3M User Upload";       
$message = "Name:";
$message = $_POST['name'];
$message = "Email:";
$message = $_POST['email'];
$message = "Order ID:";
$message = $_POST['orderno'];
$message = "Product Name:";
$message = $_POST['BX_Item[]'];
$message = "Comments:";
$message = $_POST['BX_birth]'];
/*********Creating Uniqid Session*******/
$txtSid = md5(uniqid(time()));
$headers = "";
$headers .= $_POST["name"]."<".$_POST["email"].">\nReply-To: ".$_POST["email"].""; 
$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,$message);  
if($flgSend)
{
echo "Email Sent SuccessFully.";
}
else
{
echo "Error in Sending Email.";
}
?>
<fieldset class="row1">
<legend>Order Information</legend>
<p>Thank you for your information. A3M Direct will be in touch</p>
<?php else: ?>
<fieldset class="row1">
<legend>Sorry</legend>
<p>Some things went wrong please try again.</p>
</fieldset>
<?php endif; ?>
<div class="clear"></div>
</form>
</body>
</html>

JS

function addRow(tableID) {
    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;
    if(rowCount < 1000){                            // limit the user from creating fields more than your limits
        var row = table.insertRow(rowCount);
        var colCount = table.rows[0].cells.length;
        for(var i=0; i<colCount; i++) {
            var newcell = row.insertCell(i);
            newcell.innerHTML = table.rows[0].cells[i].innerHTML;
        }
    }else{
         alert("Please enter 1000 at a time, or contact A3M Direct.");

    }
}

function deleteRow(tableID) {
    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;
    for(var i=0; i<rowCount; i++) {
        var row = table.rows[i];
        var chkbox = row.cells[0].childNodes[0];
        if(null != chkbox && true == chkbox.checked) {
            if(rowCount <= 1) {                         // limit the user from removing all the fields
                alert("Cannot Remove all information.");


            break;
            }
            table.deleteRow(i);
            rowCount--;
            i--;
        }
    }
}

我在测试时收到的电子邮件

    Lee<lee@boxchilli.com>
    Reply-To: lee@boxchilli.comMIME-Version: 1.0
    Content-Type: multipart/mixed; boundary="8cd09bc06c304411f8d45359ebd270e2"

    This is a multi-part message in MIME format.
    --8cd09bc06c304411f8d45359ebd270e2
    Content-type: text/html; charset=utf-8
    Content-Transfer-Encoding: 7bit

更新

我已经添加了以下答案中获得的代码,并通过电子邮件

获取
    lee<lee@boxchilli.com>
Reply-To: lee@boxchilli.comMIME-Version: 1.0
Content-Type: multipart/mixed; boundary="18a2609e079b32ac5c98c954421a15f6"

This is a multi-part message in MIME format.
--18a2609e079b32ac5c98c954421a15f6
Content-type: text/html; charset=utf-8
Content-Transfer-Encoding: 7bit



--18a2609e079b32ac5c98c954421a15f6
Content-Type: application/octet-stream; name="mjweb-new-year.jpg"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="mjweb-new-year.jpg"

/9j/4QAYRXhpZgAASUkqAAgAAAAAAAAAAAAAAP/sABFEdWNreQABAAQAAABQAAD/4QMtaHR0cDov
L25zLmFkb2JlLmNvbS94YXAvMS4wLwA8P3hwYWNrZXQgYmVnaW49Iu+7vyIgaWQ9Ilc1TTBNcENl
aGlIenJlU3pOVGN6a2M5ZCI/PiA8eDp4bXBtZXRhIHhtbG5zOng9ImFkb2JlOm5zOm1ldGEvIiB4
OnhtcHRrPSJBZG9iZSBYTVAgQ29yZSA1LjMtYzAxMSA2Ni4xNDU2NjEsIDIwMTIvMDIvMDYtMTQ6
NTY6MjcgICAgICAgICI+IDxyZGY6UkRGIHhtbG5zOnJkZj0iaHR0cDovL3d3dy53My5vcmcvMTk5
OS8wMi8yMi1yZGYtc3ludGF4LW5zIyI+IDxyZGY6RGVzY3JpcHRpb24gcmRmOmFib3V0PSIiIHht
bG5zOnhtcE1NPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvbW0vIiB4bWxuczpzdFJlZj0i
aHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL3NUeXBlL1Jlc291cmNlUmVmIyIgeG1sbnM6eG1w

1 个答案:

答案 0 :(得分:0)

将enctype属性添加到表单中(enctype =“multipart / form-data”)

http://www.w3schools.com/tags/att_form_enctype.asp