2017-03-07 22 views
0

我有这个代码,实际上不完全工作,我得到的电子邮件与所有的信息,但电子邮件不包括我试图的文件附加...(我得到它的唯一方法是删除jquery ajax函数)任何想法为什么? (我这样做是因为我想在提交表单后在同一页面中获得警报)感谢您的帮助!加载文件,通过电子邮件发送,并保持在同一页

HTML

<form class="ajax_nueva_idea" action="php/send_idea.php" method="post" enctype="multipart/form-data"> 


    <div> 
    <input required="" id="nombreproyecto" name="nombreproyecto" type="text" class="validate"> 
    </div> 

    <div> 
    <input required="" id="sector_for" name="sector_for" type="text" class="validate"> 
    </div> 

    <div> 
    <input required="" id="ubigeo_for" type="text" name="ubigeo_for" class="validate"> 
    </div> 

    <div> 
    <input required="" id="videoex_for" name="videoex_for" type="text" class="validate"> 
    </div> 

    <div> 
    <select required="" id="etapa_for" name="etapa_for"> 
    <option value="0"</option> 
    <option value="1">1/4</option> 
    <option value="2">2/4</option> 
    <option value="3">3/4</option> 
    <option value="4">4/4</option> 
    </select> 
    </div> 

    <div> 
    <input name="attachment" type="file"> 
    </div> 


    <div> 
    <textarea required="" id="info_for" name="info_for"></textarea> 
    </div> 

    <div> 
    <button type="submit" name="action" id="enviar_proyecto">Guardar 
    </button> 
    </div> 

</form> 

PHP

<?php 

    $to = '[email protected]'; 
    $nombreproyecto = $_POST['nombreproyecto']; 
    $sector = $_POST['sector_for']; 
    $ciudad = $_POST['ubigeo_for']; 
    $video = $_POST['videoex_for']; 
    $etapa = $_POST['etapa_for']; 
    $subject = "'gobig.com.co' Nuevo proyecto "."'".$nombreproyecto."'"; 

    $message = 

    "Cargo: ".$nombreproyecto."\n". 
    "Sector o industria: ". $sector ."\n". 
    "Ciudad: ".$ciudad ."\n". 
    "Link video:". $video ."\n". 
    "Etapa del proyecto:".$etapa."\n" . 
    "Frase convencer: ".$_POST['info_for']; 

    $tmpName = $_FILES['attachment']['tmp_name']; 
    $fileType = $_FILES['attachment']['type']; 
    $fileName = $_FILES['attachment']['name']; 

    $headers = "From: $nombreproyecto"; 

    if (file($tmpName)) { 

     $file = fopen($tmpName,'rb'); 
     $data = fread($file,filesize($tmpName)); 
     fclose($file); 


     $randomVal = md5(time()); 
     $mimeBoundary = "==Multipart_Boundary_x{$randomVal}x"; 


     $headers .= "\nMIME-Version: 1.0\n"; 
     $headers .= "Content-Type: multipart/mixed;\n" ; 
     $headers .= " boundary=\"{$mimeBoundary}\""; 


     $message = "This is a multi-part message in MIME format.\n\n" . 
     "--{$mimeBoundary}\n" . 
     "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . 
     "Content-Transfer-Encoding: 7bit\n\n" . 
     $message . "\n\n"; 


     $data = chunk_split(base64_encode($data)); 


     $message .= "--{$mimeBoundary}\n" . 
     "Content-Type: {$fileType};\n" . 
     " name=\"{$fileName}\"\n" . 
     "Content-Transfer-Encoding: base64\n\n" . 
     $data . "\n\n" . 
     "--{$mimeBoundary}--\n"; 
    } 

    $flgchk = mail ("$to", "$subject", "$message", "$headers"); 

    if($flgchk){ 
     //echo "A email has been sent to: $to"; 
    } 
    else{ 
     //echo "Error in Email sending"; 
    } 


?> 

JAVASCRIPT

jQuery('.ajax_nueva_idea').submit(function() { 

    $.ajax({ 
     url  : $(this).attr('action'), 
     type : $(this).attr('method'), 
     data : $(this).serialize(), 

     success : function(data) { 
        alert('Idea guardada exitosamente!'); 
        $('#enviar_proyecto').html('Guardado <i class="material-icons right">check</i>'); 

        }, 
     error : function(){ 
        alert('Something wrong'); 

        } 
    }); 

    return false; 
}); 
+0

使用phpmailer它非常好 –

回答

相关问题