使用AJAX,PHP发送带附件的电子邮件

时间:2015-03-16 18:05:11

标签: php jquery ajax email attachment

请帮忙。 我在我的反馈表单中使用此代码,使用PHP,Ajax,jQuery发送带附件的电子邮件,但是有错误 $ _files变量为空,我的文件没有发送。为什么呢?

HTML

<form action="javascript:void(0);" enctype="multipart/form-data" method="post" class="myfrm1">
    <input type="text" name="name">
    <input type="text" name="phone">
    <input type="text" name="email">
    <input type="file" name="file_attach">
</form>

JS

var data = $(".myfrm1").serialize();
$.ajax({
    url: "../php/submit_form_top.php",  
    data: data, 
    type: 'POST',
    cache: false,
    success: function () {              
        //if php returned 1/true (send mail success)    
        $('#popup1').fadeOut(500);
        $('#overlaybox,#popup2').fadeIn(500);
        setTimeout(function(){
            $('#overlaybox,#popup2').fadeOut(500);
        },3000);    
    }       
});

PHP

$file_attached = false;
if(isset($_FILES['file_attach'])) //check uploaded file
{
    //get file details we need
    $file_tmp_name    = $_FILES['file_attach']['tmp_name'];
    $file_name        = $_FILES['file_attach']['name'];
    $file_size        = $_FILES['file_attach']['size'];
    $file_type        = $_FILES['file_attach']['type'];

    //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));
    //now we know we have the file for attachment, set $file_attached to true
    $file_attached = true;
}

if($file_attached) //continue if we have the file
{
    $subject.='_top';
    # Mail headers should work with most clients
    $headers = "MIME-Version: 1.0\r\n";
    $headers = "X-Mailer: PHP/" . phpversion()."\r\n";
    $headers .= "From: ".$from."\r\n";
    $headers .= "Subject: ".$subject."\r\n";
    $headers .= "Reply-To: ".$from."" . "\r\n";
    $headers .= "Content-Type: multipart/mixed; boundary=".md5('boundary1')."\r\n\r\n";

    $headers .= "--".md5('boundary1')."\r\n";
    $headers .= "Content-Type: multipart/alternative;  boundary=".md5('boundary2')."\r\n\r\n";

    $headers .= "--".md5('boundary2')."\r\n";
    $headers .= "Content-Type: text/plain; charset=utf-8\r\n\r\n";
    $headers .= $message."\r\n\r\n";

    $headers .= "--".md5('boundary2')."--\r\n";
    $headers .= "--".md5('boundary1')."\r\n";
    $headers .= "Content-Type:  ".$file_type."; ";
    $headers .= "name=\"".$file_name."\"\r\n";
    $headers .= "Content-Transfer-Encoding:base64\r\n";
    $headers .= "Content-Disposition:attachment; ";
    $headers .= "filename=\"".$file_name."\"\r\n";
    $headers .= "X-Attachment-Id:".rand(1000,9000)."\r\n\r\n";
    $headers .= $encoded_content."\r\n";
    $headers .= "--".md5('boundary1')."--";
}else{
    //proceed with PHP email.
    $headers = 'From: '.$from.'' . "\r\n" .
    'Reply-To: '.$from.'' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
}

//send the mail
if (mail($to,$subject,$message,$headers)) $result = 1; else $result = 0;

提前感谢您的关注

0 个答案:

没有答案