带有附件的PHP邮件——附件以编码文本的形式发送,而不是以文件的形式发送

带有附件的PHP邮件——附件以编码文本的形式发送,而不是以文件的形式发送,php,Php,请任何人帮忙 我正在用php发送带有附件的电子邮件。已发送电子邮件,但我们未收到作为文件的附件,但它是以编码文本的形式发送的…有任何建议和帮助吗 这是我的php代码 <?php if(isset($_POST['submit'])) { ob_start(); if(isset($_FILES['upload_cv'])) { $file_tmp_name = $_FILES['upload_cv']['tmp_name'];

请任何人帮忙

我正在用php发送带有附件的电子邮件。已发送电子邮件,但我们未收到作为文件的附件,但它是以编码文本的形式发送的…有任何建议和帮助吗

这是我的php代码

<?php
if(isset($_POST['submit']))
{
        ob_start();

        if(isset($_FILES['upload_cv']))
    {
    $file_tmp_name    = $_FILES['upload_cv']['tmp_name'];
    $file_name        = $_FILES['upload_cv']['name'];
    $file_size        = $_FILES['upload_cv']['size'];
    $file_type        = $_FILES['upload_cv']['type'];
    $file_error       = $_FILES['upload_cv']['error'];
    //$user_email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
    if($file_error>0)
    {
        die('upload error');
    }
    //read from the uploaded file & base64_encode content for the mail
    $handle   = fopen($file_tmp_name, "r");
    $content  = fread($handle, $file_size);
    fclose($handle);
    $encoded_content = chunk_split(base64_encode(file_get_contents(($content))));
    }
    $boundary = md5("ajantasoya");
    $recipientEmail = "prg6@gmail.com";
    //$senderEmail = $_POST['email'];
    $senderEmail = "vipinmishra.alld@gmail.com";
    $Subject = "Test";
    $headers  = "MIME-Version: 1.0 \r\n";
    $headers .= "Content-Type: multipart/mixed; boundary=\"PHP-mixed-".$boundary."\"\r\n";
    $headers .= "Content-Transfer-Encoding: 7bit" . " \r\n";
    $headers .= "Content-type: text/plain; charset=iso-8859-1 \r\n";
    $headers .= "From: ".$senderEmail." \r\n";
    $headers .= "Reply-To: ".$senderEmail." \r\n";
    $headers .= "Subject:". $subject." \r\n";
    $headers .= "X-Mailer: PHP/".phpversion();
    $headers .= "--" . $boundary . " \r\n";
    $headers .= "Content-Type: text/HTML; charset=\"iso-8859-1\"" ." \r\n";
    $headers .= "Content-Transfer-Encoding: 8bit" . " \r\n";
    // Main message start
    $msg_body .= "--" . $boundary . " \r\n";
    $msg_body .= "Content-Type: text/HTML; charset=\"iso-8859-1\"" ." \r\n";
    $msg_body .= "Content-Transfer-Encoding: 8bit" . " \r\n";
    $msg_body  = "Name: ".$_POST['name']. "\r\n"; 
    $msg_body .= "Current Location: ".$_POST['location']. "\r\n"; 
    $msg_body .= "Home town : ".$_POST['home-town']. "\r\n"; 
    $msg_body .= "Marital Status: ".$_POST['mstatus']. "\r\n"; 
    $msg_body .= "Children, if any: ".$_POST['num-ch']. "\r\n"; 
    $msg_body .= "Position applying for : ".$_POST['pos-for']. "\r\n"; 
    $msg_body .= "Date of Birth : ".$_POST['dob']. "\r\n"; 
    $msg_body .= "Strengths: ".$_POST['strenght']. "\r\n"; 
    $msg_body .= "Weaknesses: ".$_POST['weakness']. "\r\n"; 

    //Attachment Part

    $msg_body .= "--".$boundary."\r\n";
    $msg_body .= "Content-Type: application/octet-stream; name=\"" . $file_name . "\r\n";
    $msg_body .= "Content-Transfer-Encoding: base64 \r\n";
    $msg_body .= "Content-Disposition: attachment \r\n";

    //$msg_body .="Content-Type: ".$file_type. ";  Name=".$file_name."\r\n";
    //$msg_body .="Content-Type: ".$file_type. ";  Name=".$file_name."\r\n";
    //$msg_body .="Content-Disposition: Attachment;       Filename=".$file_name."\r\n";
    //$msg_body .="Content-Transfer-Encoding: base64\r\n";
    $msg_body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
    $msg_body .= $encoded_content;

    $message = $msg_body;
    $send = @mail($recipientEmail, $Subject , $message, $headers);
    if ($send) {
        echo "Message sent!";
    }
    else {
        echo "ERROR sending message.";
    }

}
ob_end_flush();     
?>

在标题中,您有:

$headers .= "Content-Type: multipart/mixed; boundary=\"PHP-mixed-".$boundary."\"\r\n";
因此,消息和附件之间的边界应该是
--PHP mixed-$boundary
。但当您对消息正文进行编码时,您将发送:

$msg_body .= "--" . $boundary . " \r\n";
您缺少
$boundary
之前的
PHP mixed-
前缀。将这些行更改为:

$msg_body .= "--PHP-mixed-" . $boundary . " \r\n";
此外,您没有在附件标题的正确位置指定文件名。您在
内容类型:
行中有它,它应该在
内容配置:

$msg_body .= "Content-Type: application/octet-stream \r\n";
$msg_body .= "Content-Transfer-Encoding: base64 \r\n";
$msg_body .= "Content-Disposition: attachment; filename=\"$file_name\"  \r\n";

这大概就是它的样子。你可能缺少的部分**

           $eol = "\n";
           // main header (multipart mandatory)
           $headers = "From: $form". $eol;
           $headers .= "MIME-Version: 1.0" . $eol;
           **$headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"";**

           // message
            $body .= "--$boundary" . $eol;
            $body .= "Content-Transfer-Encoding: 7bit" . $eol.$eol;
            $body .= "This is a MIME encoded message." . $eol;
            $body .= "--".$boundary.$eol;
            $body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
            $body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
            $body .= $message.$eol;

            //attachment
            $body .= "--$boundary" . $eol;
            $body .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $eol;
            $body .= "Content-Transfer-Encoding: base64" . $eol;
            $body .= "Content-Disposition: attachment". $eol.$eol;
            $body .= $content . $eol;
            **$body .= "--$boundary--";**

            mail('email', 'subject', $body, $headers)

尝试先将文件保存到磁盘。然后从保存路径将其附加。文件名应位于
内容处置:
字段中,而不是
内容类型:
。它应该是
filename=
,而不是
name=
。谢谢。。。我在服务器上保存后尝试过,但仍然存在相同的问题…感谢您的回复…我尝试过,但仍然存在相同的问题这里是我的完整代码。请更正并发布…这应该是问题的答案还是问题的继续?如果它是问题的一部分,请通过单击“编辑”链接将其添加到问题中,不要将其作为答案发布。标题中缺少
多部分/混合的
内容类型。
           $eol = "\n";
           // main header (multipart mandatory)
           $headers = "From: $form". $eol;
           $headers .= "MIME-Version: 1.0" . $eol;
           **$headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"";**

           // message
            $body .= "--$boundary" . $eol;
            $body .= "Content-Transfer-Encoding: 7bit" . $eol.$eol;
            $body .= "This is a MIME encoded message." . $eol;
            $body .= "--".$boundary.$eol;
            $body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
            $body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
            $body .= $message.$eol;

            //attachment
            $body .= "--$boundary" . $eol;
            $body .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $eol;
            $body .= "Content-Transfer-Encoding: base64" . $eol;
            $body .= "Content-Disposition: attachment". $eol.$eol;
            $body .= $content . $eol;
            **$body .= "--$boundary--";**

            mail('email', 'subject', $body, $headers)