2013-07-30 30 views
0

我有一个.html表单,它需要填写数据和.pdf附件才能上传。 .pdf和数据需要通过电子邮件发送。无法使用PHP发送带有表单数据的pdf附件

代码我已经成功检查.pdf,但是当我收到电子邮件时,电子邮件来自我们的一个Apache邮件服务器而不是发件人,电子邮件为空..没有表单数据和附件被剥离并替换为空白.txt

有人可以看看我的代码,让我知道我在做什么错了吗?我没有看到它。

PHP代码:

<?php 

$fileInfo = new finfo(FILEINFO_MIME_TYPE); 
$fileMime = $fileInfo->file(@$_FILES["file"]["tmp_name"]); 
if($fileMime == "application/pdf") 
{ 
//Requestor 
$requestor=$_POST["$requestor"]; 

//Requestor Phone 
$telephone=$_POST["$telephone"]; 

//Requestor Email 
$mail_from=$_POST["$req_mail"]; 

//Document Subject 
$subject=$_POST["$subject"]; 

// Services Date 
$req_date=$_POST["req_date"]; 

//Addintional Info 
$info=$_POST["body"]; 

$email_to = "[email protected]"; // Who the email is too 

$headers = "From: ".$mail_from; 

$file = fopen($fileatt,'rb'); 
$data = fread($file,filesize($fileatt)); 
fclose($file); 

$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

    $headers .= "\nMIME-Version: 1.0\n" . 
"Content-Type: multipart/mixed;\n" . 
" boundary=\"{$mime_boundary}\""; 

$message .= "This is a multi-part message in MIME format.\n\n" . 
"--{$mime_boundary}\n" . 
"Content-Type:text/html; charset=\"iso-8859-1\"\n" . 
"Content-Transfer-Encoding: 7bit\n\n" . 
$message . "\n\n"; 

$data = chunk_split(base64_encode($data)); 

$message .= "--{$mime_boundary}\n" . 
"Content-Type: {$fileatt_type};\n" . 
" name=\"{$fileatt_name}\"\n" . 
//"Content-Disposition: attachment;\n" . 
//" filename=\"{$fileatt_name}\"\n" . 
"Content-Transfer-Encoding: base64\n\n" . 
$data . "\n\n" . 
"--{$mime_boundary}--\n"; 

$message .="Request From: ".$requestor."\n"."Contact Phone: ".$telephone."\n"."Contact Email: ".$mail_from."\n"."Project/Mission Name: ".$subject."\n"."Need by Date: ".$req_date."\n"."Details: ".$info; 

$ok = @mail($email_to, $subject, $message, $headers); 

if($ok) 
{ 
    header("Form & attachment sent successfully"); 
} 
    else 
    { 
    die("Sorry but the email could not be sent. Please go back and try again!"); 
    } 
} 
else 
{ 
header("Your attachment must be a .pdf"); 
}  
?> 

,这是我的.html:

<form action="sendreview.php" method="post" enctype="multipart/form-data"> 
<table width="99%" border="0" cellspacing="1" cellpadding="3"> 

<tr> 
    <td width="40%">Requestor Name</td> 
    <td width="2%">:</td> 
    <td width="58%"><span id="sprytextfield1"><input name="requestor" type="text" id="requestor" size="50" /><span class="textfieldRequiredMsg">A name is required.</span><span class="textfieldInvalidFormatMsg">A name is required.</span></span></td> 
</tr> 

<tr> 
    <td width="40%">Requestor Number</td> 
    <td width="2%">:</td> 
    <td width="58%"><span id="sprytextfield2"> 
    <input name="telephone" type="text" id="telephone" size="50" /> 
    <span class="textfieldRequiredMsg">A phone number is required.</span> 
    <span class="textfieldInvalidFormatMsg">Invalid format 000-000-0000.</span></span> </td> 
</tr> 

<tr> 
    <td width="40%">Requestor Email</td> 
    <td width="2%">:</td> 
    <td width="58%"><span id="sprytextfield3"> 
<input name="req_mail" type="text" id="req_mail" size="50" /> 
<span class="textfieldRequiredMsg">An email address is required.</span><span class="textfieldInvalidFormatMsg">A valid email address is required.</span></span> 
    </td> 
</tr> 

<tr> 
    <td width="40%">Project/Mission:</td> 
    <td width="2%">:</td> 
    <td width="58%"><span id="spryselect1"> 
    <select name="subject" id="subject"> 
    <optgroup label="TEST"> 
     <option value="1">1</option> 
     <option value="2">2</option> 
    </optgroup> 
    </select> 
    <span class="selectRequiredMsg">Please select an item.</span></span></td> 
</tr> 

<tr> 
    <td width="36%">Need by Date</td> 
    <td width="2%">:</td> 
    <td width="62%"> 
    <div id="OAWidget"></div> 
    <script type="text/javascript"> 
    // BeginOAWidget_Instance_2443523: #OAWidget 

    document.body.className += " yui-skin-sam"; 
    YAHOO.namespace("widget.Calendar"); 

    YAHOO.init_OAWidget = function() { 
    var oCalendar_OAWidget = new YAHOO.widget.Calendar("oCalendar_OAWidget","OAWidget", 
    {selected: "", 
    mindate: null, 
     maxdate: null, 
     title: "", 
    close: false, 
     iframe: true, 
     multi_select: false, 
     navigator: false, 
     show_weekdays: true, 
    locale_weekdays: "short", 
     start_weekday: 0, 
     show_week_header: false, 
     show_week_footer: false, 
     hide_blank_weeks: false } 
    ); 
    oCalendar_OAWidget.render(); 
    } 
    YAHOO.util.Event.onDOMReady(YAHOO.init_OAWidget); 

    // EndOAWidget_Instance_2443523 
     </script> 
</tr> 

<tr> 
    <td>Message:</td> 
    <td>:</td> 
    <td><span id="sprytextarea1"> 
    <textarea name="body" cols="50" rows="4" id="body"></textarea> 
    <span class="textareaRequiredMsg">A value is required.</span></span></td> 
</tr> 

<tr> 
    <td>Attach File</td> 
    <td>:</td> 
    <td><input type="file" name="file" id="file"> File type must be a PDF</td> 
</tr> 

<tr> 
    <td></td> 
    <td></td> 
    <td><input type="submit" name="Submit" value="Submit"> <input type="reset" name="Submit2" value="Reset"></td> 
</tr> 
</table> 
</form> 
+0

不要自己构建mime电子邮件。该邮件构建代码的ALl可以用4或5行swiftmailer或phpmailer替换。另外,你的'header()'调用是完全错误的... –

+0

谢谢,我回到使用SwiftMailer,并能够让所有的东西都能正常工作。 – Kim

回答

0

您没有正确地从$ _ POST获取值。

错误:

$requestor=$_POST["$requestor"]; 

右:

$requestor=$_POST["requestor"]; 

你也应该消毒和/或验证所有输入。

+0

谢谢,我注意到我发布后(D'oh!),我想回到使用SwiftMailer,并能够让所有的东西都起作用。 – Kim

相关问题