PHP MAIL WITH ATTACHMENT - 附件以编码文本的形式发送,而不是文件

时间:2016-09-10 08:11:36

标签: 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();     
?>

3 个答案:

答案 0 :(得分:0)

在标题中,您有:

$headers .= "Content-Type: multipart/mixed; boundary=\"PHP-mixed-".$boundary."\"\r\n";

因此,消息和附件之间的边界应为--PHP-mixed-$boundary。但是当您对邮件正文进行编码时,您正在发送:

$msg_body .= "--" . $boundary . " \r\n";

您在PHP-mixed-之前缺少$boundary前缀。将这些行更改为:

$msg_body .= "--PHP-mixed-" . $boundary . " \r\n";

此外,您没有在附件标题中的正确位置指定文件名。您可以在Content-type:行中找到它,它应该在Content-disposition:

$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";

答案 1 :(得分:0)

    <?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($content));
        $boundary = md5("ajantasoya");
        }


        $recipientEmail = "prg6@gmail.com";
        //$senderEmail = $_POST['email'];
        $senderEmail = "prg5@gmail.com";
        $Subject = "Test";
        $headers  = "MIME-Version: 1.0 \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();
        $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 .= "Qualifications : \r\n \r\n"; 
        $msg_body .= "             Stream      "; 
        $msg_body .= "Year of Completion      "; 
        $msg_body .= "Percentage      \r\n \r\n";  
        $msg_body .= "Others      "; 
        $msg_body .= $_POST['other-e']."      ";
        $msg_body .= $_POST['other-y']."      ";
        $msg_body .= $_POST['other-p']. "\r\n"; 
        $msg_body .= "Masters      "; 
        $msg_body .= $_POST['master-e']."      ";
        $msg_body .= $_POST['master-y']."      ";
        $msg_body .= $_POST['master-p']. "\r\n"; 
        $msg_body .= "Bachelors      "; 
        $msg_body .= $_POST['bachlor-e']."      ";
        $msg_body .= $_POST['bachlor-y']."      ";
        $msg_body .= $_POST['bachlor-p']. "\r\n"; 
        $msg_body .= "HSC            "; 
        $msg_body .= $_POST['hse-e']."      ";
        $msg_body .= $_POST['hse-y']."      ";
        $msg_body .= $_POST['hse-p']. "\r\n"; 
        $msg_body .= "SSC            "; 
        $msg_body .= $_POST['ssc-e']."      ";
        $msg_body .= $_POST['ssc-y']."      ";
        $msg_body .= $_POST['ssc-p']. "\r\n";
            $tot_emp_details = sizeof($_POST['cname']);
        //echo $tot_emp;

        $msg_body .= "Strengths: ".$_POST['strenght']. "\r\n"; 
        $msg_body .= "Weaknesses: ".$_POST['weakness']. "\r\n"; 
        $msg_body .= "Where do you see yourself in 5 years?: ".$_POST['next-5-year']. "\r\n"; 
        $msg_body .= "Where do you see yourself in 10 years?: ".$_POST['next-10-year']. "\r\n"; 
        $msg_body .= "What is your eventual career goal?: ".$_POST['career-goal']. "\r\n"; 
        $msg_body .= "Computer skills: ".$_POST['computer']. "\r\n"; 
        $msg_body .= "Languages: ".$_POST['language']. "\r\n"; 
        $msg_body .= "Others, if any: ".$_POST['other-any']. "\r\n"; 
            //Attachment Part
            $msg_body .= "--".$boundary."\r\n";
            //$msg_body .="Content-Type: ".$file_type. ";  Name=".$file_name."\r\n";
            $msg_body .= "Content-Type: application/octet-stream \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();     
    ?>

答案 2 :(得分:0)

这大致应该是这样的。您可能缺少的部分**

           $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)