表单文件输入中的PHP文件附件

时间:2018-08-11 08:09:07

标签: php html forms

我的表格确实发送了除文件以外的所有信息/如何解决?

这是输入文件:

<input type="file" name="file" placeholder="ЗАГРУЗИТЬ ЧЕК" id="file_kd" required>
<br></p>

这是代码php邮件:

<?php
header('Refresh: 0; URL=http://yougotit.agency/kodabra/thank-you.php'); // 
переадресация на страницу спасибо
$to = "stanislav.mandrik@gmail.com"; // емайл получателя данных из формы 
$tema = "Kodabra - заявка успешно отправлена!"; // тема полученного емайла 
$from = "Kodabra <no-reply@kodabra.com>";
$photo = $_FILES['file']['name'];
$message = "Ваше имя: ".$_POST['kdname']."<br>";
$message .= "E-mail: ".$_POST['kdemail']."<br>"; 
$message .= "Номер телефона: ".$_POST['kdphone']."<br>";
$message .= "Артикул модели (указан на упаковке): ".$_POST['kdartic']."<br>"; 
$message .= "Номер чека: ".$_POST['kdbill']."<br>";  
$message .= "Комментарий: ".$_POST['kdcomment']."<br>"; 
$message .= "Согласился на обработку персональных данных. ".$_POST['kdagree']."<br>"; 
$message .= "Фото чека: ".($photo)."\n";
$headers = "MIME-Version: 1.0"."\r\n".
           "Content-type: text/html; charset=\"utf-8\""."\r\n".
           "From: $from"."\r\n";

mail($to, $tema, $message, $headers);
?>

2 个答案:

答案 0 :(得分:0)

从我标记为重复的答案中,请不要投票

要发送带有附件的电子邮件,我们需要使用multipart / mixed MIME类型,该类型指定电子邮件中将包含混合类型。此外,我们想使用分段/替代MIME类型来发送电子邮件的纯文本和HTML版本,请看示例:

    <?php 
    //define the receiver of the email 
    $to = 'youraddress@example.com'; 
    //define the subject of the email 
    $subject = 'Test email with attachment'; 
    //create a boundary string. It must be unique 
    //so we use the MD5 algorithm to generate a random hash 
    $random_hash = md5(date('r', time())); 
    //define the headers we want passed. Note that they are separated with \r\n 
    $headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com"; 
    //add boundary string and mime type specification 
    $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
    //read the atachment file contents into a string,
    //encode it with MIME base64,
    //and split it into smaller chunks
    $attachment = chunk_split(base64_encode(file_get_contents('attachment.zip'))); 
    //define the body of the message. 
    ob_start(); //Turn on output buffering 
    ?> 
    --PHP-mixed-<?php echo $random_hash; ?>  
    Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 

    --PHP-alt-<?php echo $random_hash; ?>  
    Content-Type: text/plain; charset="iso-8859-1" 
    Content-Transfer-Encoding: 7bit

    Hello World!!! 
    This is simple text email message. 

    --PHP-alt-<?php echo $random_hash; ?>  
    Content-Type: text/html; charset="iso-8859-1" 
    Content-Transfer-Encoding: 7bit

    <h2>Hello World!</h2> 
    <p>This is something with <b>HTML</b> formatting.</p> 

    --PHP-alt-<?php echo $random_hash; ?>-- 

    --PHP-mixed-<?php echo $random_hash; ?>  
    Content-Type: application/zip; name="attachment.zip"  
    Content-Transfer-Encoding: base64  
    Content-Disposition: attachment  

    <?php echo $attachment; ?> 
    --PHP-mixed-<?php echo $random_hash; ?>-- 

    <?php 
    //copy current buffer contents into $message variable and delete current output buffer 
    $message = ob_get_clean(); 
    //send the email 
    $mail_sent = @mail( $to, $subject, $message, $headers ); 
    //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
    echo $mail_sent ? "Mail sent" : "Mail failed"; 
    ?>

如您所见,发送带有附件的电子邮件很容易完成。在前面的示例中,我们具有multipart / mixed MIME类型,在其中,我们具有multipart / alternative MIME类型,用于指定电子邮件的两个版本。为了包含消息的附件,我们将指定文件中的数据读取为字符串,并使用base64对其进行编码,将其拆分成较小的块以确保其与MIME规范匹配,然后将其作为附件包含。

来自here

答案 1 :(得分:0)

要发送带有文件附件的电子邮件,您必须将邮件拆分为具有不同编码的多个部分-这是我的操作方法。请注意,这里有一个用于纯文本信息的部分,然后它通过读取字节流并重新编码为base64格式并将其添加到消息正文中,指定内容类型,其文件名,来更改内容编码并添加所有附件。等

当然,所有这些都假定您已设置了正常工作的邮件,并且mail()的纯文本消息可以正常工作...

<?php
$headers  = "MIME-Version: 1.0\r\n";
$headers .= "From: $from_name <$from_address>\r\n";
$headers .= "Date: " . date("Ymd H:i:s") . "\r\n";
$headers .= "Reply-To: $from_name <$from_address>\r\n";            
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: High\r\n";
$headers .= "X-Mailer: ".$_SERVER['PHP_SELF']. "?id=". $_SERVER['UNIQUE_ID']. "\r\n";

$separator=md5(time());
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"\r\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n";
$headers .= "This is a MIME encoded message.\r\n";
// text message as normal for f2m
$messageBody="--".$separator."\r\n";
$messageBody.="Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$messageBody.="Content-Transfer-Encoding: 8bit\r\n";
$messageBody.="\r\n".$message_body."\r\n\r\n";
// add attachments
// $attachments is basically the $_FILES array
for($i=0;$i<count($attachments);$i++){
    $attachcontent=chunk_split(base64_encode(file_get_contents($attachments[$i]['tmp_name'])));
    $messageBody.="--".$separator."\r\n";
    $messageBody.="Content-Type: application/octet-stream; name=\"".$attachments[$i]['name']."\"\r\n";
    $messageBody.="Content-Transfer-Encoding: base64\r\n";
    $messageBody.="Content-Disposition: attachment; filename=\"".$attachments[$i]['name']."\"\r\n";
    $messageBody.="\r\n".$attachcontent."\r\n";
}
$messageBody.="--" . $separator . "--";
mail($to_address, $subject, $messageBody, $headers);
?>