0

I am trying to send an email with attachment in php using PHPMailer and JQuery.post method. When I try to post the data, email sents without attachment and shows couldnot access the file.

var file_data = $('#filemove').prop('files')[0];   //this is how I get the file 

in php Page:

if(!empty($_FILES['file_data']['name'])){

        $jcv_name = $_FILES['file_data']['name'];   
        $strFileName = basename($jcv_name);
        $strFileSuffix = substr($strFileName, strrpos($strFileName, ".") + 1);
        $strFileNameNoExt = basename($strFileName, ".{$strFileSuffix}");


        $strImgOrig = "images/cv/" . "{$strFileNameNoExt}_jcv_{$curtime}.{$strFileSuffix}";
        $dbFileName = "{$strFileNameNoExt}_jcv_{$curtime}.{$strFileSuffix}";
        if(move_uploaded_file($_FILES["file_data"]["tmp_name"], $strImgOrig)) {
            $applicantcv = $dbFileName;
        }

Do I need to use file API inorder to get the file input type in jquery.post method?

Edit 1

            <?php
        if(isset($_POST['file_data'])) { $file_data= $_POST['file_data']; } else { $file_data=""; }
        include_once("class/class.phpmailer.php");
        if(!empty($_FILES['file_data']['name'])){
        $jcv_name = $_FILES['file_data']['name'];   
        $strFileName = basename($jcv_name);
        $strFileSuffix = substr($strFileName, strrpos($strFileName, ".") + 1);
        $strFileNameNoExt = basename($strFileName, ".{$strFileSuffix}");
        $strImgOrig = "images/cv/" . "{$strFileNameNoExt}_jcv_{$curtime}.{$strFileSuffix}";
        $dbFileName = "{$strFileNameNoExt}_jcv_{$curtime}.{$strFileSuffix}";
        if(move_uploaded_file($_FILES["file_data"]["tmp_name"], $strImgOrig)) {
        $applicantcv = $dbFileName;
        }
        }
        $subject = "subject";       
        $strMsg = "message";
        $to="abc@ud.com"

        $objMail = new PHPMailer();
        $objMail->From = $email;                                //  Message From
        $objMail->AddAddress($to);                          //  Message To
        $objMail->Subject = $subject;                       //  Message Subject
        $objMail->Body = $strMsg;                               //  Message Body/Content
        $objMail->IsHTML(true);                                 //  Message type to HTML.
        $objMail->AddAttachment($strImgOrig);
        $objMail->Send();

        ?>

Script:

            function send_email()
        {
        var name=$("#fullname").val();
        var email=$("#cemail").val();
        var file_data = $('#filemove').prop('files')[0];
        $.post('send_emailfun.php',{name:""+name+"",email:""+email+"",file_data:""+file_data+""},function(data){
        $("#enquiry_success").modal("show");
        });
        }
Aishwaryas
  • 633
  • 2
  • 18
  • 44

0 Answers0